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