利用随机类生成数组,并用冒泡排序以及选择排序对不同数组的元素进行从小到大的排序

利用随机类生成数组,并用冒泡排序以及选择排序对不同数组的元素进行从小到大的排序

import java.util.Random;

public class BubbleSort {

           int num=20;      

              int[] a=new int[num];

              String[] b=new String[num];

              Random rand=new Random();            

   BubbleSort(){

               for(int i=0;i

                   Integer k=new Integer(rand.nextInt(100));

                   Integer t=new Integer(rand.nextInt(100));

                 a[i]=k;

                 b[i]=String.valueOf(t); }

               System.out.println("冒泡排序序列,原始随机数列a[]    ");

               for(int j=0;j

                System.out.print(a[j]+" ");}

               System.out.println("");

               System.out.println("选择排序序列,原始随机数列b[]    ");

               for(int j=0;j

                System.out.print(b[j]+" ");}      }    

   void BubbleSortProcedure(){  //冒泡排序方法

                         int pass,i,exchangeCnt;

                      int temp =0;

for(pass=0;pass扫描多次

                  exchangeCnt=0;//记录本轮两两交换的次数

                          for(i=0;i      //一次扫描过程

                          { //每次扫描比较范围缩小一个

                              if( a[i]>a[i+1] { //一次两两比较交换过程

                                     temp = a[i];

                                  a[i] = a[i+1];

                                  a[i+1] = temp;

                                  exchangeCnt++;}}

                     prnt();

                     System.out.println(" ");               

                     if(exchangeCnt == 0) //若一次也未交换,则说明已完全排好序,不必再循环                    return; }}   

   void SelectSortProcedure(){   //选择排序方法

                  int pass,i,k;

               String temp =" ";

               for(pass=0;pass

                   for(i=pass,k=i;i     

                       if(b[i].compareToIgnoreCase(b[k]) < 0)     

                           k = i;                  

                   temp = b[pass];

                   b[pass] = b[k];

                   b[k] = temp; }

               System.out.println("利用选择排序排列原始随机数列:    ");

               for(i=0;i

                   System.out.print(b[i]+" ");}  

   void prnt(){

               for(int i=0;i

                      System.out.print(a[i]+" ");

            }     

   public static void main(String[] args){

               BubbleSort bubble=new BubbleSort();

               System.out.println("");

               System.out.println("利用冒泡排序排列原始随机数列:    ");

               bubble.BubbleSortProcedure();

               bubble.SelectSortProcedure();

       }} 

你可能感兴趣的:(算法分析代码区)