Java bubble sort method to achieve

  "Bubble sort" may be a disordered array in ascending order or descending order of the sort, is a more common sorting algorithm, since the data ascending or descending manner process to the number of columns or the number of columns of the first end referred to as "bubbling." For the bubble sort I think the most important points is to: (1) the number of times required to be reordered, ie the number of cycles, this sequence is based on the size of the array to set, such as the size of the array is n, then need to loop it is the number ( n--. 1 ) times; (2) a further number of times required for each cycle is a comparison between the data, ordering may be determined after each maximum or minimum of the data, not then the next cycle comparison, so each frequency is compared ( n-- I ) ;

  In the case of a disorderly array, or whether it is sorted in descending order from small to large bubbling, if n is an array of data, then the first data without the need for size comparison with yourself, you will need to compare is with the remaining n - 1 cycles of data, so it is necessary to re-sort is n - 1 times, determine the number of comparisons during each cycle is a critical need to make, can be determined in the course of the first cycle (with small to large, for example) the largest number so that the next cycle will not need to be compared.

  The following example is an array in small to large order:

 1 package ren.arrayListSortedTest;
 2 
 3 import java.util.Scanner;
 4 
 5 public class ArraySortedTest {
 6     //排序
 7     public static void sort(int [] unSortedArray){
 8         for(int i=1;i<unSortedArray.length;i++){
 9             for(int j=0;j<unSortedArray.length-i;j++){
10                 if(unSortedArray[j]>unSortedArray[j+1]){
11                     int temp = unSortedArray[j];
12                     unSortedArray[j]=unSortedArray[j+1];
13                     unSortedArray[j+1]=temp;
14                 }
15             }
16         }
17 
18     }
19     //打印(已排序好)
20     public static void print(int [] unPrintedArray){
21         for(int i=0;i<unPrintedArray.length;i++){
22             System.out.print(unPrintedArray[i]+",");
23         }
24     }
25     public  static  void main (String [] args) {
 26 is          Scanner SC = new new Scanner (the System.in);
 27          // first open a keyboard input size specified array 
28          int [] = unSortedArray new new  int [sc.nextInt () ];
 29          for ( int I = 0; I <unSortedArray.length; I ++ ) {
 30              // keyboard input content specified array 
31 is              unSortedArray [I] = sc.nextInt ();
 32          }
 33 is          ArraySortedTest.sort (unSortedArray) ;
 34 is          ArraySortedTest.print (unSortedArray);
 35         System.out.println ( "sort execution finished .." );
 36      }
 37 }

  Results of the:

 

   Input through the keyboard 5 to be opened to determine the capacity of the array is an array of data 5, and then specify the size of each element is the number of the original array here is [34,23,11,65,24], after sorting the results to [11,23,24,34,65].

Guess you like

Origin www.cnblogs.com/dashenaichicha/p/12329576.html