冒泡排序法的三种方法。

冒泡法:

public class Sort{
 
 public static void main(String[] args){
  Random rand = new Random();
  
  int numCount = 10;
  int[] numArray = new int[numCount ];


  for(int i = 0; i < 10; i++){
   numArray[i] = rand.nextInt(1000);
  }

  System.out.println("排序前:");
  
  Sort sort = new Sort();
  sort.printArray(numArray);

  sort.sort(numArray);

  System.out.println("排序后:");
  sort.printArray(numArray);
  

  
 }

反向排序法:

public class QSortAlgorithm extends SortAlgorithm
{

 
    private boolean pauseTrue(int lo, int hi) throws Exception {
 super.pause(lo, hi);
 return true;
    }

   void QuickSort(int a[], int lo0, int hi0) throws Exception
   {
      int lo = lo0;
      int hi = hi0;
      int mid;

      if ( hi0 > lo0)
      {

         mid = a[ ( lo0 + hi0 ) / 2 ];


         while( lo <= hi )
         {

      while( ( lo < hi0 ) && pauseTrue(lo0, hi0) && ( a[lo] < mid ))
   ++lo;

      while( ( hi > lo0 ) && pauseTrue(lo0, hi0) && ( a[hi] > mid ))
   --hi;

            if( lo <= hi )
            {
               swap(a, lo, hi);
               ++lo;
               --hi;
            }
         }


         if( lo0 < hi )
            QuickSort( a, lo0, hi );

   
         if( lo < hi0 )
            QuickSort( a, lo, hi0 );

      }
   }

   private void swap(int a[], int i, int j)
   {
      int T;
      T = a[i];
      a[i] = a[j];
      a[j] = T;

   }

   public void sort(int a[]) throws Exception
   {
      QuickSort(a, 0, a.length - 1);
   }
}

快速排序法:

public class Sort{
 
 public static void main(String[] args){
  Random rand = new Random();
  
  int numCount = 20;
  int[] numArray = new int[numCount ];


  for(int i = 0; i < 20; i++){
   numArray[i] = rand.nextInt(1000);
  }

  System.out.println("排序前:");
  
  Sort sort = new Sort();
  sort.printArray(numArray);
          
  sort.sort(numArray);

  System.out.println("排序后:");
  sort.printArray(numArray);
  

  
 }

 private boolean stopRequested;

 

 

 public void printArray(int[] arr){
  for(int i = 0 ; i < arr.length; i++){
   System.out.println(arr[i]);
  }
 }

 public void sort(int[] arr){
//  排序
  for (int i = arr.length; --i>=0; ) {
      boolean swapped = false;
      for (int j = 0; j<i; j++) {
   if (stopRequested) {
       return;
   }
   if (arr[j] > arr[j+1]) {
       int T = arr[j];
       arr[j] = arr[j+1];
       arr[j+1] = T;
       swapped = true;
   }
   pause(i,j);
      }
      if (!swapped)
   return;
  }
 }

 private void pause(int i, int j) {
 
  
 }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值