From 643bfa5dba6dbd5c517b1bb20e0dc12c51addbd6 Mon Sep 17 00:00:00 2001 From: Achint Srivastava <32853258+achint769muj@users.noreply.github.com> Date: Mon, 3 Dec 2018 21:43:10 +0530 Subject: [PATCH] Added the code for Bubble sort in C language (#23389) * Added the code for Bubble sort in C language * fix: code formatting and added additional code --- .../sorting-algorithms/bubble-sort/index.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/guide/english/algorithms/sorting-algorithms/bubble-sort/index.md b/guide/english/algorithms/sorting-algorithms/bubble-sort/index.md index 38b35c2c61..06b89eaabd 100644 --- a/guide/english/algorithms/sorting-algorithms/bubble-sort/index.md +++ b/guide/english/algorithms/sorting-algorithms/bubble-sort/index.md @@ -161,6 +161,38 @@ def swap( A, x, y ): A[y] = tmp ``` +### Example in C +```c +#include + +int BubbleSort(int array[], int n); + +int main(void) { + int arr[] = {10, 2, 3, 1, 4, 5, 8, 9, 7, 6}; + BubbleSort(arr, 10); + + for (int i = 0; i < 10; i++) { + printf("%d", arr[i]); + } + return 0; +} +int BubbleSort(int array[], n) +{ +for (int i = 0 ; i < n - 1; i++) + { + for (int j = 0 ; j < n - i - 1; j++) //n is length of array + { + if (array[j] > array[j+1]) // For decreasing order use + { + int swap = array[j]; + array[j] = array[j+1]; + array[j+1] = swap; + } + } + } +} +``` + ### More Information - [Wikipedia](https://en.wikipedia.org/wiki/Bubble_sort)