-
Notifications
You must be signed in to change notification settings - Fork 59
/
BubbleSort.cpp
50 lines (48 loc) · 1.3 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
#include<bits/stdc++.h>
#define swap(x,y) { x = x + y; y = x - y; x = x - y; }
using namespace std;
/**
* Function to Sort the array using Modified Bubble Sort Algorithm
* @param arr: Array to be Sorted
* @param n: Size of array
* @return : None
*/
void bubbleSort(int arr[], int n)
{
int i, j;
bool flag;
// Outer pass
for(i = 0; i < n; i++)
{
flag = false; // Set flag as false
for(j = 0; j < n-i-1; j++)
{
// Compare values
if( arr[j] > arr[j+1])
{
swap(arr[j],arr[j+1]);
flag = true;
}
}
// If no to elements are swapped then
// array is sorted. Hence Break the loop.
if(!flag)
{
break;
}
}
}
int main(int argv, char* argc[])
{
int arr[] = {1,5,6,8,3,4,7,2,9};
int n = sizeof(arr)/sizeof(int);
cout<<"Unsorted Array :";
for(int i=0;i<n;i++) // Print the Original Array
cout<<arr[i]<<" ";
cout<<endl;
bubbleSort(arr,n); // Call for Bubble Sort Function
cout<<"Sorted Array :";
for(int i=0;i<n;i++) // Print the Sorted Array
cout<<arr[i]<<" ";
return(0);
}