From dce5f89e9a6f9d60accecaeef1e5118f850b528a Mon Sep 17 00:00:00 2001 From: Crystal Secaira <123703060+csecaira@users.noreply.github.com> Date: Sun, 20 Oct 2024 04:45:24 -0400 Subject: [PATCH 1/2] Added quick-sort.java --- code/languages/Java/quick-sort.java | 73 +++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 code/languages/Java/quick-sort.java diff --git a/code/languages/Java/quick-sort.java b/code/languages/Java/quick-sort.java new file mode 100644 index 0000000000..bdd57c6d22 --- /dev/null +++ b/code/languages/Java/quick-sort.java @@ -0,0 +1,73 @@ +class quick_sort { + public static void quickSort(int[] array, int startIndex, int endIndex) { + if (startIndex < endIndex) { + int pivot = partition(array, startIndex, endIndex); + quickSort(array, startIndex, pivot - 1); + quickSort(array, pivot + 1, endIndex); + } + } + + public static int partition (int[] array, int startIndex, int endIndex) { + int pivot = array[endIndex]; + int left = startIndex; + int right = endIndex - 1; + + while (left <= right) { + if (array[left] <= pivot){ + left++; + } else if (array[right] >= pivot){ + right--; + } else{ + swap(array, left, right); + } + } + swap(array, left, endIndex); + return left; + } + + public static void swap(int[] array, int indexOne, int indexTwo){ + int temp = array[indexOne]; + array[indexOne] = array[indexTwo]; + array[indexTwo] = temp; + } + + public static void testingInts() { + /* This function tests quick sort on an array of 10 + non repeating integers */ + int[] intArray = {2, 50, 10, 31, 3, 7, 8, 1, 4, 98}; + String beforeSort = ""; + String afterSort = ""; + for (int i = 0; i < intArray.length; i++) { + beforeSort = beforeSort + " " + intArray[i]; + } + System.out.println("Before:" + beforeSort); + quickSort(intArray, 0, 9); + for (int i = 0; i < intArray.length; i++) { + afterSort = afterSort + " " + intArray[i]; + } + System.out.println("After:" + afterSort); + } + + public static void testingRepeats() { + /* This function tests quick sort on an array of 10 + integers, some of which repeat */ + int[] intArray = {10, 2, 1, 19, 8, 7, 2, 4, 76, 2}; + String beforeSort = ""; + String afterSort = ""; + for (int i = 0; i < intArray.length; i++) { + beforeSort = beforeSort + " " + intArray[i]; + } + System.out.println("Before:" + beforeSort); + quickSort(intArray, 0, 9); + for (int i = 0; i < intArray.length; i++) { + afterSort = afterSort + " " + intArray[i]; + } + System.out.println("After:" + afterSort); + } + + public static void main(String[] args) { + testingInts(); + System.out.println(" "); + testingRepeats(); + } +} From 563683cea7bcc71c1ed9f5616008320fbcd89b41 Mon Sep 17 00:00:00 2001 From: Crystal Secaira <123703060+csecaira@users.noreply.github.com> Date: Sun, 20 Oct 2024 05:07:49 -0400 Subject: [PATCH 2/2] Added README_quick-sort.md --- code/languages/Java/README_quick-sort.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 code/languages/Java/README_quick-sort.md diff --git a/code/languages/Java/README_quick-sort.md b/code/languages/Java/README_quick-sort.md new file mode 100644 index 0000000000..c86c2fcefe --- /dev/null +++ b/code/languages/Java/README_quick-sort.md @@ -0,0 +1,11 @@ +The quick sort algorithm runs on **O(nlogn)**. It takes an array and assigns an +element as a pivot value and splits (paritions) the array to recursively sort +the pivot value into the correct (sorted) position. However, when the first pivot +element is either the smallest or largest element, this triggers the algorithms +worst time complexity: O(n^2). + +Quick Sort Steps: +1. A pivot is chosen and the array is split into subarrays +2. Elements that are **less** than the pivot are moved to its left +3. Elements that are **greater** than the pivot are moved to its right +4. Quick sort is recursively called on both subarrays