Java 快速排序

举报
陈言必行 发表于 2021/08/14 00:41:36 2021/08/14
【摘要】 快速排序代码实现: 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];...

快速排序代码实现:


  
  1. public class P_5 {
  2. static final int SIZE=5;
  3. static void quickSort(int[] arr,int left,int right){
  4. int f,t;
  5. int rtemp,ltemp;
  6. ltemp=left;
  7. rtemp=right;
  8. f=arr[(left+right)/2];
  9. while(ltemp<rtemp){
  10. while(arr[ltemp]<f){
  11. ++ltemp;
  12. }
  13. while(arr[rtemp]>f){
  14. --rtemp;
  15. }
  16. if(ltemp<=rtemp){
  17. t=arr[ltemp];
  18. arr[ltemp]=arr[rtemp];
  19. arr[rtemp]=t;
  20. --rtemp;
  21. ++ltemp;
  22. }
  23. }
  24. if(ltemp==rtemp){
  25. ltemp++;
  26. }
  27. if(left<rtemp){
  28. quickSort(arr, left, ltemp-1);
  29. }
  30. if(ltemp<right){
  31. quickSort(arr, rtemp+1, right);
  32. }
  33. }
  34. public static void main(String[] args) {
  35. int[] shuzu=new int[SIZE];
  36. int i;
  37. for(i=0;i<SIZE;i++){
  38. shuzu[i]=(int)(100+Math.random()*(100+1));
  39. }
  40. System.out.print("排序前的数组为:\n");
  41. for(i=0;i<SIZE;i++){
  42. System.out.print(shuzu[i]+" ");
  43. }
  44. System.out.print("\n");
  45. quickSort(shuzu, 0, SIZE-1);
  46. System.out.print("排序后的数组为:\n");
  47. for(i=0;i<SIZE;i++){
  48. System.out.print(shuzu[i]+" ");
  49. }
  50. System.out.print("\n");
  51. }
  52. }


文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。

原文链接:czhenya.blog.csdn.net/article/details/77609684

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。