Skip to content

Commit 42deaf1

Browse files
committed
chore-flash_sort
1 parent 7a41a30 commit 42deaf1

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

sorting/flash_sort.cpp

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file
3-
* @brief Implementation of the Flash Sort algorithm.
3+
* @brief Implementation of the Flash Sort algorithm with self-tests.
44
* @details
55
* Flash Sort is an in-place, linear-time sorting algorithm for uniformly distributed data.
66
* It divides the data into classes, rearranges elements into their respective classes,
@@ -10,65 +10,90 @@
1010
* @author [Sachin Pangal](https://github.com/orthodox-64)
1111
*/
1212

13+
#include <algorithm>
14+
#include <cassert>
1315
#include <iostream>
1416
#include <vector>
15-
#include <algorithm>
1617

1718
/**
1819
* @brief Sorts an array using the Flash Sort algorithm.
1920
* @param arr The array to be sorted.
20-
* @param n The number of elements in the array.
2121
*/
22-
23-
void flashSort(std::vector<float>& arr) {
22+
void flashSort(std::vector<float>& arr) {
2423
int n = arr.size();
2524
if (n <= 1) return;
2625

27-
// Step 1: Find the minimum and maximum values in the array
2826
float minVal = *std::min_element(arr.begin(), arr.end());
2927
float maxVal = *std::max_element(arr.begin(), arr.end());
28+
if (minVal == maxVal) return;
3029

31-
// Step 2: Calculate the number of classes (buckets)
3230
int numClasses = 0.45 * n;
3331
if (numClasses < 2) numClasses = 2;
3432

35-
// Step 3: Create a histogram of the data
3633
std::vector<int> count(numClasses, 0);
3734
for (int i = 0; i < n; ++i) {
3835
int index = numClasses * (arr[i] - minVal) / (maxVal - minVal);
36+
if (index >= numClasses) index = numClasses - 1; // prevent overflow
3937
count[index]++;
4038
}
4139

42-
// Step 4: Calculate the starting index of each class
4340
std::vector<int> pos(numClasses, 0);
4441
pos[0] = 0;
4542
for (int i = 1; i < numClasses; ++i) {
4643
pos[i] = pos[i - 1] + count[i - 1];
4744
}
4845

49-
// Step 5: Rearrange the array into classes
5046
std::vector<float> temp = arr;
5147
for (int i = 0; i < n; ++i) {
5248
int index = numClasses * (arr[i] - minVal) / (maxVal - minVal);
49+
if (index >= numClasses) index = numClasses - 1;
5350
arr[pos[index]++] = temp[i];
5451
}
5552

56-
// Step 6: Sort each class using insertion sort
5753
for (int i = 0; i < numClasses; ++i) {
5854
int start = pos[i] - count[i];
5955
int end = pos[i];
6056
std::sort(arr.begin() + start, arr.begin() + end);
6157
}
6258
}
6359

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;
6870

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
7398
return 0;
7499
}

0 commit comments

Comments
 (0)