forked from aditya-bijapurkar/Sorting-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubbleSort.cpp
75 lines (62 loc) · 1.94 KB
/
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
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
/*
BUBBLE SORT!
Bubble sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.
After every pass (transversing) the array is passed from next element, continued till 2nd last element in array.
Maximum number of passes = (n-1).
Time complexities:
worst case: O(n²)
average case: O(n logn)
best case: O(n) //alerady sorted
*/
#include <iostream>
using namespace std;
void swap(int *a, int *b){
//two elements are swapped using pointers as actual value has to be changed.
int temp=*a;
*a=*b;
*b=temp;
}
void bubbleSort(int arr[], int n){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){ //after every i'th element, array is transversed for next j elements.
if(arr[j]>arr[j+1]){
swap(&arr[j], &arr[j+1]);
}
}
}
cout<<"\nThe Bubble Sorted array is: ";
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
}
int main(){
//creating an array.
int n;
cout<<"Enter number of elements in the array: ";
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cout<<"Enter value of "<<i<<" element: ";
cin>>arr[i];
}
//printing the array.
cout<<"\nThe array is: ";
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
bubbleSort(arr,n);
return 0;
}
/*
Illustration:
array -> {41,66,32,2} 1st pass
{(41,66),32,2} (41 & 66 are compared and swapped if required)
{41,(32,66),2}
{41,32,(2,66)}
{32,2,41,66} 2nd pass
{2,32,41,66} 3rd pass //sorted array
total passes required = n-1 (here 4-1=3)
*/
/*
code written by Aditya Bijapurkar
*/