-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathBubbleSort.cpp
More file actions
53 lines (43 loc) · 959 Bytes
/
BubbleSort.cpp
File metadata and controls
53 lines (43 loc) · 959 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
#include <bits/stdc++.h>
using namespace std;
void printArray(int array[], int size)
{
for (int i = 0; i < size; i++)
{
cout << array[i] << " ";
}
cout << endl;
}
int main()
{
int size;
cin >> size;
int array[size];
for (int i = 0; i < size; i++)
{
cin >> array[i];
}
cout << "Before Sort: ";
printArray(array, size);
cout << endl;
// Bubble Sort Implementation
for(int i =1; i<size;i++){
int flag = 0;
cout<< "iteration no : " << i << endl ;
for(int j=0; j<size-i; j++){
if(array[j]>array[j+1]){
int temp = array[j];
array[j]=array[j+1];
array[j+1]=temp;
flag = 1;
}
printArray(array, size);
}
cout<< endl;
if(flag == 0) break;
}
cout << "After Sort: ";
printArray(array, size);
cout << endl;
return 0;
}