Skip to content

Commit 06d8bd2

Browse files
committed
- Added first Hybrid sorting algorithm: Introsort. Required rework to allow Insertion/Heap Sort objects to be used on sections of the array as part of Introsort.
- Rework of most Sort Classes to accommodate changes to pointer names/functions - The lower/upper bounds of each sort can now be set (allowing them to be used in other classes e.g. hybrid sorts). - Added Binary Intro Sort. which uses a Binary Insertion rather than normal Insertion
1 parent 1e64876 commit 06d8bd2

18 files changed

+463
-276
lines changed

Sorting Algorithms/BinaryInsertion.cs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace Sorting_Algorithms
1+
namespace Sorting_Algorithms
82
{
93
class BinaryInsertion : InsertionSort
104
{
115
private int IndexToInsert;
126

137
public BinaryInsertion(double[] array) : base(array)
148
{
15-
//if this breaks, use SortI = 0, and IndexToInsert = SortI;
16-
SortI = 1;
17-
SortJ = 0;
18-
IndexToInsert = BinarySearchForSpace(SortI, 0, SortI);
9+
}
10+
11+
public override void setBounds(int min, int max)
12+
{
13+
base.setBounds(min, max);
14+
SortI = min;
15+
SortJ = SortI + 1;
16+
IndexToInsert = BinarySearchForSpace(SortI, min, SortI);
1917
}
2018

2119
public override int getK()
@@ -40,50 +38,48 @@ public override double[] Run()
4038
IndexToInsert = SortI;
4139
}
4240
}
43-
else if (++SortI < array.Length)
41+
else if (++SortI <= max)
4442
{
45-
IndexToInsert = BinarySearchForSpace(SortI, 0, SortI);
43+
IndexToInsert = BinarySearchForSpace(SortI, min, SortI);
4644
SortJ = SortI - 1;
4745
}
4846

49-
isFinished = (SortI >= array.Length);
47+
isFinished = SortI > max;
5048

5149
return array;
5250
}
5351

5452
public override double[] QuickRun()
5553
{
56-
IndexToInsert = BinarySearchForSpace(SortI, 0, SortI);
54+
IndexToInsert = BinarySearchForSpace(SortI, min, SortI);
5755

5856
for (SortJ = SortI - 1; SortJ > IndexToInsert - 1; SortJ--)
5957
{
6058
Swap(SortJ, SortJ + 1);
6159
}
6260

6361
SortI++;
64-
isFinished = (SortI >= array.Length);
62+
isFinished = SortI > max;
6563

6664
return array;
6765
}
6866

69-
private int BinarySearchForSpace(int toInsert, int min, int max)
67+
private int BinarySearchForSpace(int toInsert, int low, int high)
7068
{
7169
int mid;
7270

7371
///applies binary search to find index where element should be located
74-
while (min < max)
72+
while (low < high)
7573
{
76-
mid = min + (max - min) / 2;
74+
mid = low + (high - low) / 2;
7775

78-
if (array[toInsert] >= array[mid])
79-
min = mid + 1;
76+
if (Compare(toInsert, mid) >= 0)
77+
low = mid + 1;
8078
else
81-
max = mid;
82-
83-
comparisonCount++;
79+
high = mid;
8480
}
8581

86-
return min;
82+
return low;
8783
}
8884
}
8985
}

Sorting Algorithms/BubbleSort.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ public BubbleSort(double[] array) : base(array) { }
66

77
public override double[] Run()
88
{
9-
if (SortJ + 1 < array.Length - SortI)
9+
if (SortJ + 1 <= max - SortI)
1010
{
11-
if (array[SortJ] > array[SortJ + 1])
11+
if (Compare(SortJ, SortJ + 1) > 0)
1212
{
1313
Swap(SortJ, SortJ + 1);
1414
swappedThisCycle = true;
1515
}
1616

17-
comparisonCount++;
1817
SortJ++;
1918
}
2019
else
@@ -24,11 +23,11 @@ public override double[] Run()
2423
SortI = array.Length;
2524

2625
swappedThisCycle = false;
27-
SortJ = 0;
26+
SortJ = min;
2827
SortI++;
2928
}
3029

31-
isFinished = (SortI >= array.Length);
30+
isFinished = SortI > max;
3231

