mirror of
https://github.com/aicodix/code.git
synced 2026-04-27 14:30:36 +00:00
added insertion sort and regression test
This commit is contained in:
parent
6372a965cf
commit
524571e6fb
2 changed files with 73 additions and 0 deletions
34
sort.hh
Normal file
34
sort.hh
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
Some stable sorting algorithms
|
||||
|
||||
Copyright 2024 Ahmet Inan <inan@aicodix.de>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
template <typename TYPE>
|
||||
static void insertion_sort(TYPE *a, int n)
|
||||
{
|
||||
for (int i = 1, j; i < n; ++i) {
|
||||
TYPE t = a[i];
|
||||
for (j = i; j > 0 && a[j-1] > t; --j)
|
||||
a[j] = a[j-1];
|
||||
a[j] = t;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename TYPE>
|
||||
static void insertion_sort(int *p, TYPE *a, int n)
|
||||
{
|
||||
p[0] = 0;
|
||||
for (int i = 1, j; i < n; ++i) {
|
||||
TYPE t = a[i];
|
||||
for (j = i; j > 0 && a[j-1] > t; --j) {
|
||||
a[j] = a[j-1];
|
||||
p[j] = p[j-1];
|
||||
}
|
||||
a[j] = t;
|
||||
p[j] = i;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue