From f48020f8ce0f7a8a4dadbc27c7239a41ed4d2581 Mon Sep 17 00:00:00 2001 From: Sanjeev Kumar Kushwaha Date: Mon, 30 Oct 2017 21:54:37 +0530 Subject: [PATCH] Update bubble_sort.cpp --- sorting/bubble_sort.cpp | 63 +++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/sorting/bubble_sort.cpp b/sorting/bubble_sort.cpp index f928e1b..b4cc563 100644 --- a/sorting/bubble_sort.cpp +++ b/sorting/bubble_sort.cpp @@ -1,32 +1,41 @@ -//complexity = O(n^2) +// C program for implementation of Bubble sort #include -int main() + +void swap(int *xp, int *yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +// A function to implement bubble sort +void bubbleSort(int arr[], int n) { -int n; -scanf("%d",&n); -int arr[n]; -int i; - -for(i=0;i arr[j+1]) + swap(&arr[j], &arr[j+1]); } - -int j,k,count=0; -for(j=0;jarr[k+1]){ - int temp,a; - temp=arr[k]; - arr[k]=arr[k+1]; - arr[k+1]=temp; - count++; - } - } + +/* Function to print an array */ +void printArray(int arr[], int size) +{ + int i; + for (i=0; i < size; i++) + printf("%d ", arr[i]); + printf("n"); } -for(i=0;i