mirror of
https://github.com/aicodix/dsp.git
synced 2026-04-27 14:30:36 +00:00
we ain't come this far to omit sorting
This commit is contained in:
parent
c86f767cac
commit
cf60e38b9d
5 changed files with 57 additions and 4 deletions
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Quick select algorithm
|
||||
Quick algorithms for sorting and selecting
|
||||
|
||||
Copyright 2024 Ahmet Inan <inan@aicodix.de>
|
||||
*/
|
||||
|
|
@ -57,6 +57,17 @@ static void partition(TYPE *a, int &l, int &h)
|
|||
++i;
|
||||
}
|
||||
|
||||
template <typename TYPE>
|
||||
static void sort(TYPE *a, int l, int h)
|
||||
{
|
||||
if (l < h) {
|
||||
int lt = l, gt = h;
|
||||
partition(a, lt, gt);
|
||||
sort(a, l, lt - 1);
|
||||
sort(a, gt + 1, h);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename TYPE>
|
||||
static void select(TYPE *a, int l, int h, int k)
|
||||
{
|
||||
|
|
@ -74,6 +85,12 @@ static void select(TYPE *a, int l, int h, int k)
|
|||
|
||||
}
|
||||
|
||||
template <typename TYPE>
|
||||
void quick_sort(TYPE *a, int n)
|
||||
{
|
||||
QUICK::sort(a, 0, n-1);
|
||||
}
|
||||
|
||||
template <typename TYPE>
|
||||
TYPE quick_select(TYPE *a, int k, int n)
|
||||
{
|
||||
|
|
@ -6,7 +6,7 @@ Copyright 2021 Ahmet Inan <inan@aicodix.de>
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "quick_select.hh"
|
||||
#include "quick.hh"
|
||||
|
||||
namespace DSP {
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ Copyright 2024 Ahmet Inan <inan@aicodix.de>
|
|||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include "quick_select.hh"
|
||||
#include "quick.hh"
|
||||
|
||||
int main()
|
||||
{
|
||||
|
|
|
|||
36
tests/quick_sort_test.cc
Normal file
36
tests/quick_sort_test.cc
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
Test for the Quick sort algorithm
|
||||
|
||||
Copyright 2024 Ahmet Inan <inan@aicodix.de>
|
||||
*/
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include "quick.hh"
|
||||
|
||||
int main()
|
||||
{
|
||||
const int MAX_N = 1 << 16;
|
||||
unsigned seed = 42;
|
||||
if (1) {
|
||||
std::random_device rd;
|
||||
seed = rd();
|
||||
}
|
||||
typedef std::default_random_engine generator;
|
||||
typedef std::uniform_int_distribution<int> distribution;
|
||||
auto rand = std::bind(distribution(0, MAX_N), generator(seed));
|
||||
int a[MAX_N];
|
||||
for (int loop = 0; loop < 1000; ++loop) {
|
||||
int size = rand();
|
||||
for (int i = 0; i < size; ++i)
|
||||
a[i] = rand();
|
||||
DSP::quick_sort(a, size);
|
||||
for (int i = 1; i < size; ++i)
|
||||
assert(a[i-1] <= a[i]);
|
||||
}
|
||||
std::cerr << "Quick sort algorithm test passed!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -6,7 +6,7 @@ Copyright 2021 Ahmet Inan <inan@aicodix.de>
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "quick_select.hh"
|
||||
#include "quick.hh"
|
||||
|
||||
namespace DSP {
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue