Insert Sorting Algorithm and Sorting Algorithm

Source: Internet
Author: User

Insert Sorting Algorithm and Sorting Algorithm

There is an ordered data sequence that requires inserting a number in the sorted data sequence, but the data sequence is still ordered after insertion, in this case, we need to use a new sorting method-insert sorting algorithm. The basic operation of insert sorting is to insert a data into ordered data that has been sorted in order to obtain a new ordered data with a number plus one. The algorithm applies to sorting a small amount of data.

Java Implementation of insert sorting:

1 package com. mianshi. easy; 2 public class Insert {3 4 public static void main (String [] args) {5 int [] a = {, 8, 7 }; 6 7 insertSort (a); 8 9 for (int I = 0; I <. length; I ++) {10 System. out. print (a [I] + ""); 11} 12} 13 14 // insert sort 15 public static void insertSort (int []) {16 17 for (int I = 1; I <. length; I ++) {18 int temp = a [I]; 19 int j = I-1; 20 while (j> = 0 & a [j]> temp) {// compare the obtained temp value with the preceding value, which is smaller than the preceding value, and place the preceding value backward. values greater than or equal to (stable) values are not exchanged, directly jump out of the value 21 a [j + 1] = a [j]; 22 j --; 23} 24 a [j + 1] = temp; 25} 26} 27} 28 29 Results: 30 2 3 7 8 9 14 15 16 21

Algorithm Implementation diagram: (optimistic about the algorithm based on the above implementation diagram)

 

 

 

Time Complexity:The average time complexity is.

Insert sorting is not suitable for sorting applications with large data volumes. However, if the data size to be sorted is small, for example, if the magnitude is smaller than, insertion sorting is still a good choice.

Algorithm stability:Stable sorting algorithm.

Insert sorting inserts an element at a time based on an ordered small sequence. Of course, at the beginning, this ordered small sequence had only one element, which was the first element. The comparison starts from the end of the ordered sequence, that is, the element to be inserted is compared with the already ordered sequence. If it is larger than it, it is directly inserted after it, otherwise, search until you find the inserted position. If you encounter an element that is equal to the inserted element, the inserted element is placed behind the element that you want to insert. Therefore, the order of equal elements is not changed, and the order from the original unordered sequence is the order after sorting, so insertion sorting is stable.

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.