1. Insert sort-Direct Insert sort (Straight insertion sort)
Basic idea:
Inserts a record into a sorted ordered table to get a new ordered table with a record number of 1. That is, the 1th record of the sequence is first considered as an ordered subsequence, and then inserted from the 2nd record, until the entire sequence is ordered.
Important: Set up sentinels for temporary storage and judgment of array boundaries.
Direct Insert Sort Example:
If you encounter an element that is equal to the insertion element, the insertion element places the elements that you want to insert behind the equality element. Therefore, the order of the equal elements is unchanged, the order from the original unordered sequence is the order after the sequence, so the insertion sort is stable.
implementation of the algorithm:
[Java] View plain copy print? public class straihtinertionsort { //fast sorting algorithm public static int[] sort (Int[] arr) { int n = arr.length; for (int i =0; i<n-1 ; i++) { if (arr[i) &NBSP;>&NBSP;ARR[I+1]) { //Exchange int tmp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = tmp; int index = i; //because of the above Exchange,arr[i] < ARR[I+1] , So the index stores the indexes that are about to be compared to the previous one, that is, the surveillance whistle int j=i-1; &nbSp; while ( j>=0 && arr[j] > arr[index] ) { //Exchange int tmp1 = arr[j]; arr[j] = arr[index]; arr[index] = tmp1; index = j; j--; } } } return arr; } //print array public static void print (Int[] arr) { for (int i=0; i<arr.length ; i++) { &nBsp; system.out.print (arr[i] + " "); } System.out.println (); } public