Direct insertion sort of the two approaches

Many people may not notice this problem, and today just met, then a little talk about

 

Direct insertion sort should be sorting algorithm, the algorithm described in many data structures and algorithms book first talk is this:

The sort column to be treated as two paragraphs, one is sorted column is not some sort column. When sorting each trip, as the first column of the lookup unsorted in the sorted column (because direct insertion sort, especially where it is compared one by one) its proper position, and then inserted (insertion process is accompanied by a after the series of elements shift).

 

I did not think too much, write directly to a code by text description:

DEF InsertSort1 (the LIST):
     for I in Range (1, len (the LIST)): # starting from 1, 0 to sort column element considered 
        the TEMP the LIST = [I] # store intermediate variables 
        for II in Range (0, I ):
             IF the LIST [ii]> the TEMP: # front to back successive approximation, if it is larger than the elements present, then it shows that he should fall on this bit. Here it is not added in order to ensure equal numbers sorted stability 
                for III in Range (I, II, -1): # Note that the displacement element from the back 
                    the LIST [III] the LIST = [-III. 1 ] 
                the LIST [II] = the TEMP
                 BREAK 
    return the LIST

 

Later, looking at the time found that does not seem quite right, how nested three-for, and thus be recalled this period should be compared from the back of

def InsertSort2(LIST):
    for i in range(1,len(LIST)):
        TEMP=LIST[i]
        flag=True
        for ii in range(i-1,-1,-1):#从后往前进行逐次比较
            if LIST[ii]>TEMP:
                LIST[ii+1]=LIST[ii]
            else:
                 LIST[ii+1]=TEMP
                 flag=False
                 break
        if flag:
            LIST[0]=TEMP
    return LIST

This uses two auxiliary variables, TEMP and flag

 

In fact many books written in this period of time, with another clever way, but with an auxiliary variable, where you can see the other articles, I wrote here was immediately recalled the book this approach called the Sentinel, 0 element that is not used to store an array of elements, as a temp use, where needed flag does not require the use of flag, to maintain the consistency of operations. In fact, this is not the focus we have to discuss.

 

At first glance, the second method only two nested for, is not to be more than a good method?

The answer is no, in fact, the problem with a map can be a good expression of:

Performance InsertSort1 () is very stable, in the drawings, of which the successive approximation "away", "away" and subsequent displacement of the previous period is just add line length (l)

The performance InsertSort2 () is not so stable, in the figure, when the elements to be ranked in the sorted column in the right position on the list, its performance is certainly better (shorter pay "journey"), and when to be front row of elements, its performance is worse (to pay twice as much, much longer "walk")

 

In fact, two practices are in line with insertion sort of thinking, the same average performance, the best performance in the case of 2 better than 1, in the worst case 1> 2

To be honest I did not expect this most original algorithm such a fun little hidden secret, it can be considered a rewarding

Guess you like

Origin www.cnblogs.com/asakura/p/11108518.html