Python algorithm Exercises

Source: Internet
Author: User

1. Insert Sort:

Insert Sort By default the first is the list that is already sorted, after each one is compared and sorted, after the row, all the natural has been lined up, the second optimization algorithm, if it has been inserted, to find the right position, then break, if you do not need to sort, then it is already the largest, Because the front has been lined up, after the row, there is no need to row, you can break.

Import Timestart = Lambda:time.time () List1 = [66,33,1,3,111,3,10,29,7,123,56,23] * 50def busorted (unsorted):    L = len (unsorted)    For I in Range (0,l):        key = Unsorted[i] for        x in range (i,0,-1):            if UNSORTED[X] < Unsorted[x-1]:                unsorte D[X], unsorted[x-1] = unsorted[x-1], unsorted[x]    return unsorteddef optimizesorted (unsorted):    l = Len (unsorted ) for    I in range (0,l):        key = Unsorted[i] for        x in range (i,0,-1):            if unsorted[x] < UNSORTED[X-1]:
   UNSORTED[X], unsorted[x-1] = unsorted[x-1], unsorted[x]            else:                break    return unsortedif __name__ = = ' __ main__ ':    print (list1)    t1 = start ()    print (busorted (list1))    t2 = start ()    print ('----------- Time is {} '. Format (T2-T1))

  

Python algorithm Exercises

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.