-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab#11__Farhanulhaq__Task#2.cpp
More file actions
60 lines (52 loc) · 1.39 KB
/
Copy pathLab#11__Farhanulhaq__Task#2.cpp
File metadata and controls
60 lines (52 loc) · 1.39 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
#include <iostream>
using namespace std;
void bubbleSort(int arr[],int n) {
int comparisons= 0,swaps= 0;
for (int i= 0;i < n - 1;i++) {
for (int j= 0;j <n - i - 1;j++) {
comparisons++;
if (arr[j] <arr[j + 1]) {
swap(arr[j],arr[j + 1]);
swaps++;
}
}
}
cout<<"Comparisons: "<<comparisons<<", Swaps: "<<swaps<<endl;
}
void printArray(int arr[],int n) {
for (int i=0;i< n;i++) {
cout<<arr[i]<<" ";
}
cout<<endl;
}
void testBubbleSort() {
int arr1[] ={5,2,9,1,5,6};
int n1 =sizeof(arr1)/sizeof(arr1[0]);
int arr2[] ={1,2,3,4,5,6};
int n2 =sizeof(arr2)/sizeof(arr2[0]);
int arr3[] ={6,5,4,3,2,1};
int n3 =sizeof(arr3)/sizeof(arr3[0]);
cout<<"Original array: ";
printArray(arr1,n1);
bubbleSort(arr1,n1);
cout<<"Sorted array: ";
printArray(arr1,n1);
cout<<"Original array: ";
printArray(arr2,n2);
bubbleSort(arr2,n2);
cout<<"Sorted array: ";
printArray(arr2,n2);
cout<<"Original array: ";
printArray(arr3,n3);
bubbleSort(arr3,n3);
cout<<"Sorted array: ";
printArray(arr3,n3);
}
// Time Complexity:
//Worst Case: O(n^2)
//Best Case: O(n)
// Average Case: O(n^2)
int main() {
testBubbleSort();
return 0;
}