From 1a56fb5e4751873b41f2937e72f3b1d2a29d1846 Mon Sep 17 00:00:00 2001 From: Arpit61 <45363400+Arpit61@users.noreply.github.com> Date: Sat, 26 Sep 2020 00:46:38 +0530 Subject: [PATCH] Update bubble_sort --- bubble_sort | 73 ++++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/bubble_sort b/bubble_sort index 0f57f61..32ef6ad 100644 --- a/bubble_sort +++ b/bubble_sort @@ -1,36 +1,41 @@ +#include +using namespace std; + +void swap(int *xp, int *yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + + +void bubbleSort(int arr[], int n) +{ + int i, j; + for (i = 0; i < n-1; i++) + + + for (j = 0; j < n-i-1; j++) + if (arr[j] > arr[j+1]) + swap(&arr[j], &arr[j+1]); +} + -#include -#define MAX 100 -int main(void) -{ - int arr[MAX],i,j,temp,n,xchanges; - printf("Enter the number of elements : "); - scanf("%d",&n); - for(i=0; i arr[j+1]) - { - temp = arr[j]; - arr[j] = arr[j+1]; - arr[j+1] = temp; - xchanges++; - } - } - if(xchanges==0) /*If list is sorted*/ - break; - } - printf("Sorted list is :\n"); - for(i=0; i