forked from ZoranPandovski/al-go-rithms
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbubble_sort.cpp
More file actions
38 lines (34 loc) · 778 Bytes
/
bubble_sort.cpp
File metadata and controls
38 lines (34 loc) · 778 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
//PROGRAM TO SORT AN ARRAY USING BUBBLE SORT METHOD
#include<iostream> // INCLUDING LIBRARIES
using namespace std;
//FUNCTION TO SORT ARRAY
void bubble_sort(int arr[], int sz)
{
for(int i=0;i<sz-1;i++)
{
for(int j=0;j<sz-1-i;j++)
{
if(arr[j]>arr[j+1])
{
swap(arr[j],arr[j+1]);
}
}
}
}
// MAIN FUNCION
int main()
{
int sz;
cout<<"Enter the size of Array : ";
cin>>sz;
// TAKING INPUT FROM USER
int arr[sz];
cout<<"\nEnter Elements : ";
for(int i=0; i<sz;i++)
cin>>arr[i];
bubble_sort(arr,sz); // FUNCTION CALLING
cout<<"\nSorted Array : "; // DISPLAY SORTED ARRAY
for(int i=0;i<sz;i++)
cout<<" "<<arr[i];
return 0;
}