Eight sorting algorithm principles and Java implementations (direct insert sort)

Source: Internet
Author: User

Overview

Sorting has an internal sort and an external sort, the internal sort is the data record is sorted in memory, and the external sort is because the sorted data is large, one cannot hold all the sort records at a time, and the external memory needs to be accessed during the sorting process.

Here we talk about eight sorts of sort are internal sort.


When n is larger, it should be sorted by the time complexity O (nlog2n) : Quick Sort, heap sort, or merge sort order.

Quick sort: is currently considered the best method based on the comparison of internal sorting, when the keywords to be sorted are randomly distributed, the average time of the fast sorting is shortest;


1. Insert sort-Direct insert sort (straight insertion sort)

Basic idea:

Insert a record into the sorted ordered table to get a new, sequential table with a 1 increase in the number of records. That is, the 1th record of the sequence is considered an ordered subsequence, and then inserted from the 2nd record one after the other until the entire sequence is ordered.

Important: Set up Sentinel as a temporary storage and judgment array boundary.

Example of a direct insert sort:


If you encounter an element equal to the insert, the insertion element places the element you want to insert behind the equal element. So, the order of the equal elements is not changed, the order from the original unordered sequence is the order of the sequence, so the insertion sort is stable.

The implementation of the algorithm:

1 /**2  *3  * @authorZhangtao4  */5  Public classStraightinsertionsort6 {7      Public Static voidMain (string[] args)8     {9         intarr[]={3,1,5,7,2,4,9,6};Ten Insertsort (arr); One     } A     //Direct Insert Sort -     Static voidInsertsort (int[] a) -     { the         intArrlength=a.length; -          for(intI= 1; i<arrlength; i++){   -             if(A[i] < a[i-1]) {//if the first element is greater than the i-1 element, it is inserted directly. Less then, move the ordered table after inserting -                 intj= i-1;  +                 inttemp = A[i];//Copy as Sentinel, which stores the elements to be sorted -A[i] = a[i-1];//move one element at a succession +                  while(Temp<a[j]) {//find the insertion position in an ordered table AA[J+1] =A[j];  atj--;//element Move back -                     if(j<0) -                     { -                          Break; -                     } -                 }   inA[J+1] = temp;//Insert to correct position -             }   toPrintLine (A,i);//Print the results of each trip sort +         }   -     } the     //Print the sort results each time *     Static voidPrintLine (int[] arr,inti) $     {Panax NotoginsengSystem.out.println (i+ ":"); -         intArrlength=arr.length; the          for(intj=0;j<arrlength;j++) +         { ASystem.out.print (arr[j]+ ""); the         } + System.out.println (); -     } $}

Efficiency:

Time complexity: O (n^2).

The other insert sort has two-insert sort, 2-way insert sort.

Eight sorting algorithm principles and Java implementations (direct 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.