forked from Anubhav-1020/Hacktoberfest-Beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bubble_Sort.cpp
42 lines (37 loc) · 837 Bytes
/
Bubble_Sort.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
//Bubble Sort
//Bubble sort is one of the simplest sorting algorithms. It is a stable sort algorithm.
//The program for bubble sort is C++ is as follows :
#include <iostream>
using namespace std;
int main()
{
//Enter number of elements in the array
int n;
cin>>n;
int nums[n];
//Enter the numbers to be sorted
for(int i=0;i<n;i++)
cin>>nums[i];
//Bubble Sort
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(nums[j] > nums[j+1])
{
int temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
cout<<"Sorted numbers are :\n";
for(int i=0;i<n;i++)
cout<<nums[i]<<" ";
return 0;
}
//Time Complexity: O(n^2)
//Auxiliary Space: O(1)
//Example:
//Input: n=5 , nums[] = {2, 7, 4, 10, 1}
//Output: nums[] = {1, 2, 4, 7, 10}