-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubblesort.cpp
More file actions
53 lines (43 loc) · 1.12 KB
/
bubblesort.cpp
File metadata and controls
53 lines (43 loc) · 1.12 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
/*
author: Alessandro Ferrante
* bubblesort.cpp
*/
/*
| The file introduces the Bubble Sort sorting algorithm with templates
| The bubble sorting algorithm works by comparing adjacent elements
| and swapping them if they are out of order.
| This process is repeated until there are exchanges equal to the length of the array
*/
#include <iostream>
#include <cstdlib>
#include <time.h>
#define N 20
using namespace std;
template <typename T> void _swap(T& a, T& b){
T tmp;
tmp = a;
a = b;
b = tmp;
}
template <typename T> void bubblesort(T * data, int n){
for (int i = 0; i < n; i++){
for(int j = 0; j < n - 1; j++){
if(data[j] > data[j+1])
// T temp = data[j];
// data[j] = data[j+1];
// data[j+1] = temp;
_swap<T>(data[j], data[j+1]);
}
}
}
int main(int argc, char ** argv ){
float data[N];
for(int i = 0; i< N; i++){
data[i] = ((float)rand()/ RAND_MAX * 50);
}
bubblesort<float>(data, N);
for(int i = 0; i < N; i++){
cout << i << " " << data[i] << endl;
}
return 0;
}