static void MaxHeapify(SortingArray array, int n, int i) { int largest = i; int l = 2 * i + 1; int r = 2 * i + 2; if (l < n) { if (array.IsBigger(l, largest)) { largest = l; } Updated(array); } if (r < n) { if (array.IsBigger(r, largest)) { largest = r; } Updated(array); } if (largest != i) { array.Swap(i, largest); Updated(array); MaxHeapify(array, n, largest); } }
public Drawer(SortingArray arrayToDraw, Graphics graph, RectangleF drawingArea) { NextFrameDrawings = new Dictionary <int, DrawingOptions>(); this.arrayToDraw = arrayToDraw; this.graph = graph; this.drawingArea = drawingArea; }
public static void ShellSort(SortingArray array) { int distance = array.Length / 2; while (distance > 1) { for (int i = distance; i < array.Length; i++) { int j = i; while (j >= 0) { Updated(array); j -= distance; if (array.IsBigger(j, j + distance)) { array.Swap(j, j + distance); } else { break; } } } distance = (distance + 1) / 2; } InsertionSort(array); }
public static void CocktailSort(SortingArray array) { int left = 0; int right = array.Length - 1; while (left < right) { for (int i = left; i < right; i++) { if (array.IsBigger(i, i + 1)) { array.Swap(i, i + 1); } Updated(array); } for (int i = right; i > left; i--) { if (array.IsSmaller(i, i - 1)) { array.Swap(i, i - 1); } Updated(array); } left++; right--; } }
public static void OddEvenSort(SortingArray array) { var sorted = false; while (!sorted) { sorted = true; for (var i = 1; i < array.Length - 1; i += 2) { if (array.IsBigger(i, i + 1)) { array.Swap(i, i + 1); sorted = false; } Updated(array); } for (var i = 0; i < array.Length - 1; i += 2) { if (array.IsBigger(i, i + 1)) { array.Swap(i, i + 1); sorted = false; } Updated(array); } } }
static void QuickSortRecursion(SortingArray array, int left, int right) { if (left < right) { int pivot = right; int l = left; int r = right; while (l < r) { if (array.IsBigger(l, pivot)) { while (l < r && array.IsBiggerOrEqual(r, pivot)) { r--; Updated(array); } array.Swap(l, r); } l++; Updated(array); } if (l > left) { QuickSortRecursion(array, left, l - 1); } if (r < right) { array.Swap(r, pivot); QuickSortRecursion(array, r, right); } } }
static void BitonicCompare(SortingArray array, int source, int destination, bool direction) { if (direction == array.IsBigger(source, destination)) { array.Swap(source, destination); } Updated(array); }
public static void GenerateReversed(SortingArray array) { for (int i = 0; i < array.Length; i++) { array.Modify(i, array.Length - i); Updated(array); } }
public static void GenerateRandomAscending(SortingArray array) { for (int i = 0; i < array.Length; i++) { StandardGenerator rand = new StandardGenerator(); int value = rand.Next(i, array.Length) + 1; array.Modify(i, value); Updated(array); } }
public static void GenerateRandom(SortingArray array) { for (int i = 0; i < array.Length; i++) { StandardGenerator rand = new StandardGenerator(); int val = rand.Next(1, array.Length + 1); array.Modify(i, val); Updated(array); } }
public static void Shuffle(SortingArray array) { for (int i = 0; i < array.Length; i++) { StandardGenerator rand = new StandardGenerator(); int pos = rand.Next(0, array.Length); array.Swap(i, pos); Updated(array); } }
public void StartAsync(SortingArray array, Algorithm algorithm) { if (!IsWorking) { currentAlgorithm = algorithm; currentArray = array; workThread = new Thread(DoWork); workThread.Start(); } }
static void BitonicSortRecursion(SortingArray array, int index, int length, bool direction) { if (length > 1) { int median = (length / 2); BitonicSortRecursion(array, index, median, true); BitonicSortRecursion(array, index + median, median, false); BitonicMerge(array, index, length, direction); } }
public static void MaxHeapSort(SortingArray array) { for (int i = array.Length / 2 - 1; i >= 0; i--) { MaxHeapify(array, array.Length, i); } for (int i = array.Length - 1; i >= 0; i--) { array.Swap(0, i); Updated(array); MaxHeapify(array, i, 0); } }
static void BitonicMerge(SortingArray array, int index, int length, bool direction) { if (length > 1) { int median = (length / 2); for (int i = index; i < index + median; i++) { BitonicCompare(array, i, i + median, direction); } BitonicMerge(array, index, median, direction); BitonicMerge(array, index + median, median, direction); } }
public static void BubbleSort(SortingArray array) { for (int i = array.Length - 1; i >= 0; i--) { for (int j = 0; j < i; j++) { if (array.IsBigger(j, j + 1)) { array.Swap(j, j + 1); } Updated(array); } } }
public static void GenerateCubic(SortingArray array) { double a = array.Length / 2; double n = 1 / (a * a); for (int x = 0; x < array.Length; x++) { double t = x - a; int y = Math.Min(array.Length, (int)(n * (t * t * t) + a) + 1); array.Modify(x, y); Updated(array); } Shuffle(array); }
public static void OptimizedCocktailSort(SortingArray array) { int left = 0; int right = array.Length - 1; while (left < right) { int nextLeft = right; int nextRight = left; bool sorted = true; for (int i = left; i < right; i++) { if (array.IsBigger(i, i + 1)) { array.Swap(i, i + 1); sorted = false; nextRight = i; } Updated(array); } if (sorted) { break; } right = nextRight; for (int i = right; i > left; i--) { if (array.IsSmaller(i, i - 1)) { array.Swap(i, i - 1); sorted = false; nextLeft = i; } Updated(array); } if (sorted) { break; } left = nextLeft; } }
public static void GenerateSortedAndShuffleNPercent(SortingArray array, int chance) { GenerateSorted(array); for (int i = 0; i < array.Length; i++) { StandardGenerator rand = new StandardGenerator(); int randNum = rand.Next(101); if (randNum <= chance) { int pos = rand.Next(array.Length); array.Swap(i, pos); Updated(array); } } }
public static void Generate8Blocked(SortingArray array) { double d = array.Length / 8.0; double val = d; for (int i = 0; i < array.Length; i++) { if (i >= val) { val += d; } array.Modify(i, (int)val); Updated(array); } Shuffle(array); }
public static void SelectionSort(SortingArray array) { for (int i = array.Length - 1; i > 0; i--) { int biggest = 0; for (int j = 0; j <= i; j++) { if (array.IsBiggerOrEqual(j, biggest)) { biggest = j; } Updated(array); } array.Swap(biggest, i); Updated(array); } }
public static Color GetColor(SortingArray array, int pos, DrawingColor color) { switch (color) { case DrawingColor.Default: return(GetDefaultColor(array, pos)); case DrawingColor.Selected: return(GetSelectedColor(array, pos)); case DrawingColor.Swapped: return(GetSwappedColor(array, pos)); case DrawingColor.Special: return(GetSpecialColor(array, pos)); } return(Color.WhiteSmoke); }
public static void InsertionSort(SortingArray array) { for (int i = 1; i < array.Length; i++) { for (int j = i; j > 0; j--) { if (array.IsBigger(j - 1, j)) { array.Swap(j - 1, j); } else { Updated(array); break; } Updated(array); } } }
public static void CombSort(SortingArray array) { int distance = (int)(array.Length / 1.3); while (distance > 1) { for (int i = 0; i < array.Length - distance; i++) { if (array.IsBigger(i, i + distance)) { array.Swap(i, i + distance); } Updated(array); } distance = (int)(distance / 1.3); } OptimizedBubbleSort(array); }
public static void OptimizedBubbleSort(SortingArray array) { for (int i = array.Length - 1; i >= 0; i--) { bool sorted = true; for (int j = 0; j < i; j++) { if (array.IsBigger(j, j + 1)) { array.Swap(j, j + 1); sorted = false; } Updated(array); } if (sorted) { break; } } }
static void MergeSortRecursion(SortingArray array, int[] temp, int left, int right) { if (left < right) { int len = right - left + 1; int mid = left + len / 2; MergeSortRecursion(array, temp, left, mid - 1); MergeSortRecursion(array, temp, mid, right); int p1 = left; int p2 = mid; for (int i = 0; i < len; i++) { bool first = true; if ((p1 >= mid) | (p2 <= right && array.IsBigger(p1, p2))) { first = false; } if (first) { temp[i] = array.Get(p1); p1++; } else { temp[i] = array.Get(p2); p2++; } Updated(array); } for (int i = 0; i < len; i++) { array.Modify(left + i, temp[i]); Updated(array); } } }
static Color GetSpecialColor(SortingArray array, int pos) { switch (style) { case DrawingStyle.Default: return(DefaultSpecialColor); case DrawingStyle.Rainbow: return(RainbowSpecialColor(array.Get(pos), array.Length - 1)); case DrawingStyle.DistanceBased: return(DistanceSpecialColor()); case DrawingStyle.Monochrome: return(Color.White); case DrawingStyle.Custom: return(CustomSpecialColor); default: return(Color.Gray); } }
static Color GetDefaultColor(SortingArray array, int pos) { switch (style) { case DrawingStyle.Default: return(DefaultDefaultColor); case DrawingStyle.Rainbow: return(RainbowDefaultColor(array.Get(pos), array.Length - 1)); case DrawingStyle.DistanceBased: return(DistanceDefaultColor(array.DistanceToSortedPosition(pos), array.Length)); case DrawingStyle.Monochrome: return(Color.LightSlateGray); case DrawingStyle.Custom: return(CustomDefaultColor); default: return(Color.WhiteSmoke); } }
static Color GetSelectedColor(SortingArray array, int pos) { switch (style) { case DrawingStyle.Default: return(DefaultSelectedColor); case DrawingStyle.Rainbow: return(RainbowSelectedColor(array.Get(pos), array.Length - 1)); case DrawingStyle.DistanceBased: return(DistanceSelectedColor(array.DistanceToSortedPosition(pos), array.Length)); case DrawingStyle.Monochrome: return(Color.LightGray); case DrawingStyle.Custom: return(CustomSelectedColor); default: return(Color.LightGray); } }
static Color GetSwappedColor(SortingArray array, int pos) { switch (style) { case DrawingStyle.Default: return(DefaultSwappedColor); case DrawingStyle.Rainbow: return(RainbowSwappedColor()); case DrawingStyle.DistanceBased: return(DistanceSwappedColor()); case DrawingStyle.Monochrome: return(Color.White); case DrawingStyle.Custom: return(CustomSwappedColor); default: return(Color.LightGray); } }