My Java Development Learning journey------The insertion sort of the >java classic sorting algorithm

Source: Internet
Author: User


first, the principle of the algorithmInsert Sort method: the so-called insertion sort method is to insert a number into the occupied position. Suppose we enter "53,27,36,15,69, 42" We start with the second number, this number is 27, our task is just to see if 27 has the right position, our practice is compared to the number on the left of this number, so we compare 27 and 53, 27:53 small, so we exchanged 27 and 53, the original arrangement has become the "", ","Next, let's see if the 3rd number is in the right position. This number is 36, its left number is 53, 36:53 is small, so we will exchange 36 and 53, arranged into a " --------"We must continue to see whether 36 is in the right place, 36 on the left is 27, 27:36 is small, 36 is not moving,the sort is still" -------- ". look at the fourth number, the number is 15, we will 15 and its left side of the number than 15, So the 151 way to the left to move, when the sort became "15,  27,  36,  53, A. ". And look at the fifth number, the number is 69, we compare 69 to the number on the left, it's smaller than 69, so the 69 remained motionless, when the sort became ",,,A ."
Finally, we check the sixth number, the number is 42,42 must move to the left, moving to 42 to the left is 36, so our arrangement becomes ",,A. A.The sort was thus finished.the so-called insertion sorting method, is to check the number I, if the number on its left is larger than it, to exchange, this action continues until the number of the left of the number is smaller than it, it can be stopped. Insert Sort method The main loop has two variables: I and J, each time the loop is executed, the number I will be placed in the appropriate position on the left. Second, the algorithm description
1, starting with the first element, the element can be considered to have been sorted. 2. Take out the next element and scan it from the back forward in the sequence of elements that have been sorted. 3. If the element (sorted) is greater than the new element, move the element to the next position. 4. Repeat step 3 until you find that the sorted element is less than or greater than the position of the new element. 5. Insert the new element into the position. 6, repeat step 2.
Three, efficiency analysis
If the goal is to arrange the sequence of n elements in ascending order, then the insertion sort is the best and worst case scenario. Best case: The sequence is already in ascending order, in which case the comparison operation needs to be done (n-1) times. worst case: The sequence is in descending order, then there are N (n-1)/2 times to be performed at this time. The direct insert sort is a stable sort, the worst time complexity is O (n^2), the best time complexity is O (n), and the spatial complexity is O (1). The assignment operation to insert a sort is the number of times the comparison operation is added (n-1).
Therefore, the insertion sort is not suitable for applications that have a larger amount of data for sorting.

Iv. Implementation of the Code
public class Insertsorttest {public static void Insertsort (int[] source) {int I, j;int insertnode;//the data to be inserted//starts with the second element of the array The ring inserts the elements in the array for (i = 1; i < source.length; i++) {//sets the 2nd element in the array as the first loop to insert the data Insertnode = Source[i];j = i-1;//If the element to be inserted is less than The first J element moves the J element Backward while ((J >= 0) && Insertnode < source[j]) {source[j + 1] = source[j];j--;  } Until the element to be inserted is not less than the first J element, insert Insertnote into the array source[j + 1] = Insertnode; System.out.print ("First" + i + "sequencing:");p Rintarray (source);}} private static void PrintArray (int[] source) {for (int i = 0; i < source.length; i++) {System.out.print ("\ t" + source[i ]);} System.out.println ();} public static void Main (string[] args) {int source[] = new int[] {53, 27, 36, 15, 69, 42}; System.out.print ("Initial keyword:");p Rintarray (source); System.out.println (""); Insertsort (source); System.out.print ("\ n-sort result:");p Rintarray (source);}}



v. Results of Operation
Initial keywords:      532736156942 the first 1 orders: 275336156942 The first 2 order: 273653156942 The first 3 order: 152736536942 the 4 order: 152736536942 The first 5 order: 152736425369 sorted Results: 152736425369



==================================================================================================

Ouyangpeng welcome reprint, sharing with people is the source of progress!

Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng

==================================================================================================




My Java Development Learning journey------The insertion sort of the >java classic sorting algorithm

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.