Skip to content
Snippets Groups Projects
Commit 6a80405a authored by David Sehnal's avatar David Sehnal
Browse files

sorting fixes

parent fc4473e7
Branches
Tags
No related merge requests found
...@@ -84,9 +84,24 @@ function partitionArrayAsc(data: number[], parts: number[], l: number, r: number ...@@ -84,9 +84,24 @@ function partitionArrayAsc(data: number[], parts: number[], l: number, r: number
parts[1] = tail; parts[1] = tail;
} }
function insertionSort({ data, cmp, swap }: Ctx, start: number, end: number) {
for (let i = start + 1; i <= end; i++) {
let j = i - 1;
while (j >= start && cmp(data, j, j + 1) > 0) {
swap(data, j, j + 1);
j = j - 1;
}
}
}
function quickSort(ctx: Ctx, low: number, high: number) { function quickSort(ctx: Ctx, low: number, high: number) {
const { parts } = ctx; const { parts } = ctx;
while (low < high) { while (low < high) {
if (high - low < 16) {
insertionSort(ctx, low, high);
return;
}
partitionGeneric(ctx, low, high); partitionGeneric(ctx, low, high);
const li = parts[0], ri = parts[1]; const li = parts[0], ri = parts[1];
...@@ -100,11 +115,11 @@ function quickSort(ctx: Ctx, low: number, high: number) { ...@@ -100,11 +115,11 @@ function quickSort(ctx: Ctx, low: number, high: number) {
} }
} }
function insertionSort(data: number[], start: number, end: number) { function insertionSortArrayAsc(data: number[], start: number, end: number) {
for (let i = start + 1; i <= end; i++) { for (let i = start + 1; i <= end; i++) {
const key = data[i]; const key = data[i];
let j = i - 1; let j = i - 1;
while (j >= 0 && data[j] > key) { while (j >= start && data[j] > key) {
data[j + 1] = data[j]; data[j + 1] = data[j];
j = j - 1; j = j - 1;
} }
...@@ -115,7 +130,7 @@ function insertionSort(data: number[], start: number, end: number) { ...@@ -115,7 +130,7 @@ function insertionSort(data: number[], start: number, end: number) {
function quickSortArrayAsc(data: number[], parts: number[], low: number, high: number) { function quickSortArrayAsc(data: number[], parts: number[], low: number, high: number) {
while (low < high) { while (low < high) {
if (high - low < 16) { if (high - low < 16) {
insertionSort(data, low, high); insertionSortArrayAsc(data, low, high);
return; return;
} }
......
...@@ -50,7 +50,8 @@ function shuffleArray(data: any[]) { ...@@ -50,7 +50,8 @@ function shuffleArray(data: any[]) {
} }
describe('qsort-array asc', () => { describe('qsort-array asc', () => {
const data0 = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const data0 = new Array(50);
for (let i = 0; i < data0.length; i++) data0[i] = i;
const data1 = [1, 1, 2, 2, 3, 3, 4, 4, 4, 6, 6, 6]; const data1 = [1, 1, 2, 2, 3, 3, 4, 4, 4, 6, 6, 6];
function test(name: string, data: any[], randomize: boolean) { function test(name: string, data: any[], randomize: boolean) {
...@@ -72,7 +73,8 @@ describe('qsort-array asc', () => { ...@@ -72,7 +73,8 @@ describe('qsort-array asc', () => {
}) })
describe('qsort-array generic', () => { describe('qsort-array generic', () => {
const data0 = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const data0 = new Array(50);
for (let i = 0; i < data0.length; i++) data0[i] = i;
const data1 = [1, 1, 2, 2, 3, 3, 4, 4, 4, 6, 6, 6]; const data1 = [1, 1, 2, 2, 3, 3, 4, 4, 4, 6, 6, 6];
function test(name: string, data: any[], randomize: boolean) { function test(name: string, data: any[], randomize: boolean) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment