码迷,mamicode.com
首页 > 其他好文 > 详细

快速排序

时间:2014-06-20 14:41:45      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:class   tar   数据   string   art   for   

package T1;

public class QuickSort {
public static void quickSort(int a[],int start,int end)
{
int i,j;
i =start;
j =end;
if((a==null)||(a.length==0))
return;
while(i<j)
{
while(i<j&&a[i]<=a[j])/*以数组start下标的数据为key,右侧扫描*/
{
j--;
}
if(i<j)/*右侧扫描,找出第一个比key小的,交换位置*/
{
int temp=a[i];
a[i] =a[j];
a[j] =temp;
}
while(i<j&&a[i]<a[j])/*左侧扫描(此时a[j]中存储着key值)*/
{
i++;
}
if(i<j)/*找出第一个比key大的,交换位置*/
{
int temp=a[i];
a[i] =a[j];
a[j] =temp;
}
}
if(i-start>1)
{
/*递归调用,把key前面的完成排序*/
quickSort(a,start,i-1);
}
if(end-i>1)
{
quickSort(a,i+1,end);/*递归调用,把key后面的完成排序*/
}
}

public static void main(String[] args) {
int s[]={6,2,7,3,8,9,1,78,45,67,3,4,-5,50};
quickSort(s, 0, s.length-1);
for(int i=0;i<s.length;i++){
System.out.println(s[i]);
}
}
}

快速排序,布布扣,bubuko.com

快速排序

标签:class   tar   数据   string   art   for   

原文地址:http://www.cnblogs.com/feipiaopiao/p/3796958.html

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