Two ways to insert a sort

Source: Internet
Author: User

Some adjustments have been made to the insertion sort now used, which optimizes the speed. In fact, the best optimization is to generate processing data when possible to keep order, the order is mainly from small to large arrangement.

Master Logic prior to optimization

arr = [2,4,6,7,9,5,8,3,1];arr_len = Len (arr); i = 1;while I < arr_len:j = i;        While J > 0 and Arr[j-1] > Arr[j]: # in the loop body in exchange for TMP = ARR[J];        ARR[J] = arr[j-1];        ARR[J-1] = tmp;    J-= 1; i + = 1;print (arr);

The idea of optimization focused on two points, 1 is the initialization of TMP, 2 is the time of the element exchange position

arr = [2,4,6,7,9,5,8,3,1];arr_len = Len (arr); i = 1;while I < arr_len:j = i;    TMP = Arr[j];        While J > 0 and Arr[j-1] > Tmp:arr[j] = arr[j-1];    J-= 1;    ARR[J] = tmp; i + = 1;print (arr);


Two ways to insert a sort

Related Article

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.