Python implementation sorting algorithm (a)--insert Sort algorithm

Source: Internet
Author: User

1 " "2 Insert Sort Algorithm3 Raw Data4 sort the data after the data sorteddata, by default is the big sort from small dozen5 6 1. Starting with the first element of data, the element is assigned to Sorteddata[0], which can be assumed to have been sorted sorteddata7 8 2. Remove an element of data data[i],9 (1) scan sorteddata from left to right,Ten if Data[i] is less than the sorteddata of an element sd, then insert data[i] into the SD position, select the next element One (1) if data[i] is greater than the last element of Sorteddata, insert data[1] at the end of the Sorteddata and select the next element A  - 3. Repeat steps 2 - " " the  - #/usr/bin/env python - #Coding:utf-8 - #__author__= "livermorium116" +  -  + ImportRandom A Import Time at classinsertionsorted (): -     def __init__(self): -Self.data=Self.datarand () -          -Self. Sorteddata=[self.data[0]]#the 1th element is obtained by default, -         Print('before sorting:') in         Print('__________________') - Self . Sortedbyinsertion () to          +     defsortedbyinsertion (self): -Timecount=time.time ()##程序计时 theTimecountcpu=time.clock ()##CPU计时 *          $          forDinchSelf.data[1:]:##从余下的元素选取Panax Notoginseng              forSdinchSelf. Sorteddata:##选取已排序元素中的一个SD -                     ifD <SD: thetem=Self . Sorteddata.index (SD) + Self . Sorteddata.insert (tem,d) A                         Print(self.) Sorteddata) the                          Break + Select the next element -              $             ifD >= Self. SORTEDDATA[-1]:##比较最后一个元素 $ Self . Sorteddata.append (D) -                 Print(self.) Sorteddata) -                 Continue  the               -                 WuyiTimecountcpu=time.clock ()-timecountcpu theTimecount=time.time ()-Timecount -          Wu         Print('after sorting: \ n') -         Print(self.) Sorteddata) About         Print("Total Sort spents%4f\ncpu time%4f\n"%(TIMECOUNT,TIMECOUNTCPU)) $     defDatarand (self): -temp=[] -          forIinchRange (10): -Temp.append (Random.randint (1,100)) A         Print(temp) +         Print("_____________") the         returnTemp -      $          the if __name__=="__main__": the      theis=insertionsorted () the      -         

Python implementation sorting algorithm (a)--insert Sort algorithm

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.