-
Notifications
You must be signed in to change notification settings - Fork 437
Expand file tree
/
Copy pathBubble-Sort.cpp
More file actions
67 lines (51 loc) · 1.13 KB
/
Bubble-Sort.cpp
File metadata and controls
67 lines (51 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
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<size-1-i; j++){
if(array[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<size-1-i; j++){
if(array[j] < array[j+1]){
swap(array[j+1], array[j]);
flag = 1;
}
}
if(flag == 0){
break;
}
}
}
}
int main() {
int size;
int order;
cout << "Enter the size of the array:" << endl;
cin >> 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:" <<endl;
for (int i = 0; i < size; i++) {
cout << array[i] << " ";
}
return 0;
}