神州数码笔试:快排

题目描述:

输入一个字符串,如3,5,1,2,6,7
输出排序后的字符串,要求使用快排

package leetcode;

import java.util.Scanner;

public class Main

// 快排
public static void qsort(int[] arr, int left, int right) {
if(left < right) {
int i = left;
int j = right;
int x = arr[left];
while(i < j) {
while(i < j && arr[j] >= x) j--;
if(i < j) {
arr[i++] = arr[j];
}
while(i < j && arr[i] < x) i++;
if(i < j) {
arr[j--] = arr[i];
}
}
arr[i] = x;
qsort(arr, left, i-1);
qsort(arr, i+1, right);
}
}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String line = sc.nextLine();
String[] arr = line.split(",");

int[] list = new int[arr.length];
for(int i = 0; i < arr.length; i++) {
list[i] = Integer.parseInt(arr[i]);
}
qsort(list, 0, list.length-1);
for(int i = 0; i < list.length - 1; i++) {
System.out.print(list[i]+" ");
}
System.out.println(list[list.length-1]);
}
sc.close();
}

}

基础要打牢啊