forked from argonautica/sorting-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort_withPasses.cpp
73 lines (64 loc) · 1.21 KB
/
BubbleSort_withPasses.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
// By Arshdeep (github.com/ar33h)
#include<iostream>
using namespace std;
int arr[10];
int n, i;
void bubble_sort(int arr[], int n);
void display(int arr[], int n);
void passes(int arr[], int n);
int main()
{
cout<<"\n------BUBBLE SORT------";
cout<<"\n\nEnter no. of Array elements: ";
cin>>n;
cout<<"\nEnter Array elements: ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"\nUnsorted Array: [";
for(i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
cout<<"]\n";
bubble_sort(arr,n);
display(arr, n);
}
void bubble_sort(int arr[], int n)
{
int j, k;
int temp;
for(i=0; i<n-1; i++)
{
for(j=0; j<n-(i+1); j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
passes(arr, n);
}
}
}
}
void display(int arr[], int n)
{
cout<<"\n\nSorted Array: [";
for(i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
cout<<"]";
}
void passes(int arr[], int n)
{
int x;
cout<<"\nPass "<<i+1<<": [";
for(x=0; x<n; x++)
{
cout<<arr[x]<<" ";
}
cout<<"]";
}