forked from piyush-kash/Hacktober2021-cpp-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble_Sort.cpp
More file actions
32 lines (31 loc) · 801 Bytes
/
Bubble_Sort.cpp
File metadata and controls
32 lines (31 loc) · 801 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
/* This is a cpp program to sort an array
using Bubble sort in ascending order ,
you can enter the number of elements in the array of your choice
and also the elements of your choice*/
#include<iostream>
using namespace std;
void Bubble(int arr[], int n){
if (n == 1)
return;
for (int i=0; i<n-1; i++)
if (arr[i] > arr[i+1])
swap(arr[i], arr[i+1]); //swap elements
Bubble(arr, n-1);
}
main() {
int n;
cout<<"Enter the number of elements:\n";
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<" in the array:\n";
cin>>arr[i];
}
int no = sizeof(arr)/sizeof(arr[0]);
cout << "Sorted Sequence ";
Bubble(arr, no);
for(int i = 0; i <no;i++){
cout << arr[i] << " ";
}
}