-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.cpp
More file actions
34 lines (34 loc) · 852 Bytes
/
bubbleSort.cpp
File metadata and controls
34 lines (34 loc) · 852 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
#include<iostream>
using namespace std;
void printArray(int *A, int n){
for(int i = 0; i<n; i++){
cout<<A[i]<<" ";
}
cout<<endl;
}
void bubbleSort(int *A, int n){
int temp; int isSorted = 0;
for(int i = 0; i<n-1; i++){
cout<<"Working for pass number: "<<i+1<<endl;
isSorted = 1;
for(int j = 0; j<n-i-1; j++){
if(A[j]>A[j+1]){
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
isSorted = 0;
}
}
if(isSorted){
return;
}
}
}
int main(){
int A[] = {12, 54, 65, 7, 23, 9};int n = sizeof(A)/sizeof(int);
cout<<"Printing array before Sorting"<<endl;
printArray(A,n );
bubbleSort(A, n);
cout<<"Printing array after Sorting"<<endl;
printArray(A, n);
return 0;}