50-Channel Programming small topic---288 large sort algorithm Java 01 direct insertion Sort __ajax

Source: Internet
Author: User
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;&GT;&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 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.