Java algorithm-Insert sort

Source: Internet
Author: User

The basic idea of inserting a sort is that, in the process of traversing an array, assuming that the element that precedes the ordinal i is [0..i-1] is already sorted, this trip needs to find the correct position K for the element x corresponding to I, and move the compared element one after another in the search for this position k, which is the element x "vacated position" and finally The value of the element that corresponds to K is assigned to x, and the insertion sort is named according to the ordered attribute.

  The insertion sort works by constructing an ordered sequence, for unsorted data, from a backward forward scan in a sorted sequence, to find the appropriate location and insert it. See the code and comments for specific steps.

The following is an instance where the number marked with red is the inserted number, the number that is crossed out is the element that is not involved in this sort, the element between the red mark and the number that is crossed out is the element that moves backwards, for example, the second pass participates in the ordering element for [11, 31, 12], the element that needs to be inserted is currently not in the correct position, so we need to compare with the previous element 31, 11, while comparing the elements moved over one side, until the first to find a smaller than 12 of the element 11 o'clock stop comparison, at this time 31 corresponding index 1 is the location of 12 needs to be inserted.

Initial: [One, Up, 5, 5, (+), (+), +, (+)] The first trip: [One, A, a, A/V, +, +,--------    ] (no moving elements) second trip: [
    11, 5, (-- ), +-----------------------------------------------  4> are moved backwards) fourth: [5, one, A, a, a, a, a, a, a, 11, 30] (no moving elements) fifth trip: [5, 12, 31, 34, 26, 38, 36, 18,] (to move backwards) The six: [5, one, A, a, a, a, a, a, a, a, six, 11, and move backwards] The seventh trip: [5,, 12, 26, 30,  --------------------------------(no moving elements) eighth: [5, one, one, a, a, a, a, a, a, a.] ( move backwards) nineth trip: [5, 11, 12, 18 , 26, 30, 31, 34, 36, 38] (26, 30, 31, 34, 36, 38 move backwards)

The insertion sort is better than the selection sort, because it can take advantage of the pre-ordered array elements in the sorting process, effectively reducing the number of comparisons, which, of course, depends on the initial order of the arrays, In the worst case (the given array happens to be in reverse order) the number of insertions to be compared and moved will be equal to 1 + 2 + 3 ... + n = n * (n + 1)/2, in extreme cases, the efficiency of the insertion order is even worse than the selection sort. So the insertion sort is an unstable sorting method, and the insertion efficiency is closely related to the initial order of the array. In general, the time complexity and space complexity of the insertion Order are O (N2) and O (1) respectively .

Implementation code:

/*** Insert Sort <br/> * <ul> * <li> starting with the first element, the element can be thought to have been sorted </li> * <li> take out the next element, in the sequence of ordered elements from the back Forward Scan </li> * <li> if the element (sorted) is greater than the new element, move the element to the next position </li> * <li> Repeat step 3 until the sorted element is found to be less than or equal to the position of the new element </li > * <li> Insert new element into this location </li> * <li> Repeat step 2</li> * </ul> * *@paramnumbers*/   Public Static voidInsertsort (int[] numbers) {       intSize =Numbers.length, temp, J;  for(intI=1; i<size; i++) {Temp=Numbers[i];  for(j = i; j > 0 && Temp < numbers[j-1]; j--) Numbers[j]= Numbers[j-1]; NUMBERS[J]=temp; }   }

Java algorithm-Insert sort

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.