给出了12个数(ps:具体哪12个忘了),要求是:写一个函数快速对上述数据排序

解决方案 »

  1.   

    才12个数,给个冒泡的:public class Sort {public static void main(String[] args) {int[] values = { 3, 1, 6, 2, 9, 0, 7, 4, 5, 15, 23, -8};
    sort(values);for (int i = 0; i <values.length; ++i) {
    System.out.println("Index:" + i + " Value: " + values[i]);
    }
    }public static void sort(int[] values) {
    int temp;
    for (int i= 0; i < values.length; ++i) {
      for(int j = 0; j < values.length - i -1; ++j) {
          if(values[j] > values[j + 1]) {
             temp= values[j];
          values[j]= values[j + 1];
    values[j+ 1] = temp;
    }
    }
    }}}
      

  2.   


       1. public class BubleSort {  
       2.    
       3.   public static void print(int [] temp) {//打印数组算法  
       4.   for(int i=0;i<temp.length;i++) {  
       5.    System.out.print(" " + temp[i]);  
       6.   }  
       7.  }  
       8.    
       9.  public static void bubleSort(int [] a) {//冒泡排序  
      10.   int length = a.length ;  
      11.   int temp = 0;  
      12.   for(int i=length-1;i>=1;i--) {  
      13.    for(int j=0;j<=i-1;j++) {  
      14.     if(a[j] > a[j+1]) {  
      15.      temp = a[j] ;  
      16.      a[j] = a[j+1] ;  
      17.      a[j+1] = temp ;  
      18.     }  
      19.    }  
      20.   }  
      21.  }  
      22.    
      23.  public static void main(String []args) {//注意本例中,排序int由命令行参数输入  
      24.   int a[] = new int[args.length];  
      25.   for(int i=0;i<args.length;i++){  
      26.    a[i] = Integer.parseInt(args[i]) ;  
      27.   }  
      28.   BubleSort b = new BubleSort();  
      29.   b.bubleSort(a);//调用冒泡排序  
      30.   b.print(a);//打印排序后结果  
      31.  }  
      32. }  
      

  3.   

    public class PaiXu { public static void main(String[] args) {
    int[] array = {1,2,3,4,5,6,7,8,9,0};//12数自己再加两个·不想按了!上班ing
    int swap;
    for(int i=0;i<array.length;i++){
    for(int j=0;j<i;j++){
    if(array[i]>array[j]){
    swap=array[i];
    array[i]=array[j];
    array[j]=swap;
    }
    }
    }
    for(int i=0;i<array.length;i++){
    System.out.println(array[i]);
    }
    }}
      

  4.   

    public class BubleSort {  
       2.    
       3.   public static void print(int [] temp) {  
       4.   for(int i=0;i<temp.length;i++) {  
       5.    System.out.print(" " + temp[i]);  
       6.   }  
       7.  }  
       8.    
       9.  public static void bubleSort(int [] a) {//冒泡排序  
      10.   int length = a.length ;  
      11.   int temp = 0;  
      12.   for(int i=length-1;i>=1;i--) {  
      13.    for(int j=0;j<=i-1;j++) {  
      14.     if(a[j] > a[j+1]) {  
      15.      temp = a[j] ;  
      16.      a[j] = a[j+1] ;  
      17.      a[j+1] = temp ;  
      18.     }  
      19.    }  
      20.   }  
      21.  }  
      22.    
      23.  public static void main(String []args) {//注意本例中,排序int由命令行参数输入  
      24.   int a[] = new int[args.length];  
      25.   for(int i=0;i<args.length;i++){  
      26.    a[i] = Integer.parseInt(args[i]) ;  
      27.   }  
      28.   BubleSort b = new BubleSort();  
      29.   b.bubleSort(a);//调用冒泡排序  
      30.   b.print(a);//打印排序后结果  
      31.  }  
      32. }  
      

  5.   


    public class Bubblesequence
    {
    public static void main(String[] args)
    {
    int[] a = {5, 4, 31, 2, 1};

    bubbleSequence(a);

    for(int i=0; i<a.length; i++)
    {
    System.out.print(a[i] + " ");
    }
    }
    //冒泡法排序
    private static void bubbleSequence(int[] a)
    {
    int temp;
    for(int i=0; i<a.length-1; i++)
    {
    for(int j=0; j<a.length-1-i; j++)
    {
    if(a[j] > a[j+1])
    {
    temp = a[j];
    a[j] = a[j+1];
    a[j+1] = temp;
    }
    }
    } }


    //选择法排序
    private static void xuanZe(int[] a)
    {
    int temp;
    for(int i=0; i<a.length-1; i++)
    {
    int k = i;
    for(int j=i+1; j<a.length; j++)
    {
    if(a[k] > a[j])
    {
    k = j;
    }
    if(i != k)
    {
    temp = a[i];
    a[i] = a[k];
    a[k] = temp;
    }
    }
    }
    }


    }
      

  6.   


    public class Paixu
    {
    public static void main(String[] args)
    {
    int[] a = {5, 4, 31, 2, 1};

    bubbleSequence(a);

    for(int i=0; i<a.length; i++)
    {
    System.out.print(a[i] + " ");
    }
    }
    //冒泡法排序
    private static void bubbleSequence(int[] a)
    {
    int temp;
    for(int i=0; i<a.length-1; i++)
    {
    for(int j=0; j<a.length-1-i; j++)
    {
    if(a[j] > a[j+1])
    {
    temp = a[j];
    a[j] = a[j+1];
    a[j+1] = temp;
    }
    }
    } }


    //选择法排序
    private static void xuanZe(int[] a)
    {
    int temp;
    for(int i=0; i<a.length-1; i++)
    {
    int k = i;
    for(int j=i+1; j<a.length; j++)
    {
    if(a[k] > a[j])
    {
    k = j;
    }
    if(i != k)
    {
    temp = a[i];
    a[i] = a[k];
    a[k] = temp;
    }
    }
    }
    }


    }
      

  7.   

    为何大家不直接用sort()方法呢?直接调用java中的sort()方法,好像是用快排的!
      

  8.   

    这种小算法建议楼主自己研究,而不要COPY