Created
May 6, 2019 01:46
-
-
Save alonWang/5dd0d1478ceb237aabd9384b75b8c19e to your computer and use it in GitHub Desktop.
quick sort实现
This file contains hidden or 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 com.company; | |
import java.util.Arrays; | |
public class QuickSort { | |
public static void sort(Integer[] arrays, int low, int high) { | |
if (high <= low) { | |
return; | |
} | |
int parationIdx = paration(arrays, low, high); | |
sort(arrays, low, parationIdx - 1); | |
sort(arrays, parationIdx + 1, high); | |
} | |
private static int paration(Integer[] arrays, int low, int high) { | |
int base = arrays[low]; | |
int i = low, j = high + 1; | |
while (true) { | |
do { | |
++i; | |
} while (arrays[i] <= base && i < high); | |
do { | |
--j; | |
} while (arrays[j] >= base && j > low); | |
if (i >= j) { | |
break; | |
} | |
swap(arrays, i, j); | |
} | |
swap(arrays, low, j); | |
return j; | |
} | |
private static void swap(Integer[] arrays, int i, int j) { | |
int temp = arrays[i]; | |
arrays[i] = arrays[j]; | |
arrays[j] = temp; | |
} | |
public static void main(String[] args) { | |
Integer[] arrays = new Integer[]{4, 786, 56, 76, 87, 65, 90, 77, 5, 88}; | |
sort(arrays, 0, arrays.length - 1); | |
Arrays.asList(arrays).forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment