1
1
/* *
2
2
* @file
3
- * @brief Implementation of the Flash Sort algorithm.
3
+ * @brief Implementation of the Flash Sort algorithm with self-tests .
4
4
* @details
5
5
* Flash Sort is an in-place, linear-time sorting algorithm for uniformly distributed data.
6
6
* It divides the data into classes, rearranges elements into their respective classes,
10
10
* @author [Sachin Pangal](https://github.com/orthodox-64)
11
11
*/
12
12
13
+ #include < algorithm>
14
+ #include < cassert>
13
15
#include < iostream>
14
16
#include < vector>
15
- #include < algorithm>
16
17
17
18
/* *
18
19
* @brief Sorts an array using the Flash Sort algorithm.
19
20
* @param arr The array to be sorted.
20
- * @param n The number of elements in the array.
21
21
*/
22
-
23
- void flashSort (std::vector<float >& arr) {
22
+ void flashSort (std::vector<float >& arr) {
24
23
int n = arr.size ();
25
24
if (n <= 1 ) return ;
26
25
27
- // Step 1: Find the minimum and maximum values in the array
28
26
float minVal = *std::min_element (arr.begin (), arr.end ());
29
27
float maxVal = *std::max_element (arr.begin (), arr.end ());
28
+ if (minVal == maxVal) return ;
30
29
31
- // Step 2: Calculate the number of classes (buckets)
32
30
int numClasses = 0.45 * n;
33
31
if (numClasses < 2 ) numClasses = 2 ;
34
32
35
- // Step 3: Create a histogram of the data
36
33
std::vector<int > count (numClasses, 0 );
37
34
for (int i = 0 ; i < n; ++i) {
38
35
int index = numClasses * (arr[i] - minVal) / (maxVal - minVal);
36
+ if (index >= numClasses) index = numClasses - 1 ; // prevent overflow
39
37
count[index]++;
40
38
}
41
39
42
- // Step 4: Calculate the starting index of each class
43
40
std::vector<int > pos (numClasses, 0 );
44
41
pos[0 ] = 0 ;
45
42
for (int i = 1 ; i < numClasses; ++i) {
46
43
pos[i] = pos[i - 1 ] + count[i - 1 ];
47
44
}
48
45
49
- // Step 5: Rearrange the array into classes
50
46
std::vector<float > temp = arr;
51
47
for (int i = 0 ; i < n; ++i) {
52
48
int index = numClasses * (arr[i] - minVal) / (maxVal - minVal);
49
+ if (index >= numClasses) index = numClasses - 1 ;
53
50
arr[pos[index]++] = temp[i];
54
51
}
55
52
56
- // Step 6: Sort each class using insertion sort
57
53
for (int i = 0 ; i < numClasses; ++i) {
58
54
int start = pos[i] - count[i];
59
55
int end = pos[i];
60
56
std::sort (arr.begin () + start, arr.begin () + end);
61
57
}
62
58
}
63
59
64
- // Driver code to test the Flash Sort
65
- int main () {
66
- std::vector<float > arr = {0.897 , 0.565 , 0.656 , 0.1234 , 0.665 , 0.3434 };
67
- flashSort (arr);
60
+ /* *
61
+ * @brief Self-test implementations
62
+ */
63
+ static void test () {
64
+ // Test 1
65
+ std::vector<float > array1 = {0 .897f , 0 .565f , 0 .656f , 0 .1234f , 0 .665f , 0 .3434f };
66
+ std::cout << " Test 1... " ;
67
+ flashSort (array1);
68
+ assert (std::is_sorted (array1.begin (), array1.end ()));
69
+ std::cout << " passed" << std::endl;
68
70
69
- std::cout << " Sorted array is: " ;
70
- for (float num : arr) {
71
- std::cout << num << " " ;
72
- }
71
+ // Test 2
72
+ std::vector<float > array2 = {1 .2f , 3 .4f , 0 .5f , 2 .2f , 1 .1f };
73
+ std::cout << " Test 2... " ;
74
+ flashSort (array2);
75
+ assert (std::is_sorted (array2.begin (), array2.end ()));
76
+ std::cout << " passed" << std::endl;
77
+
78
+ // Test 3
79
+ std::vector<float > array3 = {5 .0f , 4 .0f , 3 .0f , 2 .0f , 1 .0f };
80
+ std::cout << " Test 3... " ;
81
+ flashSort (array3);
82
+ assert (std::is_sorted (array3.begin (), array3.end ()));
83
+ std::cout << " passed" << std::endl;
84
+
85
+ // Test 4
86
+ std::vector<float > array4 = {0 .1f , 0 .1f , 0 .1f , 0 .1f };
87
+ std::cout << " Test 4... " ;
88
+ flashSort (array4);
89
+ assert (std::is_sorted (array4.begin (), array4.end ()));
90
+ std::cout << " passed" << std::endl;
91
+ }
92
+
93
+ /* *
94
+ * @brief Main function
95
+ */
96
+ int main () {
97
+ test (); // execute the tests
73
98
return 0 ;
74
99
}
0 commit comments