-
Notifications
You must be signed in to change notification settings - Fork 97
/
bubbleSort.cpp
44 lines (44 loc) · 1023 Bytes
/
bubbleSort.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
#include <iostream>
#include <vector>
using namespace std;
void bubblesort(vector<int>);
int main()
{
int n;
cout << "Enter the size of the array" << endl;
cin >> n;
vector<int> arr; //vector used like array
int i=0;
int num; //number for dynamic array
cout << "Enter the elements of the array" << endl;
for(i=0;i<n;i++)
{
cin >> num;
arr.push_back(num);
}
bubblesort(arr);
}
void bubblesort(vector<int> array)
{
cout << "BUBBLE SORTED ARRAY: ";
//doing bubblesort for doing ascending order
for(int i=array.size()-1;i>0;i--) //loop for decreasing array size
{
for(int j=0;j<i;j++) //loop for
{
if(array[j] > array[j+1])
{
//swapp
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
//output the array
for(int i=0;i<array.size();i++)
{
cout << array[i] << " ";
}
cout << endl;
}