diff --git a/Sorting Algorithms/Bubble Sort.cpp b/Sorting Algorithms/Bubble Sort.cpp index 43ed6b1..b2039dc 100644 --- a/Sorting Algorithms/Bubble Sort.cpp +++ b/Sorting Algorithms/Bubble Sort.cpp @@ -1,60 +1,70 @@ /* - Petar 'PetarV' Velickovic + Maria LuĂ­sa Mendes Algorithm: Bubble Sort */ -#include -#include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#define MAX_N 5001 using namespace std; -typedef long long lld; - -int n; -int niz[MAX_N]; - -//Bubble sort algoritam za sortiranje niza -//Slozenost: O(n^2) - -inline void bubbleSort() -{ - bool swapped; - int it = 0; - do - { - swapped = false; - for (int i=0;i niz[i+1]) - { - swap(niz[i], niz[i+1]); - swapped = true; - } + +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; } - it++; - } while (swapped); + } + if(flag == 0){ + break; + } + } + } + + else if(order == 2){ + for(int i=0; i < size-1; i++){ + int flag = 0; + + for(int j=0; j> 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:" <