/// <summary> /// Basic bubble sorting function /// </summary> /// <param name="array"></param> /// <returns></returns> public static void BubbleSort2(MyArray array) { array = array.Clone(); int counter = 0; for (int i = array.UpperIndex; i >= 0; i--) { bool hasSwap = false; for (int j = 0; j < i; j++) { if (array[j] > array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; hasSwap = true; } //Console.Write(" Inner loop: "); //array.DisplayArray(); counter++; } Console.Write("Outer loop: "); array.DisplayArray(); if (!hasSwap) { break; } } Console.WriteLine("Counter: " + counter.ToString()); }
private static void SelectionSort1(MyArray array) { int minIndex = 0; int counter = 0; for (int i = 0; i < array.ArraySize; i++) { minIndex = i; for (int j = i + 1; j < array.ArraySize; j++) { if (array[j] < array[minIndex]) { minIndex = j; } counter++; } if (minIndex != i) { int temp = array[minIndex]; array[minIndex] = array[i]; array[i] = temp; } Console.Write("Outer loop: "); array.DisplayArray(); } }
/// <summary> /// Basic bubble sorting function /// </summary> /// <param name="array"></param> /// <returns></returns> public static void BubbleSort1(MyArray array) { int counter = 0; array = array.Clone(); for (int i = array.UpperIndex; i >= 0; i--) { for (int j = 0; j < i; j++) { if (array[j] > array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } //Console.Write(" Inner loop: " ); //array.DisplayArray(); counter++; } Console.Write("Outer loop: "); array.DisplayArray(); } Console.WriteLine("Counter: " + counter.ToString()); }
public static void SelectionSortTest() { Console.WriteLine("Selection sort"); MyArray array = new MyArray(10); array.DisplayArray(); SelectionSort1(array); }
/// <summary> /// Partition1 will use first element as key value and compare with others /// Will have 2 cursors first and last to split the array /// </summary> /// <param name="array"></param> /// <returns>the split index</returns> private static int Partition1(MyArray array, int startIndex, int endIndex) { int first = startIndex + 1; int last = endIndex; do { do { if (array[first] > array[startIndex]) { break; } first++; }while (first <= last); if (first > last) { break; } do { if (array[last] < array[startIndex]) { break; } last--; }while (last >= first && last > 0); if (first > last) { break; } if (last > first) { int temp = array[last]; array[last] = array[first]; array[first] = temp; } }while (last >= first && last > 0); //swap array[startIndex] and last if (last != startIndex) { int temp = array[last]; array[last] = array[startIndex]; array[startIndex] = temp; } return(last); }
public static void BubbleTest() { MyArray array = new MyArray(10); array = new MyArray(new int[] { 1, 2, 3, 4, 5 }); array.DisplayArray(); Console.WriteLine(); Console.WriteLine("Bubble sort"); BubbleSort.BubbleSort1(array); BubbleSort.BubbleSort2(array); }
public static void QuickSortTest() { MyArray array = new MyArray(6); array = new MyArray(new int[] { 5, 4, 3, 2, 1 }); array.DisplayArray(); Console.WriteLine("Quick Sort"); QuickSort2(array, 0, array.UpperIndex); array.DisplayArray(); }
private static int Partition2(MyArray array, int startIndex, int endIndex) { if (array == null || array.ArraySize == 0 || endIndex <= startIndex) { return(endIndex); } int keyValueIndex = random.Next(startIndex, endIndex); //swap keyvalue and endindex if (keyValueIndex != endIndex) { int temp = array[keyValueIndex]; array[keyValueIndex] = array[endIndex]; array[endIndex] = temp; } int storedIndex = startIndex; for (int i = startIndex; i < endIndex; i++) { if (array[i] < array[endIndex]) { int temp = array[i]; array[i] = array[storedIndex]; array[storedIndex] = temp; storedIndex++; } } //swap key value and stroed index element if (storedIndex != endIndex) { int temp = array[storedIndex]; array[storedIndex] = array[endIndex]; array[endIndex] = temp; } return(storedIndex); }
private static void QuickSort1(MyArray array, int startIndex, int endIndex) { if (startIndex >= endIndex) { return; } int splitIndex = Partition1(array, startIndex, endIndex); Console.WriteLine("split index: " + splitIndex); Console.Write("first part: "); array.DisplayArray(startIndex, splitIndex - 1); Console.Write("last part: "); array.DisplayArray(splitIndex + 1, endIndex); Console.Write("Full array: "); array.DisplayArray(); //Console.Read(); QuickSort1(array, 0, splitIndex - 1); QuickSort1(array, splitIndex + 1, endIndex); }
public MyArray Clone() { MyArray newArray = new MyArray(this.array); return(newArray); }