最近有些懒,好久没写代码了,现在写个快排练练手吧。
public class QucikSort { //此处交换两个数 public static void swap(int a[],int low,int high) { int temp=a[low]; a[low]=a[high]; a[high]=temp; } //分区,分成两部分 public static int partion(int a[],int low,int high) { swap(a,low,high); int cur=low; int index=low; while(a[cur]<a[high]) { if(a[cur]<a[high]) { a[index++]=a[cur]; } cur++; } swap(a,index,high); return index; } public static void quicksort(int a[],int low,int high) { if(low<high) { int part=partion(a,low,high); quicksort(a,low,part-1); quicksort(a,part+1,high); } } public static void main(String[] args) { // TODO Auto-generated method stub int a[]=new int[]{-2,3,4,-5,4,565,56}; for(int i=0;i<a.length;i++) { System.out.print(a[i]+" "); } System.out.println(); quicksort(a,0,a.length-1); for(int i=0;i<a.length;i++) { System.out.print(a[i]+" "); } } }
时间: 2024-11-08 02:47:47