码迷,mamicode.com
首页 > 编程语言 > 详细

快速排序

时间:2015-04-17 22:13:31      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

(java版)
public class Qsort {

static int Partition(int l[],int low,int high){

int prvotkey = l[low];
while(low<high){
while(low<high && l[high] >= prvotkey)
high--;
l[low] = l[high];
while(low<high && l[low] <= prvotkey)
low++;
l[high] = l[low];
}
l[low] = prvotkey;
return low;
}

static void QSort(int l[],int low, int high){
int prvotkey;
if(low < high){
prvotkey = Partition(l,low,high);
QSort(l,low,prvotkey-1);
QSort(l,prvotkey+1,high);
}
else{
// System.out.println("error!");
}
}

static void quicksort(int l[],int n){
QSort(l,0,n-1);
}

public static void main(String[] args) {
int a[] = {0,2,5,43,32,65,43,21,87,1};
int b;
for(b=0; b<10;b++){
System.out.print(a[b]+ " ");
}
System.out.println();
quicksort(a,10);
int c;
for(c = 0; c<10 ; c++){
System.out.print(a[c]+" ");
}
}

}

快速排序

标签:

原文地址:http://www.cnblogs.com/tangtang-123/p/4436034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!