package lintCode;
public class QuickSort {
public void sortIntegers2(int[] A) {
// write your code here
quickSort(A,0,A.length-1);
}
public static void quickSort(int[] nums, int _begin,int _end){
int begin = _begin;
int end = _end;
if(begin < end){
int temp = nums[begin];
while(begin != end){
while(begin < end && nums[end] >= temp)
--end;
//nums[begin] = nums[end];
swap(nums,begin,end);
while(begin < end && nums[begin] <= temp)
++begin;
swap(nums,begin,end);
}
quickSort(nums,_begin,begin-1);
quickSort(nums,end+1,_end);
}
}
public static void swap(int []a,int left,int right){
int temp = a[right];
a[right] = a[left];
a[left] = temp;
}
public static void main(String[] args){
int[] sort = new int[] { 1, 0, 0,10, 20, 3, 5, 6, 4, 9, 8, 12, 17, 34, 11 };
System.out.print("初始:" + "");
print(sort);
quickSort(sort, 0, sort.length - 1);
System.out.print("结果:" + "");
print(sort);
}
public static void print(int[] a){
for(int i:a){
System.out.print("--"+i);
}
System.out.println();
}
}
排序算法——快速排序
最新推荐文章于 2023-07-14 00:19:57 发布