-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBUBBLE_SORT.cpp
More file actions
111 lines (106 loc) · 3.33 KB
/
BUBBLE_SORT.cpp
File metadata and controls
111 lines (106 loc) · 3.33 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
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
using namespace std;
void swap(int *a,int *b) // HERE WE USE * BEACUSE IN FUNTION WE PASSED ADDRESS SO NOW IF WE WANT TO FETCH THE VALUE INSIDE IT
// WE USE * SO (*&arr[j] )WILL GIVE THE VALUE INSIDE IT FOR EXAPLE 1 2 3 WHATEVER WE STORED
// & DENOTES THE ADDRESS WHICH CAN BE IN FORM 200455 WHATEVER AND * KILLS & AND GIVES ITS TRUE VALUE
//SO &arr[j]= 2255000 (address) AND *&arr[J]=4(value)
{
int *temp;
*temp=*a;
*a=*b;
*b=*temp;
}
int bubble_sort(int arr[],int n)
{
// cout<<"INSIDE BUBBLE SORT FUNTION "<<endl;
int i,j;
int swap_count=0;
for( i=0;i<n-1;i++)
{
//cout<<i<<" value of i : \n";
for(j=0;j<n-i-1;j++)
{
// cout<<j<<" value of j : \n";
if(arr[j]>arr[j+1])
{
//cout<<"Values to be swapped : "<<arr[j]<<" and "<<arr[j+1]<<endl;
swap(&arr[j],&arr[j+1]); //HERE WE ARE PASSING THE ADDRESS OF BOTH SO THAT CHNAGES CAN BE MADE
swap_count++; // THIS IS CALLED PASS BY REFRENCE
}
}
}
// cout<<"value of i and j : "<<i<<" "<<j<<endl;
return swap_count;
}
////////////BUBBLE SORT RECURSSIVELY////////
void bubble_sort_recurrsive(int arr[],int n)
{
if(n==1)
{
return;
}
for(int i=0;i<n-1;i++)
{
if(arr[i]>arr[i+1])
{
swap(&arr[i],&arr[i+1]);
}
}
bubble_sort_recurrsive(arr,n-1);//HERE WE ARE PASSING THE ARRAY THAT PART WHICH IS REQUIRED
// THAT IS FOR SECOND TIME WE DONT NEED LAST ELEMENT AS ITS IN SORTED ORDER SO NOW WE WANT TO SORT ARRRAY FROM
//0 TO n-1 AND THIS IS HOW IT WORKS
}
/*
IN BUBBLE SORT WE TRAVEL AND IF THE PREVIOUS ELEMENT IS GREATER THAN THE NEXT WE JUST SWAP AND BY THIS
THE LARGEST ELEMENT GOES TO THE LAST
WE DO SAME THING AGAIN BUT NOW ARRAY TRAVERSAL IS SHORTED
WE GO TILL n-2 BECAUSE LAST ELEMENT IS THE LARGEST WE KNOW THEN WHY TO TRAVERSE THAT PART
EXAPME : 0 1 2 3 4
INDEX : 0 1 2 3 4
FIRST LOOP IS FROM INDEX 0 TO 3 ....WHY? BECAUSE WE HAVE TO SWAP THAT ELEMNT AND NEXT TO IT
SO IF LOOP RUNS FROM 0 TO 4 THEN LAST EMENT WILL BE SWAPPED WITH WHOM ....THERE IS NO ELEMENT AFTER 4TH INDEX NO 5TH INDEX EXIST
SO KEEP THIS IN MIND
NOW SECOND LOOP IS FROM
0 TO n-i-1
SO i=0 TO i=5-0-1 HERE n =5 AS 5 ELEMENTS
i TO i
0 4
0 3
0 2
0 1
AS 0 < 5-4-1 = 0<0 THIS CONDITION FAILS SO LOOP STOPS HERE THAT IS THE SECOND LOOP
*/
void print(int arr[],int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main()
{
int n;
cout<<"ENTER SIZE : "<<endl;
cin>>n;
int arr[n];
cout<<"ENTER ELEMENTS : "<<endl;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<endl;
cout<<"ORIGINAL ARRY : "<<endl;
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
cout<<"BUBBLE SORT CALLED : "<<endl;
int count=bubble_sort(arr,n);
cout<<"PRINT FUNTION CALLED "<<endl;
print(arr,n);
cout<<"TOTAL SWAP COUNTS : "<<count<<endl;
cout<<"FUNTION COMPLETED "<<endl;
return 0;
}