3332
return array;
3433
}
@@ -37,19 +36,17 @@ public override double[] QuickRun()
3736
{
3837
swappedThisCycle = false;
3938

40-
for (SortJ = 0; SortJ + 1 < array.Length - SortI; SortJ++)
39+
for (SortJ = 0; SortJ + 1 <= max - SortI; SortJ++)
4140
{
42-
if (array[SortJ] > array[SortJ + 1])
41+
if (Compare(SortJ, SortJ+1) > 0)
4342
{
4443
Swap(SortJ, SortJ + 1);
4544
swappedThisCycle = true;
4645
}
47-
48-
comparisonCount++;
4946
}
5047

5148
SortI++;
52-
isFinished = !swappedThisCycle || SortI >= array.Length;
49+
isFinished = !swappedThisCycle || SortI > max;
5350
// early exit if no swaps performed on a pass
5451

5552
return array;

Sorting Algorithms/CocktailShakerSort.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
class CocktailShakerSort : Sort
44
{
5-
protected int gap;
5+
protected int gap; // the gap between the elements currently being compared
66

77
public CocktailShakerSort(double[] array) : base(array)
88
{
@@ -11,21 +11,20 @@ public CocktailShakerSort(double[] array) : base(array)
1111

1212
public override double[] Run()
1313
{
14-
if (SortJ + gap < array.Length - (SortI) && SortJ + gap >= SortI)
14+
if (SortJ+gap <= max-SortI && SortJ+gap >= SortI)
1515
{
16-
if (gap * array[SortJ] > gap * array[SortJ + gap])
16+
if (Compare(SortJ, SortJ+gap) > 0)
1717
{
1818
Swap(SortJ, SortJ + gap);
1919
swappedThisCycle = true;
2020
}
2121

22-
comparisonCount++;
2322
SortJ += gap;
2423
}
25-
else if (SortI < array.Length - 1)
24+
else if (SortI < max)
2625
{
2726
if (!swappedThisCycle)
28-
SortI = array.Length;
27+
SortI = max + 1;
2928

3029
gap = -gap;
3130
swappedThisCycle = false;
@@ -45,9 +44,9 @@ public override double[] QuickRun()
4544
{
4645
swappedThisCycle = false;
4746

48-
for (; SortJ + gap < array.Length - SortI && SortJ + gap >= SortI; SortJ += gap)
47+
for (; SortJ + gap <= max - SortI && SortJ + gap >= SortI; SortJ += gap)
4948
{
50-
if (gap * array[SortJ] > gap * array[SortJ + gap])
49+
if (Compare(SortJ, SortJ+gap) > 0)
5150
{
5251
Swap(SortJ, SortJ + gap);
5352
swappedThisCycle = true;
@@ -58,12 +57,20 @@ public override double[] QuickRun()
5857

5958
gap = -gap;
6059

61-
if (gap > 0)
60+
if (gap > min)
6261
SortI++;
6362

64-
isFinished = !swappedThisCycle || SortI >= array.Length - 1;
63+
isFinished = !swappedThisCycle || SortI >= max;
6564

6665
return array;
6766
}
67+
68+
protected override int Compare(int a, int b)
69+
{
70+
// when moving up the list, gap > 0
71+
// when moving down the list, gap < 0
72+
// multiplying by gap, ensures the elements are compared correctly
73+
return base.Compare(a,b) * gap;
74+
}
6875
}
6976
}

Sorting Algorithms/CombSort.cs

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,46 @@
22
{
33
class CombSort : Sort
44
{
5-
private int maxIndex;
6-
private int gap;
7-
private const double comb_K = 1.3;
5+
private int gap; // gap between the elements being compared
6+
private const double comb_K = 1.3; // constant factor used to adjust gap
87

98
public CombSort(double[] array) : base(array)
109
{
11-
maxIndex = array.Length;
12-
gap = (int)(array.Length / comb_K);
10+
gap = (int)(max + 1 / comb_K);
1311
}
1412

1513
public override double[] Run()
1614
{
17-
if (SortJ + gap < maxIndex)
15+
if (SortJ + gap <= max)
1816
{
19-
if (array[SortJ] > array[SortJ + gap])
17+
if (Compare(SortJ, SortJ+gap) > 0)
2018
{
2119
Swap(SortJ, SortJ + gap);
2220
swappedThisCycle = true;
2321
}
2422

25-
comparisonCount++;
2623
SortJ += gap;
2724
}
28-
else if (SortI < array.Length)
25+
else if (SortI <= max)
2926
{
3027
if (gap > comb_K)
3128
{
3229
gap = (int)(gap / comb_K);
33-
SortI = 0;
30+
SortI = min;
3431
}
3532
else if (!swappedThisCycle)
36-
{
37-
SortI = array.Length;
38-
}
33+
SortI = max + 1;
3934
else
4035
{
41-
maxIndex = SortJ;
36+
max = SortJ - 1;
4237
SortI++;
4338
}
4439

4540
swappedThisCycle = false;
46-
SortJ = 0;
41+
SortJ = min;
4742
}
4843
else
49-
{
5044
isFinished = true;
51-
}
5245

5346
return array;
5447
}
@@ -57,33 +50,29 @@ public override double[] QuickRun()
5750
{
5851
swappedThisCycle = false;
5952

60-
for (SortJ = 0; SortJ + gap < maxIndex; SortJ++)
53+
for (SortJ = 0; SortJ + gap <= max; SortJ++)
6154
{
62-
if (array[SortJ] > array[SortJ + gap])
55+
if (Compare(SortJ, SortJ+gap) > 0)
6356
{
64-
Swap(SortJ, SortJ + gap);
57+
Swap(SortJ, SortJ+gap);
6558
swappedThisCycle = true;
6659
}
67-
68-
comparisonCount++;
6960
}
7061

7162
if (gap > comb_K)
7263
{
7364
gap = (int)(gap / comb_K);
74-
SortI = 0;
65+
SortI = min;
7566
}
7667
else if (!swappedThisCycle)
77-
{
78-
SortI = array.Length;
79-
}
68+
SortI = max + 1;
8069
else
8170
{
82-
maxIndex = SortJ;
71+
max = SortJ - 1;
8372
SortI++;
8473
}
8574

86-
isFinished = !swappedThisCycle || SortI >= array.Length;
75+
isFinished = !swappedThisCycle || SortI > max;
8776

8877
return array;
8978
}

Sorting Algorithms/CycleSort.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CycleSort : Sort
1313
public CycleSort(double[] array) : base(array)
1414
{
1515
SortJ = SortI + 1;
16-
pos = 0;
16+
pos = min;
1717
}
1818

1919
public override int getK()
@@ -25,17 +25,15 @@ public override double[] QuickRun()
2525
{
2626
pos = SortI;
2727

28-
for (int i = SortI + 1; i < array.Length; i++)
28+
for (int i = SortI + 1; i <= max; i++)
2929
{
30-
comparisonCount++;
31-
if (array[i] < array[SortI])
30+
if (Compare(i, SortI) < 0)
3231
pos++;
3332
}
3433

3534
if (pos != SortI)
3635
{
37-
comparisonCount++;
38-
while (array[SortI] == array[pos])
36+
while (Compare(SortI, pos) == 0)
3937
pos++;
4038

4139
Swap(pos, SortI);
@@ -44,25 +42,23 @@ public override double[] QuickRun()
4442
if (pos == SortI)
4543
SortI++;
4644

47-
isFinished = (SortI >= array.Length - 1);
45+
isFinished = (SortI >= max);
4846

4947
return array;
5048
}
5149

5250
public override double[] Run()
5351
{
54-
if (SortJ < array.Length)
52+
if (SortJ <= max)
5553
{
56-
comparisonCount++;
57-
if (array[SortJ] < array[SortI])
54+
if (Compare(SortJ, SortI) < 0)
5855
pos++;
5956
SortJ++;
6057
}
6158
else
6259
{
63-
while (pos != SortI && array[SortI] == array[pos])
60+
while (pos != SortI && Compare(SortI, pos) == 0)
6461
{
65-
comparisonCount++;
6662
pos++;
6763
}
6864

@@ -75,7 +71,7 @@ public override double[] Run()
7571
pos = SortI;
7672
}
7773

78-
isFinished = (SortI >= array.Length - 1);
74+
isFinished = (SortI >= max);
7975

8076
return array;
8177
}

0 commit comments

Comments
 (0)