-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08_bubbleSort.cpp
43 lines (40 loc) · 1.21 KB
/
08_bubbleSort.cpp
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
#include <iostream>
using namespace std;
// function to fill the elements in the array
void fillArray(int arr[], int n){
for(int i=0; i<n; i++){
cout<<i<<": ";
cin>>arr[i];
}
}
// function to display the array
void displayArray(int arr[],int n){
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
}
// function for bubble Sort
void bubbleSort(int arr[],int n){
for(int p=0; p<=n-2; p++){
for(int c=0; c<=n-2-p; c++){
if(arr[c]>arr[c+1]){
swap(arr[c],arr[c+1]);
}
}
}
}
int main(){
// Here in the Bubble Sort, we compare two adjacent elements and if first element is greater than the second one then we swap the elements so that order would be preserved. With every pass the greatest unsorted element reaches its correct position........
int n = 8;
int arr[8];
fillArray(arr,n);
cout<<endl;
cout<<"The Input Array looks like...."<<endl;
displayArray(arr,n);
cout<<endl;
cout<<endl<<"Sorting the array...."<<endl;
bubbleSort(arr,n);
cout<<endl<<"After sorting, the array looks like...."<<endl;
displayArray(arr,n);
return 0;
}