forked from thisisshub/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble_sort.cpp
More file actions
29 lines (29 loc) · 783 Bytes
/
Bubble_sort.cpp
File metadata and controls
29 lines (29 loc) · 783 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
// Bubble sort Time complexity O(n^2)
void bubble_Sort(int arr[],int n){
bool swapped;
for(int i = 0 ; i < n - 1 ; i++ )
{
swapped=false;
for( int j = 0 ; j < n - i - 1 ; j++ ){
if(arr[j]>arr[j+1]){
swap(arr[j],arr[j+1]);
swapped=true;
}
}
if(swapped==false) //if no swapping takes place in the entire iteration then the array is sorted.
break;
}
}
// Function to swap the values of the array, part of bubble sort
void swap(int *a, int *b){
int temp;
temp=*a;
*a=*b;
*b=temp;
}
//Function to print the array after sorting
void printArray(int arr[],int n){
for( int i = 0; i < n; i ++){
cout<<arr[i]<<" ";
}
}