[Data structure and algorithm] sorting algorithm (Python)

Source: Internet
Author: User

1. Direct Insert Sort

Given an array, starting from the second element, if it is smaller than the first one, swap the position with him, or else it will not move; if the third element is smaller than the second one, the third one will be swapped with the second one, and the second is compared with the first one.

def Insert_sort (arr):     = len (arr)    for in range (1, length):        if arr[i] < arr[i-1] :            for in range (I-1,-1,-1):                if arr[j+1] < arr[j] :                    arr[j+1],arr[j] = arr[j],arr[j+1]    return arr
View Code

Time complexity: O (n^2).

2. Hill Sort method

The array is divided into several segments to sort, and after the shot is also placed in the original paragraph subscript, for example

For the first stroke of the number labeled 1, it is 27 and 38 larger than which, which is arr[1], the same arr[0] values in 49 and 13 of the smaller values

Here's how to sort each trip

def Shell_insert_sort (ARR,DK):     = Len (arr    ) for in  Range (Dk,len (arr)):        if arr[i] < arr[i- DK]:            for in range (I-dk,-1,-1):                if arr[j+dk] < Arr[j]:                    arr[j+dk],arr[j] = arr[j],arr[j+dk]

Here's the general approach

def Shell_sort (arr):     = Len (arr)/2 while     dk >= 1:        shell_insert_sort (ARR,DK)        = DK/2
View Code

In fact, the single-pass method compared with the direct insertion of the 1 is replaced by DK

3, simple selection of sorting

In the set of numbers to sort, select the minimum (or maximum) number to exchange with the 1th position, and then in the remaining number, find the smallest (or largest) number in the 2nd position, and so on, Until the first N-1 element (the penultimate number) and the nth element (the last number) are compared.

is the loop array length, under each bit to find the subscript should be a few, such as the loop to 1, is the second digit, this time if the subscript is 1 that is his own, or exchange with this subscript the number of exchange

The following is the figure of the number I position the subscript should be how much

def Select_min_index (arr,i):     = I    = len (arr)    for in range (i + 1, length):           If Arr[j] < Arr[min_index]:            = J    return Min_index
View Code

Here's the whole method.

def Select_sort (arr):      for inch Range (0,len (arr)):         = Select_min_index (arr,i)        if min_index! = I            := arr[min_index],arr[i]
View Code

The improvement of simple sorting algorithm--two yuan Select sort

For a dollar is the big head of the same sort

4. Bubble sort

The big sink, the small float up ~

def Buble (arr):      for inch Range (0,len (arr)):          for  in range (i+1, Len (arr)):            if arr[i] > arr[j]:                = arr[j],arr[ I]    print arr
View Code

5. Fast Sorting method

1) Select a datum element, usually select the first element or the last element,

2) The records to be sorted are divided into two separate parts, and some of the recorded elements are smaller than the datum element values. The element value of another part of the record is larger than the base value.

3) At this point the datum element is in the correct position after it is sorted

4) Then proceed to sort the two parts of the record in the same way until the entire sequence is ordered.

6. Merge sort

7. Base Order

[Data structure and algorithm] sorting algorithm (Python)

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.