-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.cpp
More file actions
99 lines (55 loc) · 2.91 KB
/
Copy pathBubbleSort.cpp
File metadata and controls
99 lines (55 loc) · 2.91 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<bits/stdc++.h>
using namespace std;
// Now lets see about Bubble sort - here we push the max element at the end
// Here the TC for Worst and Avg case - O(N^2) , but for best case, when at the first time only if there is no swap, it means the whole array is already sorted. Hence, in that case the best TC is - O(1) , it means the loop runs for only i=1, and then break from there
// In this bubble sort, we compare elements with the adjacent element and push the maximum elememt at the end of the array.
// 3 2 6 7 1 --> 2 3 6 1 7 - Now after 1st iteration, highest is in last
// 2 3 1 6 7 - till 2nd last sorted
// 2 1 3 6 7 - till, 3rd last sorted
// 1 2 3 6 7 - till 4th from end sorted
// Not required to check for last single element which is at index 0, as a single element is already sorted
// lets see how the loop will go for bubble sort:
// 1st time - 0 -- n-1
// 2nd time - 0 -- n-2
// 3rd time - 0 -- n-3
// |
// |
// - 0 -- 1 --> not required to go to till 0th index, as the single element at the end is already sorted.
// Hence the best time complexity for Bubble Sort is - O(N) , when for i=1, j runs for N times and as array is sorted , no swap betn Elements done, and just break out from there.
void Bubble_Sort(int n, int arr[]){
int flag=0;
for(int i=n-1 ; i>=1 ; i--){
for(int j=0 ; j<=i-1 ; j++){ // Here have to run upto n-1 , because we are comparing j with j+1 , so when j==i , we are comparing with i+1 index, which is not there, so it will give "Runtime Error".
if(arr[j]>arr[j+1]){
swap(arr[j] , arr[j+1]);
flag=1;
}
}
if(flag==0) break; // It means no swaping occurs betn elements, and all elements are sorted in asc order.
}
}
void Bubble_sort1(int n , int arr[]){
for(int i=0 ; i<n-1 ; i++){ // i goes till n-2
for(int j=0 ; j< n-i-1 ; j++){ // Here, when i=0 and j=n-1 , then it compared to j=n, which index is not present in Array, as it goes from 0 to n-1 , hence for j we have run the loop till n-2, so it compares to n-2 and n-1 only max.
if(arr[j] > arr[j+1]){
swap(arr[j] , arr[j+1]);
}
}
}
}
int main(){
int n;
cout<<"Enter the size of Array: ";
cin>>n;
int arr[n];
cout<<"Enter the array elements: ";
for(int i=0 ; i<n ; i++){
cin>>arr[i];
}
Bubble_Sort(n , arr);
Bubble_sort1(n, arr);
// Now lets print the sorted array
for(auto i : arr){
cout<<i <<" ";
}
}