java sort, bubble sort, selection sort, insertion sort, quick drain

Bubble Sort

 

Time complexity: O (n ^ 2) space complexity of O (1)
Stability: Stable
  1. Compare adjacent elements. If the first is greater than the second, the two of them exchanged.
  2. Do the same work for each pair of adjacent elements, from the beginning to the end of the first to the last pair. Once this is done, the last element is the biggest number.
  3. Repeat these steps for all elements, except the last one.
  4. Continued to repeat the above steps for each time fewer and fewer elements, a pair of numbers until there is no need to compare.

 

 

. 1      Private  static  void bubbleSort ( int [] ARR) {
 2          for ( int I = 0; I <arr.length -. 1; ++ I) {
 . 3              Boolean the swap = to false ; // using a tag, detecting whether been exchanged
 4              for ( int J = 0; J <arr.length -. 1 - I; ++ J) {
 . 5                  IF (ARR [J]> ARR [J +. 1 ]) {
 . 6                      the swap = to true ;
 . 7                      int tmp = ARR [J ];
 . 8                      ARR [J] = ARR [J +. 1 ];
 . 9                     ARR [J +. 1] = tmp;
 10                  }
 . 11              }
 12 is              IF (! swap) {// swap that if false, no exchange occurs over, orderly array, the program ends
 13 is                  return ;
 14              }
 15          }
 16      }

 

Bubble sort optimized, avoiding the relatively orderly array after useless.

Selection Sort

First, the sort sequence is not found in the largest element, the starting position is stored in the sorted sequence, and then continue to look for the largest element from the remaining elements of unsorted and sorted into the end of the sequence. 
And so on, until all the elements are sorted.
  1. Never collating sequence, locate the largest element keyword
  2. If the element is not the maximum element of the first unsorted sequence, which first sequence and unsorted exchange element
  3. Repeat steps 1 and 2 until the end of the sort.
Stability: Unstable 
time complexity of O (n ^ 2) space complexity of O (1)
 1     public static void choiceSort(int[]arr) {
 2         for (int i = 0; i < arr.length - 1; i++) {
 3             int k=i;int temp;
 4             for (int j = i + 1; j < arr.length; j++) {
 5                 if (arr[j] <arr[k]) {
 6                     k=j;
 7                 }
 8             }
 9             if (k != i) {
10                 temp = arr[i];
11                 arr[i] = arr[k];
12                 arr[k] = temp;
13             }
14         }
15     }

Insertion Sort

Direct insertion sort

In a basic set of ordered sequences, sorting operation, insertion sort is the highest efficiency
Time complexity: O (n ^ 2) space complexity: O (1)
Stability: Stable

starts traversing the array subscripts 1; a front contrast, if the former is less than one, out of this loop, for the next cycle.
If the former is greater than one, after a preceding shift, a front continue contrast, up until the position is less than one, out of this cycle, the vacancies.
 1     public static void insertSort(int[]arr) {
 2         for (int i = 1; i < arr.length; i++) {
 3             int val = arr[i];
 4             int j = i - 1;
 5             for (; j >= 0; --j) {
 6                 if (val < arr[j]) {
 7                     arr[j + 1] = arr[j];
 8                 } else break;
 9             }
10             arr[j + 1] = val;
11         }
12     }

Improved insertion sort

Using a binary search method for finding a proper insertion position, comparison process can be reduced, improving efficiency

 

. 1      public  static  void insertSort ( int [] ARR) {
 2          for ( int I =. 1; I <arr.length; I ++ ) {
 . 3              int TEMP = ARR [I];
 . 4              int J = I;
 . 5              // binary search to find suitable insertion position 
. 6              int First = 0 ;
 . 7              int Last = J-. 1 ;
 . 8              the while (First <= Last) {
 . 9                  int Middle = (First + Last) / 2 ;
 10                  IF (ARR [J] < ARR [Middle ]) {
 11                     last = middle - 1;
12                 } else if (arr[j] > arr[middle]) {
13                     first = middle + 1;
14                 }
15             }
16             while (j>first){
17                 arr[j]=arr[j-1];
18                 j--;
19             }
20             arr[first] = temp;
21         }
22     }

 

 

 

Fast row

Stability: Unstable
Time complexity: (log2 n) worst case O: O (n ^ 2)
Optimization measures fast row:
1. When a small range of data to a certain degree, instead of using insert sequencing fast row
2. Select the appropriate reference number (random number process) (taking the number three) 

Quicksort basic idea: digging divide and conquer + number of refills .
Quick sort using a divide and conquer strategy to put a sequence is divided into two sub-sequences.
  1. Singled out a number of elements from the column, called "benchmarks."
  2. Reorder columns, all smaller than the reference value elements placed in front of the base, all larger than the reference value of the element placed behind the baseline (the same number can be either side). After the end of the partition, the reference is in the middle of the number of columns. This is called the partition (partition) operation.
  3. Recursively the number of sub-elements than the reference value and the number of columns of sub-elements is greater than the reference value column.
 1    public static void quickSort(int[]arr,int i,int j){
 2 
 3         if(i>j)
 4             return;
 5         int l=partation(arr,i,j);
 6         quickSort(arr,i,l-1);
 7         quickSort(arr,l+1,j);
 8     }
 9     private static int partation(int[] arr, int i, int j) {
10         int temp=arr[i];
. 11          the while (I < j) {
 12 is              the while (I <j && ARR [j]> temp) {
 13 is                  J, ;
 14              }
 15                  // from the beginning to find j is smaller than a first value temp 
16                  IF (I < j) {
 . 17                      ARR [I] = ARR [J];
 18 is                      I ++ ;
 . 19                  }
 20 is              the while (I <J && ARR [i + 1] < temp) {
 21 is                  I ++ ;
 22 is              }
 23 is              // find larger than the temp from i + 1 elements value 
24-                  IF (i<j) {
25                     arr[j] = arr[i + 1];
26                     j--;
27                 }
28         }
29         arr[i]=temp;
30         return i;
31     }

 

Guess you like

Origin www.cnblogs.com/jiezai/p/11029614.html