Java 快速排序
【摘要】 快速排序代码实现:
public class P_5 { static final int SIZE=5; static void quickSort(int[] arr,int left,int right){ int f,t; int rtemp,ltemp; ltemp=left; rtemp=right; f=arr[(left+right)/2];...
快速排序代码实现:
-
public class P_5 {
-
static final int SIZE=5;
-
static void quickSort(int[] arr,int left,int right){
-
-
int f,t;
-
int rtemp,ltemp;
-
ltemp=left;
-
rtemp=right;
-
f=arr[(left+right)/2];
-
while(ltemp<rtemp){
-
while(arr[ltemp]<f){
-
++ltemp;
-
}
-
-
while(arr[rtemp]>f){
-
--rtemp;
-
}
-
if(ltemp<=rtemp){
-
t=arr[ltemp];
-
arr[ltemp]=arr[rtemp];
-
arr[rtemp]=t;
-
--rtemp;
-
++ltemp;
-
}
-
-
}
-
-
if(ltemp==rtemp){
-
-
ltemp++;
-
}
-
if(left<rtemp){
-
quickSort(arr, left, ltemp-1);
-
-
}
-
if(ltemp<right){
-
quickSort(arr, rtemp+1, right);
-
}
-
}
-
public static void main(String[] args) {
-
int[] shuzu=new int[SIZE];
-
int i;
-
for(i=0;i<SIZE;i++){
-
shuzu[i]=(int)(100+Math.random()*(100+1));
-
-
}
-
System.out.print("排序前的数组为:\n");
-
for(i=0;i<SIZE;i++){
-
System.out.print(shuzu[i]+" ");
-
}
-
System.out.print("\n");
-
quickSort(shuzu, 0, SIZE-1);
-
System.out.print("排序后的数组为:\n");
-
for(i=0;i<SIZE;i++){
-
System.out.print(shuzu[i]+" ");
-
-
}
-
System.out.print("\n");
-
}
-
}
文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。
原文链接:czhenya.blog.csdn.net/article/details/77609684
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)