# [Data structure and algorithm 03] insert and sort directly

Source: Internet
Author: User

Algorithm ideas (sorted from small to large)
• View the Left and Right sequences in the sorted array. The left sequence is ordered and there is a sequence unordered.
• Assume that the first number array [0] is a sequence, then array [1, N-1] is a sequence. loop traversal without sequence, find out where array [I] should be inserted in sequence, insert.
• For example, if a disordered array is {1, 3, 2, and 2, if the ordered column {1, 3} has no sequence loops to 2, then 2 should be in the middle of 1-3 in the sequence, so [3... (2) The right shift of all data between them, that is, the 2 Location of the 3 right shift, freeing up the original location of 3, which is replaced by 2.

Blog: http://blog.csdn.net/mkrcpp/article/details/39320797

`Import Java. util. arrays;/***** @ title insert sort directly * @ author Michael. mao * @ date 1:59:04 on January 1, September 16, 2014 * @ version V1.0 */public class insertsort {/***** @ title insert the sort directly * @ Description: views the Left and Right sequences of the sorted array, the left sequence is ordered and there is a sequence disorder. * If the first number array [0] is a sequence, array [1, N-1] is a sequence. loop traversal without sequence, find out where array [I] * should be inserted in sequence, insert. <Br/> * For example, if an unordered array is {1, 3, 2, and 2, if the ordered column {1, 3} has no sequence loops to 2, * Then 2 should be in the middle of 1-3 in a sequence, so [3... 2) The right shift of all data between them, that is, the location of 3 * Right Shift 2, freeing up the original location of 3, and replaced by 2 * @ author Michael. mao * @ date September 16, 2014 2:08:40 * @ version V1.0 */public static void execute (INT [] array) {// The position to be inserted: int tmpv, j = 0; // The loop has no sequence, the number of times is N-1, the default ordered column is {array [0]} For (INT I = 1; I <array. length; I ++) {// Save the value to be inserted tmpv = array [I]; // find the proper position in the sequence for (j = 0; J <I; j ++) if (array [I] <array [J]) break; // If array [I] is larger than any of the sequences, when J exits the loop and J = I, array [0, I] is a sequence, and if (J! = I) {// shift the [J, I-1] Order right, freeing up array [J] position for (int K = I-1; k> = J; k --) array [k + 1] = array [k]; // insert array [J] = tmpv;} system. err. println (I + "----" + common. print (array) ;}}// Number of cyclic tests public static int loop_count = 1; public static int array_size = 7; public static void main (string [] ARGs) {int [] marray = Common. getarray (array_size); int alltime = 0; For (INT I = 0; I <loop_count; I ++) {// copy the array int [] tmparray = arrays. copyof (marray, array_size); Common. print (tmparray); long tmptime = system. currenttimemillis (); execute (tmparray); Common. print (tmparray); alltime + = system. currenttimemillis ()-tmptime;} system. err. println ("the array size is (" + array_size + ")" + loop_count + "The average time consumed by the next insertion sorting is:" + alltime/(float) loop_count );}}`

`1----58 80 15 27 01 68 43 2----15 58 80 27 01 68 43 3----15 27 58 80 01 68 43 4----01 15 27 58 80 68 43 5----01 15 27 58 68 80 43 6----01 15 27 43 58 68 80`

`The average time consumption for 10000 direct insertion sorting of an array of (100) is: 17.58 MS`

[Data structure and algorithm 03] insert and sort directly

Related Keywords:

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.