From 325bfcaa0876f2de8b505b4c73d06ead07a12c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maria=20Lu=C3=ADsa=20Mendes?= <72216924+lumendesp@users.noreply.github.com> Date: Tue, 13 Oct 2020 23:07:27 -0300 Subject: [PATCH] Update bubble_sort.cpp --- .../Sorting/Bubble Sort/bubble_sort.cpp | 90 +++++++++++++------ 1 file changed, 62 insertions(+), 28 deletions(-) diff --git a/Competitive Coding/Sorting/Bubble Sort/bubble_sort.cpp b/Competitive Coding/Sorting/Bubble Sort/bubble_sort.cpp index 42634a7ce..8e5b1e4f8 100644 --- a/Competitive Coding/Sorting/Bubble Sort/bubble_sort.cpp +++ b/Competitive Coding/Sorting/Bubble Sort/bubble_sort.cpp @@ -1,31 +1,65 @@ -/* Part of Cosmos by OpenGenus Foundation */ #include -#include -#include - -void bubble_sort(std::vector &v) -{ - for(int i=0; i < v.size(); ++i) - for(int j=i+1; j < v.size(); ++j) - { - if (v[i] > v[j]) - std::swap(v[i],v[j]); - } +using namespace std; + +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 input_array; - std::cout<<"Enter the number of items in the array: "; - int array_size; - std::cin>>array_size; - for(int i=0;i>input_number; - input_array.push_back(input_number); - } - bubble_sort(input_array); - for(auto x :input_array) - std::cout<> 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:" <