private static void Heapify(float[] array, int index) { //The index of the left value in the heap is assigned to be twice the value of the index of the array int left = 2 * index; //The index of the right value in the heap is set to be one greater than the left value int right = 2 * index + 1; //The maximum possible index in the heap is the current index passed to the function from the array int max = index; iterations++; /*If the left index is less than the size of the heap, and the value at this index is less than the vaue at the * current index, the maximuim index becomes the left index */ if (left <= heapSize && array[left] > array[index]) { max = left; } /*If the right index is less than the size of the heap, and the value at this index is less than the vaue at the * current index, the maximuim index becomes the right index */ if (right <= heapSize && array[right] > array[max]) { max = right; } //If the maximum possible index is less than the current value of the index, swap the items at the two indexes in the heap, and rebalance the heap if (max != index) { GenericSortingAlgorithms.Swap(array, index, max); Heapify(array, max); } }
//Reverses a sorted array so that it is in descending order public static float[] GetInDescendingOrder(float[] stockArray) { //All values that are equidistance from the centre of the array are swapped, leading to the creation of a reversed array for (int i = 0; i < stockArray.Length / 2; i++) { GenericSortingAlgorithms.Swap(stockArray, i, stockArray.Length - i - 1); } //Return the reversed array return(stockArray); }
public static float[] HeapSort(float[] array) { //Starts the algorithm by creating a heap from the unsorted dataset BuildHeap(array); //Swaps the greatest and second-greatest values in the array, and inserts them into the heap. The heap is then re-balanced for (int i = array.Length - 1; i >= 0; i--) { GenericSortingAlgorithms.Swap(array, 0, i); //The size of the heap is decremented before the heap is re-balanced heapSize--; Heapify(array, 0); } //The sorted array is returned return(array); }
//Sorts the stock data using the bubble sort algorithm public static float[] Sort(float[] array) { for (int i = 0; i < array.Length; i++) { for (int j = 0; j < array.Length - 1; j++) { iterations++; //If the left-most value is less than the right-most, swap the two in the array if (array[j] > array[j + 1]) { GenericSortingAlgorithms.Swap(array, j, j + 1); } } } //Return the sorted array return(array); }
public static int QuickSortPivot(float[] array, int low, int high) { //The pivot is always assumed to be the rightmost element of the array int startOfArray = low; float pivot = array[high]; //Each element of a partition is compared to the adjacent value, and are swapped if they are out of order for (int i = low; i < high; i++) { if (array[i] < pivot) { GenericSortingAlgorithms.Swap(array, i, startOfArray); startOfArray++; } } //The highest value and the lowest value of the partition are swapped (the pivot is moved) GenericSortingAlgorithms.Swap(array, startOfArray, high); return(startOfArray); }