Chapter 3: array [5 common algorithm] - [9 Sort] - [quick sort]

Quick sort: the quick sort is known as one of the best algorithm to achieve idea is to sort an array of issues as a scheduling problem with two small array, and each small array can continue to be seen as more small two arrays, has been recursion, until the maximum length of the array size is 2.

 

public int[] fastSort(int[] arr,int left,int right){  

      if(left < right){  

          int s = arr[left];  

          int i = left;  

          int j = right + 1;  

          while(true){  

              // find the right element of the index is greater than s  

              while(i+1 < arr.length && arr[++i] < s);  

              // find a left element of the index is less than s  

              while(j-1 > -1 && arr[--j] > s);  

              // If i> = j launch cycle  

              if(i >= j){  

                  break;  

              }else{  

                  // i and j elements of enlightenment location  

                  int t = arr[i];  

                  arr[i] = arr[j];  

                  arr[j] = t;  

              }  

          }  

          arr[left] = arr[j];  

          arr[j] = s;  

          // to left recursive  

          fastSort(arr,left,j-1);  

          // recursive to the right  

          fastSort(arr,j+1,right);  

      }  

      return arr;  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/Lucky-stars/p/11010226.html