diff --git a/Sorting/Bubble Sort/C++/Bubble_Sort_2.cpp b/Sorting/Bubble Sort/C++/Bubble_Sort_2.cpp index 2ab1354d..8e5b1e4f 100644 --- a/Sorting/Bubble Sort/C++/Bubble_Sort_2.cpp +++ b/Sorting/Bubble Sort/C++/Bubble_Sort_2.cpp @@ -1,41 +1,65 @@ #include using namespace std; -void bubbleSort(int A[],int n) -{ - int i,j; - int temp; - for(i=0;iA[j+1]) - { - temp=A[j]; - A[j]=A[j+1]; - A[j+1]=temp; - } - } - } +void bubbleSort(int array[], int size, int order){ + + if(order == 1){ + for(int i=0; i < size-1; i++){ + int flag = 0; + + for(int j=0; j array[j+1]){ + swap(array[j+1], array[j]); + flag = 1; + } + } + if(flag == 0){ + break; + } + } + } + + else if(order == 2){ + for(int i=0; i < size-1; i++){ + int flag = 0; + + for(int j=0; j>n; - int a[n]; - cout<<"Enter the element\n"; - for(int i=0;i>a[i]; - } - bubbleSort(a,n); - cout<<"The Sorted array is :-"; - for(int i=0;i> size; + + int array[size]; + + cout << "Enter the elements of the array:" << endl; + for(int i = 0; i < size; i++){ + cin >> array[i]; + } + + cout << "What type of ordering do you want: \n 1 - Ascending \n 2 - Descending" << endl; + cin >> order; + + bubbleSort(array, size, order); + + cout << "The sorted array is:" <