forked from sambhav2612/SortingAlgorithms.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble-sort.cpp
More file actions
41 lines (32 loc) · 1.09 KB
/
bubble-sort.cpp
File metadata and controls
41 lines (32 loc) · 1.09 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
# include "includeAll.h"
using namespace std;
void sort (int array[], int size) { //function for sorting the array using bubble sort algorithm
for (int i = 0; i < size-1; ++i) {
for (int j = i+1; j < size; ++j) {
if (array[i] > array[j]) {
swap (&array[i], &array[j]); //finding out the smallest of the elements present in the unsorted part of array and storing in the ith index
}
}
cout << "Iteration #" << i+1 << ":" ; //printing the array after each iteration where i+1 elements have been sorted
for (int k = 0; k < size; ++k)
cout << array[k];
cout << endl;
}
}
int main () {
int size = 0, array[100] = { 0 };
cout << endl << "Enter Size: ";
cin >> size;
cout << endl << "Enter elements: " << endl;
for (int i = 0; i < size; ++i)
cin >> array[i];
cout << endl << "Array before sorting: " << endl;
for (int i = 0; i < size; ++i)
cout << array[i];
cout << endl;
sort (array, size); //calling the bubble sort function to sort the given array
cout << endl << "Array after sorting: " << endl;
for (int i = 0; i < size; ++i)
cout << array[i];
return 0;
}