/* _____________________________Selection Sort____________________________________ */ // Smallest goes at the first position private static void SelectionSort() { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (InitialArray[i] > InitialArray[j]) { ArrayHandler.SwapArrayElements(InitialArray, i, j); } } ArrayHandler.PrintArray(InitialArray); } }
/* _____________________________Bubble Sort____________________________________ */ // Largest goes at the last position private static void BubbleSort() { for (int i = n - 1; i > 0; i--) { for (int j = i - 1; j >= 0; j--) { if (InitialArray[i] < InitialArray[j]) { ArrayHandler.SwapArrayElements(InitialArray, i, j); } } ArrayHandler.PrintArray(InitialArray); } }
private static int Partition(int low, int high) { int pivot = InitialArray[high]; // pivot int i = low - 1; // Index of smaller element for (int j = low; j <= high - 1; j++) { // If current element is smaller than the pivot if (InitialArray[j] < pivot) { i++; // increment index of smaller element ArrayHandler.SwapArrayElements(InitialArray, i, j); } } ArrayHandler.SwapArrayElements(InitialArray, i + 1, high); return(i + 1); }