快速排序

Code:
  1. package com.test.算法分析;   
  2.   
  3. import java.util.Scanner;   
  4.   
  5. public class QuickSort {   
  6.   
  7.     /**  
  8.      * @param args  
  9.      */  
  10.        
  11.   
  12.     public static void main(String[] args) {   
  13.         int c[]=InputNumber();//定义一个数组来存放所输入的数组   
  14.         quickSort(c,0,c.length-1);   
  15.         printArray(c);   
  16.   
  17.     }   
  18.        
  19.     public static int[]  InputNumber(){   
  20.         System.out.println("请输入你要排序的个数:");   
  21.         Scanner scan=new Scanner(System.in);   
  22.         int length=scan.nextInt();   
  23.         int a[]=new int[length];   
  24.         System.out.println("请输入待排序的数据:");   
  25.         for(int i=0;i<a.length;i++)   
  26.             a[i]=scan.nextInt();   
  27.            
  28.         return a;   
  29.     }   
  30.      //核心算法代码   
  31.     public static void quickSort(int array[],int left,int right){   
  32.            
  33.         if(left<right){   
  34.             int s=parssion(array,left,right);   
  35.             quickSort(array,left,s-1);   
  36.             quickSort(array,s,right);   
  37.         }   
  38.     }   
  39.        
  40.     public static int  parssion(int array[], int left, int right) {   
  41.            
  42.         int pivot = array[left];   
  43.         while (left <right) {   
  44.             while (right > left&&array[left] <array[right] ) {   
  45.                 right--;   
  46.             }   
  47.             if(right>left){   
  48.                 int temp=0;   
  49.                 temp=array[right];   
  50.                 array[right]=array[left];   
  51.                 array[left]=temp;   
  52.             }   
  53.             //swap(pivot, array[right]);   
  54.             ++left;   
  55.             while (right > left&&array[left] <array[right]  ) {   
  56.                 left++;   
  57.             }   
  58.             if(right>left){   
  59.                 int temp=0;   
  60.                 temp=array[left];   
  61.                 array[left]=array[right];   
  62.                 array[right]=temp;   
  63.             }   
  64.                
  65.             //swap(pivot, array[left]);   
  66.         }   
  67.         printArray(array);   
  68.         System.out.println();   
  69.         return left;   
  70.     }   
  71.        
  72.     /**  
  73.      *   
  74.      * @param a  
  75.      * @param b  
  76.      */  
  77.     public static void swap(int a,int b){   
  78.         int temp=0;   
  79.         temp=b;   
  80.         b=a;   
  81.         a=temp;   
  82.         System.out.println(a+" "+b);   
  83.     }   
  84.     /**  
  85.      *   
  86.      * @param array  
  87.      */  
  88.     public static void printArray(int array[]){   
  89.         for(int a:array)   
  90.             System.out.print(a+",");   
  91.     }   
  92.   
  93. }   

 

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值