Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added java quick sort #6818

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions code/languages/Java/README_quick-sort.md
Original file line number Diff line number Diff line change
@@ -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
73 changes: 73 additions & 0 deletions code/languages/Java/quick-sort.java
Original file line number Diff line number Diff line change
@@ -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();
}
}