Last active
June 29, 2021 02:38
-
-
Save ntub46010/905cdfb9389af61b52566db4814a3992 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class QuickSort { | |
public static void sort(int[] data) { | |
sort(data, 0, data.length - 1); | |
} | |
private static void sort(int[] data, int startIndex, int endIndex) { | |
if (startIndex >= endIndex) { | |
return; | |
} | |
int left = startIndex + 1; | |
int right = endIndex; | |
while (true) { | |
while (data[left] < data[startIndex] && left < endIndex) { | |
left++; | |
} | |
while (data[right] > data[startIndex] && right > startIndex) { | |
right--; | |
} | |
if (left < right) { | |
swap(data, left, right); | |
} else { | |
swap(data, startIndex, right); | |
break; | |
} | |
} | |
sort(data, startIndex, right - 1); | |
sort(data, right + 1, endIndex); | |
} | |
private static void swap(int[] array, int index1, int index2) { | |
int tmp = array[index1]; | |
array[index1] = array[index2]; | |
array[index2] = tmp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment