-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathefficient_bubblesort.cpp
More file actions
58 lines (50 loc) · 1.53 KB
/
efficient_bubblesort.cpp
File metadata and controls
58 lines (50 loc) · 1.53 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
57
58
/*
author: Alessandro Ferrante
* efficient_bubblesort.cpp
*/
/*
| The file introduces an efficient 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
_ The algorithm is optimized by replacing the outermost for loop
_ with a while loop and a boolean variable,
- adding the while loop allows you to stop executing the main loop as soon
- as the array is completely sorted, thus reducing the total number of iterations.
? Essentially, it can be more efficient in specific cases where the array
? is already sorted or partially sorted, saving unnecessary iterations.
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
#define N 20
using namespace std;
template <typename T> void _swap(T& a, T& b){
T temp = a;
a = b;
b = temp;
}
template <typename T> void bubblesort(T * data, int n){
bool done = false;
while(!done){
done = true;
for(int j = 0; j < n - 1 ; j++){
if(data[j] > data[j+1]){
_swap<T>(data[j], data[j+1]);
done = false;
}
}
}
}
int main(int argc, char ** argv){
srand(time(NULL));
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;
}