forked from maxiee/MyCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble.cpp
46 lines (36 loc) · 776 Bytes
/
bubble.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
45
#include <iostream>
using namespace std;
void swap(int *a, int *b);
void printArray(int *v, int n);
int main() {
int array[5] = {30, 10, 0, 50, 25};
int i, j;
int min_index;
printArray(array, 5);
for(i = 0; i < 4; i++) {
min_index = i;
for (j=i+1; j<5; j++) {
if(array[j] < array[min_index]) {
min_index = j;
}
}
if (i != min_index) {
swap(array[i], array[min_index]);
}
printArray(array, 5);
}
printArray(array, 5);
return 0;
}
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void printArray(int *v, int n){
for (int i=0; i<n; i++) {
cout << *v << " ";
v++;
}
cout << endl;
}