快速排序和冒泡排序

写个快速排序和冒泡排序的例子,比较下2者

这个快速排序写的不是特别好,凑合一下吧,呵呵

  1. package sort;
  2. public class QuickSort 
  3. {
  4.     /**
  5.      * @author yjb87
  6.      * @分析 快速排序法 和 冒泡排序法 的详细步骤
  7.      * @结论 快速排序法 在多核CPU下具有更高的效率
  8.      */
  9.     public static void main(String[] args) 
  10.     {
  11.         int array[] = new int[10];
  12.         array[0] = 13;
  13.         array[1] = 6;
  14.         array[2] = 2;
  15.         array[3] = 10;
  16.         array[4] = 5;
  17.         array[5] = 4;
  18.         array[6] = 1;
  19.         array[7] = 7;
  20.         array[8] = 3;
  21.         array[9] = 9;
  22.         
  23.         int array1[] = array.clone();
  24.         System.out.println("*******************使用快速排序法********************");
  25.         print(array);
  26.         quickSort(array,0,9,0,1);
  27.         System.out.println("*******************使用冒泡排序法********************");
  28.         print(array1);
  29.         bubble(array1,0);
  30.     }
  31.     /***快速排序法***/
  32.     public static void quickSort(int array[],int first,int last,int count,int realNum)
  33.     {
  34.         /**
  35.          * @array   需要排序的数组
  36.          * @first   初始值为数组第一个元素的索引,即0
  37.          * @last    初始值为数组的最后一个元素的索引,即数组长度
  38.          * @count   计算排序步数
  39.          * @realNum 分治成多少部分进行排序
  40.          * */
  41.         int temp,low,high,list_separator;
  42.         low = first;
  43.         high = last;
  44.         list_separator = array[(first+last)/2];
  45.         do
  46.         {
  47.             while(array[low] < list_separator)
  48.             {
  49.                 low++;
  50.             }
  51.             while(array[high] > list_separator)
  52.             {
  53.                 high--;
  54.             }
  55.             if(low<=high)
  56.             {
  57.                 count++;
  58.                 //System.out.println("第 "+count+" 步 同时分 "+realNum+" 个部分进行排序 /n");
  59.                 System.out.println("第 "+count+" 步");
  60.                 temp=array[low];
  61.                 array[low++]=array[high];
  62.                 array[high--]=temp;
  63.                 print(array);
  64.             }
  65.         }while(low <= high);
  66.         if(first<high)
  67.         {
  68.             realNum++;
  69.             quickSort(array,first,high,count,realNum);
  70.         }
  71.         if(low<last)
  72.         {
  73.             realNum++;
  74.             quickSort(array,low,last,count,realNum);
  75.         }
  76.     }
  77.     /**冒泡排序法**/
  78.     public static void bubble(int array[],int count)
  79.     {
  80.         /**
  81.          * @array 需要排序的数组
  82.          * @count 计算排序步数
  83.          * */
  84.         int temp;
  85.         for(int i=0;i<array.length;i++)
  86.         {
  87.             for(int j=i+1;j<array.length;j++)
  88.             {
  89.                 if(array[i]>array[j])
  90.                 {
  91.                     temp = array[i];
  92.                     array[i] = array[j];
  93.                     array[j] = temp;
  94.                     count++;
  95.                     System.out.println("第 "+count+" 步");
  96.                     print(array);
  97.                 }
  98.             }
  99.         }
  100.     }
  101.     public static void print(int array[])
  102.     {
  103.         for(int i=0;i<array.length;i++)
  104.         {
  105.             if(array[i]<10)
  106.                 System.out.print(" -  "+array[i]);
  107.             else
  108.                 System.out.print(" - "+array[i]);
  109.         }
  110.         System.out.println();
  111.     }
  112. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值