The relationship between 8 sorts:
1, direct insertion sort
(1) Basic idea: In the set of numbers to be sorted, suppose that the front (n-1) [n>=2] number is already a row
In a good order, now you have to insert the nth number into the preceding ordered number so that the number of n
It's a good order, too. Repeat the loop until all the order is sorted.
(2) Example
(3) Implement with Java[Plain] View plain copy package com.njue; publicclass insertsort { public insertsort () { int a[]={ 49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51}; int temp=0; for (int i=1;i< a.length;i++) { int j=i-1; temp=a[i]; for (; j>=0&&temp<a[j];j--) { a[j+1]=a[j]; //will be greater than the value of the temp whole to move one unit } a[j+1]=temp; } for (int i=0;i<a.length;i++) system.out.println (A[i]); } }
2, Hill Sort (minimum increment sort)
(1) Basic idea: The algorithm first will be sorted by a group of numbers by an increment D (n/2,n to the number of sorted numbers) into several groups, the subscript that is recorded in each group differs D. Inserts a direct sort of all elements in each group, and then groups it with a smaller increment (D/2). Make a direct insert sort in each group. When the increment is reduced to 1 o'clock, the sort completes after a direct insert sort.
(2) Example:
(3) implemented in Java [plain] View plain copy publicclass shellsort { publicshellsort () { int a[]={ 1,54,6,3,78,34,12,45,56,100}; double d1=a.length; int temp=0; while (true) { d1= math.ceil (D1/2); int d= (int) d1; for (int x=0;x<d;x++) { for (Int i=x+d;i<a.length;i+=d) {