-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathBubbleSort_OptimisedCode.cpp
63 lines (54 loc) · 1.01 KB
/
BubbleSort_OptimisedCode.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
// Optimized implementation of Bubble sort to avoid extra spaces.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void bubbleSort(int arr[], int n)
{
int i, j, flag, temp;
for (i = 0; i < n-1; i++)
{
flag=0;
for (j = 0; j < n-1-i; j++)
{
if (arr[j] > arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
flag=1;
}
}
// IF no two elements were swapped
// by inner loop, then break
if (flag==0)
break;
}
}
// Function to print an array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout <<" "<< arr[i];
}
}
// Driver program to test above functions
int main()
{
int n;// size of the array
cout<<"Enter the size of Array"<<endl;
cin>>n;
int arr[n];
cout<<"Enter the elements of the Array"<<endl;
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
bubbleSort(arr, n);
cout <<"Sorted array: \n";
printArray(arr, n);
return 0;
}
// Time Complexity:
// Best Case: O(n)
// Worst case: O(n^2)