-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
PancakeSorter.cs
77 lines (69 loc) · 2.09 KB
/
PancakeSorter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System.Collections.Generic;
namespace Algorithms.Sorters.Comparison;
/// <summary>
/// Class that implements pancake sort algorithm.
/// </summary>
/// <typeparam name="T">Type of array element.</typeparam>
public class PancakeSorter<T> : IComparisonSorter<T>
{
/// <summary>
/// Sorts array using specified comparer,
/// internal, in-place, stable,
/// time complexity: O(n^2),
/// space complexity: O(1),
/// where n - array length.
/// </summary>
/// <param name="array">Array to sort.</param>
/// <param name="comparer">Compares elements.</param>
public void Sort(T[] array, IComparer<T> comparer)
{
var n = array.Length;
// Start from the complete array and one by one
// reduce current size by one
for (var currSize = n; currSize > 1; --currSize)
{
// Find index of the maximum element in
// array[0..curr_size-1]
var mi = FindMax(array, currSize, comparer);
// Move the maximum element to end of current array
// if it's not already at the end
if (mi != currSize - 1)
{
// To move to the end, first move maximum
// number to beginning
Flip(array, mi);
// Now move the maximum number to end by
// reversing current array
Flip(array, currSize - 1);
}
}
}
// Reverses array[0..i]
private void Flip(T[] array, int i)
{
T temp;
var start = 0;
while (start < i)
{
temp = array[start];
array[start] = array[i];
array[i] = temp;
start++;
i--;
}
}
// Returns index of the maximum element
// in array[0..n-1]
private int FindMax(T[] array, int n, IComparer<T> comparer)
{
var mi = 0;
for (var i = 0; i < n; i++)
{
if (comparer.Compare(array[i], array[mi]) == 1)
{
mi = i;
}
}
return mi;
}
}