Quick sort, selection sort, bubble sort

Three kinds of sorting algorithms are used in the array more ordering, be specifically described below and the differences between the various sorting methods

 Quick sort

Quick sort method using a [n] sorted
to select an element from a [n] as a reference, is generally selected from a [0], set the low point to a [0] (first team), High point to a [n-1] (the tail),
starting with the tail forward scan starts, if a [high]> a [0 ], the high ++, otherwise a [high] assigned to a [low], i.e., a [low] = a [high ]
and then start scanning from the first team forward, if a [low] <a [0 ], the low ++, or a [high] = a [low ]
in this way, all will be less than the reference number of a [0] into elements a [0] on the left, to be greater than a [0] element are placed in a [0] to the right
and then left recursive respectively, right subarray sort, sort finalize

1  // quick sort 
2  static  void quicksort ( int ARR [], int Low, int High) {
 . 3      IF (Low < High) {
 . 4          int TEMP = ARR [Low]; // TEMP as the base of comparison 
. 5          the while (Low < high) {
 . 6              // when the tail element is not less than the reference data, high pointer move forward 
. 7              the while (Low <&& high ARR [high]> = TEMP) {
 . 8                  high-- ;
 . 9              }
 10              / / in this case, arr [high] <temp, the arr [high] assigned to ARR [Low] 
. 11             ARR [low] = ARR [High];
 12 is              
13 is              // When the head of the queue greater than or equal the reference data element, moving backwards low pointer 
14              the while (low <High && ARR [low] <= TEMP) {
 15                  low ++ ;
 16              }
 17              // in this case, arr [low]> temp, the arr [low] assigned to ARR [High] 
18 is              ARR [High] = arr [low];
 . 19          }
 20 is          // later when a cycle, low = high when, at this time the high or low temp is the correct position of the index
 21          // value at this position is not low tmp, it is necessary to assign temp ARR [low] 
22 is          ARR [low] = temp;
 23 is          int index = low ; //Returns the index position temp
 24          
25          // recursive call, continue across the position of the index sub-arrays are sorted according to the above method flow 
26 is          quicksort (ARR, 0, index-. 1 );
 27          quicksort (ARR,. 1 + index , High);
 28      }
 29  }
 30  
31 is  // bubble sort, small to large 
32  @Test
 33 is  public  void bubbleSort () {
 34 is      Scanner SC = new new Scanner (the System.in);
 35      int TEMP, ARR [];
 36      the while (SC. the hasNext ()) { // cycle through test
 37          // input array length and arrays, such as: enter carriage 5, and then enter 13426
38 is          int n-= sc.nextInt ();
 39          ARR = new new  int [n-];
 40          for ( int I = 0; I <n-; I ++ ) {
 41 is              ARR [I] = sc.nextInt ();
 42 is          }
 43 is          / / array adjacent two comparison number, which is greater than the preceding exchanged positions
 44          @ bubble sort: one each of a maximum number (rightmost) occurs sequentially, views of large numbers ... 
45          for ( int I = 0; I <-n-. 1; I ++ ) {
 46 is              for ( int J = 0; J <. 1-n--I; J ++ ) {
 47                  IF (ARR [J]> ARR [J +. 1 ]) {
 48                     = TEMP ARR [J];
 49                      ARR [J] = ARR [J +. 1 ];
 50                      ARR [J +. 1] = TEMP;
 51 is                  }
 52 is              }                
 53 is          }
 54 is          // sorted 
55          for ( int I = 0; I <n-; I ++ ) {
 56 is              of System.out.print (ARR [I] + "" );
 57 is          }
 58      }
 59  }
 60  
61 is  // selection sort, small to large 
62 is  @Test
 63 is  public  voidSelectSort () {
 64      Scanner SC = new new Scanner (the System.in);
 65      int TEMP, ARR [];
 66      the while (sc.hasNext ()) { // cycle through test 
67          int n-= sc.nextInt ();                
 68          ARR = new new  int [n-];
 69          for ( int I = 0; I <n-; I ++ ) {
 70              ARR [I] = sc.nextInt ();
 71 is          }
 72          // array sequentially for each number with its rear comparing the number, if the former than the latter, the position of both the exchange 
73 is          for ( int I = 0; I <-n-. 1; I ++){
74             for(int j=i+1;j<n;j++){
75                 if(arr[i]>arr[j]){
76                     temp = arr[i];
77                     arr[i] = arr[j];
78                     arr[j] = temp;
79                 }
80             }                
81         }
82         //排序后
83         for(int i=0;i<n;i++){
84             System.out.print(arr[i]+" ");
85         }
86     }
87 }

Further, similar bubble sort and selection sort, the time complexity is the same.

Here is the complexity of the various sorting algorithms comparison:

These are the sort of describes the three, if insufficient noted!

Quick sort can refer to: https: //blog.csdn.net/nrsc272420199/article/details/82587933

Guess you like

Origin www.cnblogs.com/superwei001/p/12038736.html