Sort by Java algorithms -- sort by insertion

Source: Internet
Author: User

Relationship between Sorting: the method of directly inserting the sort (straight insertion sort) is to retrieve the first element from the unordered table each time and insert it into the proper position of the ordered table, make the ordered table still orderly. The first step is to compare the first two numbers, and then insert the second number into an ordered table by size. The second step is to scan the third data and the first two numbers from the front to the back, insert the third number to the ordered table by size. perform the following operations in sequence. After (n-1) scanning, the entire sorting process is completed. Directly inserted sorting is a stable sorting. The worst-case time complexity is round (n ^ 2), and the space complexity is O (1 ). Direct insertion sorting is composed of two nested loops. The outer loop identifies and determines the value to be compared. The inner loop determines its final position for the value to be compared. Direct insertion and sorting compare the value to be compared with the previous value, so the outer loop starts from the second value. When the current value is greater than the value to be compared, continue the loop comparison until a value smaller than the value to be compared is found and the value to be compared is placed in the next position to end the loop. It is worth noting that we must use a bucket to save the value to be compared, because when the comparison is complete, we need to place the last digit of the value to be smaller than the value to sort the cards like playing cards. The basic way to insert sorting is: each step inserts a record to be sorted into the appropriate position in the previously sorted sequence according to the size of its keyword until all records are inserted. Initial sequence: I = 1 [46] 58 15 45 90 18 10 62 found I = 2 [46 58] 15 45 90 18 10 62 found -- too many I = 3 [15 46 58] 45 90 18 10 62 rows -- average I = 4 [15 45 46 58] 90 18 10 62 rows I = 5 [15 45 46 58 90] 18 10 62 rows ----- maximum I = 6 [15 18 45 46 58 90] 10 62 Jun -------- Jun I = 7 [10 15 18 45 46 58 90] 62 Jun-Jun I = 8 [10 15 18 45 46 58 62 90] C/C ++ code for direct insertion sorting: void insert_sort (int a [], int n) {int I, j, temp; for (I = 1; I <n; ++ I) {temp = a [I]; for (j = I; j> 0 & temp <a [j-1]; -- j) {a [j] = a [j-1];} a [j] = temp ;}} Java code implementation direct insertion sorting: public class InsertSort {public static void main (String [] args) {insertSort ();} public static void insertSort () {int a [] = {49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51}; int temp = 0; for (int I = 1; I <. length; I ++) {int j = I-1; temp = a [I]; System. out. println ("temp1:" + temp); for (; j> = 0 & temp <a [j]; j --) {a [j + 1] = a [j]; // move the value greater than temp to a System. out. println ("a [j]" + a [j]);} a [j + 1] = temp; System. out. println ("temp2:" + temp);} for (int I = 0; I <. length; I ++) System. out. println (a [I]) ;}}

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.