-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathBubbleSort.cpp
50 lines (48 loc) · 1.24 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
//C++ Program to implement Bubble Sort
#include <bits/stdc++.h>
using namespace std;
//Function to perform bubble sort
//An O(n^2) algorithm to sort an array
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
{
int flag=0; //initializing a flag element
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
flag=1; //the flag value will be changed only when swapping will take place in a pass
}
}
if(flag==0)
break; // this will ensure that if swapping did'nt take place outer loop will terminate, thus making the code efficient
}
return;
}
//Driver Code
int main()
{
int n; //Size of array
cout << "Enter the size of the array followed by the array elements: " << endl;
cin >> n;
int arr[n] = {0}; //Initializing array with all elements 0
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
bubbleSort(arr, n);
cout << "Sorted Array is: " << endl;
for(int i = 0 ; i < n; i++)
{
cout<<arr[i]<<" ";
}
//Buffer flush
cout << endl;
return 0;
}