Created
April 12, 2021 04:57
-
-
Save johnelf/a590f794d1520ed8a9d6f1c59b22f7f8 to your computer and use it in GitHub Desktop.
quick sort with arrays
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
package sorting; | |
public class QuickSortWithArray { | |
public static void main(String[] args) { | |
int[] arrs = new int[10]; | |
for (int i = 0; i < 10; i++) { | |
arrs[i] = (int) (Math.random() * 100); | |
} | |
printArray(arrs); | |
System.out.println(); | |
quickSort(arrs, 0, arrs.length - 1); | |
printArray(arrs); | |
} | |
private static int[] quickSort(int[] arrays, int low, int high) { | |
if (arrays.length <= 0) { | |
return arrays; | |
} | |
if (low >= high) { | |
return arrays; | |
} | |
int lowboundry = low; | |
int highboundry = high; | |
int pivot = arrays[low]; | |
while (low < high) { | |
while (low < high && arrays[high] >= pivot) { | |
high--; | |
} | |
arrays[low] = arrays[high]; | |
while (low < high && arrays[low] <= pivot) { | |
low++; | |
} | |
arrays[high] = arrays[low]; | |
} | |
arrays[low] = pivot; | |
printArray(arrays); | |
System.out.println(); | |
quickSort(arrays, lowboundry, low); | |
quickSort(arrays, low + 1, highboundry); | |
return arrays; | |
} | |
private static boolean isOutIndexOfBoundary(int index, int[] arrs) { | |
return (index < 0 || index > arrs.length - 1); | |
} | |
private static void printArray(int[] arrs) { | |
for (int i = 0; i < arrs.length; i++) { | |
System.out.print(arrs[i] + " "); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment