-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.cpp
More file actions
55 lines (40 loc) · 840 Bytes
/
bubbleSort.cpp
File metadata and controls
55 lines (40 loc) · 840 Bytes
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
#include <bits/stdc++.h>
#include <ctime>
using namespace std;
void bubbleSort(int arr[], int &n) {
int temp;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(arr[j] > arr[j+1]) {
swap(arr[j], arr[j+1]);
}
}
}
}
void array(int arr[], int &n) {
cout << endl;
cout << "Unsorted array: " << endl;
for (int i = 0; i<n; i++) {
arr[i] = rand() % 100;
cout << arr[i] << " ";
}
bubbleSort(arr, n);
}
int main() {
srand(time(NULL));
setlocale(LC_CTYPE, "Polish");
system("title Bubble Sort");
system("color ce");
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
array(arr, n);
cout << endl;
cout << endl;
cout << "Sorted array: " << endl;
for (int i = 0; i<n; i++) {
cout << arr[i] << " ";
}
return(0);
}