Python -- sorting and python sorting

Source: Internet
Author: User

Python -- sorting and python sorting

After I graduated from school, I only knew the Bubble sorting and found that I had a very simple understanding of the sorting.

As a result, I searched for various sorting methods on the Internet. The following are some simple explanations I made based on the information I found and my understanding.

If there are any errors, please correct them. (Learning and making progress together)

1. Insert sorting: the optimum is O (n), the worst is O (n ^ 2), and the average O (n ^ 2)
(1) always define the first element as ordered, and insert the remaining elements one by one into this sequence.
(2) During the Insert Process, the elements to be inserted are compared one by one with those in a sequence and inserted to the appropriate position.
Python:

Def InserSort (lists): count = len (lists) for I in range (1, count ): key = lists [I] # index = I # record position of the element to be inserted while index> 0 and lists [index-1]> key: # compare one by one, if it is smaller than the previous number, move the element forward to the position lists [index] = lists [index-1] index-= 1 # Insert the element to be inserted, insert the specified position lists [index] = key return lists
View Code

2. Bubble Sorting: the optimum is O (n), the worst is O (n ^ 2), and the average O (n ^ 2)

(1) Compare adjacent elements. If the first is bigger than the second, exchange the two of them.
(2) perform the same operation on each adjacent element, starting from the first pair to the last one. At this point, the final element should be the largest number.
(3) Repeat the preceding steps for all elements except the last one.
(4) continue to repeat the above steps for fewer and fewer elements until there is no need to compare any number.
Python:

Def BubbleSort (lists): count = len (lists) for I in range (count): for j in range (count-i-1 ): if lists [j]> lists [j + 1]: lists [j], lists [j + 1] = lists [j + 1], lists [j] # return lists
View Code

3. Hill sorting: (essentially group insertion sorting)
(1) take an integer d1 less than n as the first increment and group all records of the file. All records whose distance is multiples of d1 are placed in the same group.
(2) Direct insertion sorting in each group
(3) take the second increment d2 <d1 repeat the preceding grouping and sorting until the obtained increment = 1, that is, all records are placed in the same group for direct insertion sorting.

def ShellSort(lists):    count = len(lists)    group = count // 2    while group>0:        for i in xrange(count):            index =i             j = i+1            while j<count:                if lists[j]<lists[index]:                    index = j                j += group            if index != j:                lists[index],lists[i]=lists[i],lists[index]        group = group //2    return lists
View Code

4. Fast sorting: Maximum O (NlogN)
(1) set the two variables I, j. When the sorting starts, I = 0, j = n-1.
(2) Use the first element of the array as the base, key = A [0].
(3) search forward from j, that is, start backward (j --), and find the first value smaller than or equal to the key.
(4) Search backward from I, that is, search backward from the beginning (I ++) to find the first value greater than or equal to the key.
(5) swap the number locations found in step 1 and step 2.
(6) Repeat steps 3, 4, and 5 until I = j swap the base with A [I. In this case, it is bounded by A [I] and is divided into two zones.
(7) Repeat steps 3, 4, 5, and 6 for the two zones

Python

Def quickSort (lists, left, right): if left> = right: return lists # Set the base key = lists [left] I = left j = right while I <j: # starting from the right of the list, find a number smaller than or equal to the base number. while I <j and lists [j]> = key: j = j -- # Start from the left of the List to find a number larger than or equal to the base number. while I <j and lists [I] <= key: I = I ++ if I <j: lists [I], lists [j] = lists [j], lists [I] # after completing the first round of comparison, the list is divided into two parts, and I = j, set this number back to the base lists [left] = lists [I] lists [I] = key # repeat the preceding operation before and after recursion quickSort (lists, left, I-1) quickSort (lists, j + 1, right) return lists
View Code

5. Box sorting: O (m + n)
(1) set several boxes and scan records to be sorted in sequence: R [0], R [1],…, R [n-1]
(2) load all records with the keyword k to the k box (Allocation)
(3) connect the first and end of non-empty boxes in sequence (collect ).
Python

Def barrelSort (lists): # select a maximum number of maxnum = max (lists) # create a list where all elements are 0, as the bucket = [0] * (maxnum + 1) # Put all elements in the box, and Add 1 for I in lists to the number of corresponding elements: bucket [I] + = 1 # store sort_lists = [] # retrieve the elements in the box for j in range (len (bucket): if bucket [j]! = 0: for n in range (bucket [j]): sort_lists.append (j) return sort_lists
View Code

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.