-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubblesort.cpp
More file actions
56 lines (52 loc) · 1.1 KB
/
bubblesort.cpp
File metadata and controls
56 lines (52 loc) · 1.1 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
using namespace std;
void bubbleSort(int[], int);
void displayVector(int[], int);
void permut(int&, int&);
int main()
{
int *vec, size;
vec = (int*)malloc(sizeof(int));
do
{
cout << "Please enter the size of your vector (size > 0): ";
cin >> size;
} while (size <= 0);
cout << "Enter value for each case of the vector: " << endl;
for(int i=0;i<size;i++)
{
cout << "[" << i+1 << "]: ";
cin >> vec[i];
cin.ignore();
}
cout << "The actual vector: " << endl;
displayVector(vec, size);
cout << "sorting....." << endl;
bubbleSort(vec, size);
cout << "After sorting with bubble sort: " << endl;
displayVector(vec, size);
free(vec);
}
void bubbleSort(int vec[], int size)
{
for(int i=0;i<size;i++)
{
for(int j=size;j>i;j--)
{
if(vec[j-1] > vec[j]) permut(vec[j-1], vec[j]);
}
}
}
void displayVector(int vec[], int size)
{
for(int i=0;i<size;i++)
{
cout << vec[i] << " ";
}
}
void permut(int& x, int& y)
{
int tmp = x;
x=y;
y=tmp;
}