python3-- say simple is not simple sorting algorithm

  At the beginning of contact algorithm, we may look ignorant, I do not know where to start, especially now use a wide variety of languages, and achieve a variety of different languages, so, in this case, do not get lost himself, mastered the algorithm works, like solving mathematical formulas, theorems to you, think they can use the code to implement it, you've got to develop good problem-solving ideas.

Let's start from the sorting algorithm entry:

The following is a disordered array for A = [3,1,5,6,7,2,4,8] to sort

1. Simple bubble sort

Early into the sorting algorithm, bubbling to try it, almost violent principles and enumeration method, very much as possible to enumerate all the elements are out, one by one comparison, the practice of simple and rough (very * very violent).

On the following codes:

'' ' 
Bubble sort 
' '' 
DEF bubblesorft (A): 
    for I in Range (len (A)): a first layer # cycle, the control sequencing number 
        for j in range (len (A ) -i): # cycle before the second layer, a control element and a rear element size than 
            IF. 1 + J <len (a): 
                IF a [J]> a [+ J. 1]: 
                    TEMP = a [+ J. 1] 
                    a [J + 1'd] = A [J] 
                    A [J] = TEMP 
    return A 

A = [3,1,5,6,7,2,4,8] 
A = bubblesorft (A) 
Print (A)

[1, 2, 3, 4, 5, 6, 7, 8]

Algorithm is simple and clear, if not read, then knock yourself several times the code, you will soon realize, ha ha

This all is not finished, the next on a simple but not simple merge sort algorithm, you come to realize it:

2. Simple but not simple merge sort

Said simply because you get a thorough understanding of, say around a very simple recursive people actually want to understand when to return, this time wanted to understand the value of recursion.

In the first sorting merge sort the array into two portions, and separately sorted, and then sorted into one of two parts of the array is sorted.

The following code on feel:

'' ' 
Merge sort algorithm 
' '' 
DEF mangesorft (A): 
    IF len (A) <=. 1: 
        return A 

    Half = int (len (A) / 2) # The list is divided into two 
    first = mangesorft (A [0 : half]) # a recursive, so that the last element points smaller, until the element, and then return the sorted list of 
    second = mangesorft (a [half: len (a)]) # a recursive, such that the last sub-element of smaller and smaller, until the element, and then return the sorted list of 

    I = 0 
    J = 0 
    newA = [] 
    the while I <len (First) or J <len (SECOND): # number of control cycles 
        if i <len (first) and j <len (second): # key merge sort, merge control element index 
            IF First [I] <= SECOND [J]: 
                newA.append (First [I]) 
                I + = . 1 
            the else: 
                newA.append (SECOND [J])  
                J =. 1 +
        the else:
            if i < len(first):
                newA.append(first[i])
                i += 1
            if j < len(second):
                newA.append(second[j])
                j +=1
    return newA

if __name__ == "__main__":
    A = [3,1,5,6,7,2,4,8]
    A = mangesorft(A)
    print(A)

[1, 2, 3, 4, 5, 6, 7, 8]

Comparison of these two algorithms, so what you think not, and if so, then congratulations, you can pull together a small hand jumped into the pit of the algorithm,

With these two artifacts, after the encounter algorithmic problems for disorderly array or list, Nazan first to row a sequence of it, this is the first step in problem-solving, come on!

Guess you like

Origin www.cnblogs.com/weijiazheng/p/10984963.html