-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathBubbleSort.cpp
More file actions
39 lines (36 loc) · 822 Bytes
/
BubbleSort.cpp
File metadata and controls
39 lines (36 loc) · 822 Bytes
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
// CPP program for sorting the elements using Bubble sort
#include<iostream>
using namespace std;
int main()
{
int n, i, arr[50], j, temp;
//Taking input from user
cout<<"Enter the Size (max. 50): ";
cin>>n;
cout<<"Enter "<<n<<" Numbers: ";
for(i=0; i<n; i++)
cin>>arr[i];
cout<<endl;
//the algorithm starts from here->
for(i=0; i<(n-1); i++)
//outer loop
{
for(j=0; j<(n-i-1); j++)
//inner loop
{
if(arr[j]>arr[j+1])
{
//swapping
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
cout<<"The New Array is: ";
cout<<endl;
//Printing the array
for(i=0; i<n; i++)
cout<<arr[i]<<" ";
cout<<endl;
}