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