Several examples of common sorting algorithms implemented by Python

Source: Internet
Author: User
The previous period of time for the preparation of Baidu interview bad things, although the final still be brushed, or the days of the "booty" put points up, the algorithm has been a relatively weak place, after the more efforts ah.

The following python implements several commonly used sorts, such as quick sort, select sort, and two-way and sort, etc.
Copy the Code code as follows:


#encoding =utf-8
Import Random
From copy import copy

def directinsertsort (seq):
"" Insert sort Directly "" "
size = Len (seq)
For I in Range (1,size):
TMP, j = seq[i], I
While J > 0 and TMP < SEQ[J-1]:
Seq[j], j = seq[j-1], j-1
SEQ[J] = tmp
return seq

def directselectsort (seq):
"" Select the sort directly ""
size = Len (seq)
For I in Range (0,size-1):
K = I;j = i+1
While J < size:
If SEQ[J] < Seq[k]:
K = J
J + = 1
SEQ[I],SEQ[K] = Seq[k],seq[i]
return seq

def bubblesort (seq):
"" Bubble Sort "" "
size = Len (seq)
For I in Range (1,size):
For j in Range (0,size-i):
If seq[j+1] < Seq[j]:
SEQ[J+1],SEQ[J] = seq[j],seq[j+1]
return seq

def _divide (seq, Low, high):
"" "Quick Sort Division Function" ""
TMP = Seq[low]
While low! = High:
While low < high and Seq[high] >= tmp:high-= 1
If low < high:
Seq[low] = Seq[high]
Low + = 1
While low < high and Seq[low] <= Tmp:low + = 1
If low < high:
Seq[high] = Seq[low]
High-= 1
Seq[low] = tmp
return low

def _quicksort (seq, Low, high):
"" Quick sort auxiliary function "" "
If Low >= High:return
Mid = _divide (seq, low, high)
_quicksort (seq, Low, mid-1)
_quicksort (seq, mid + 1, high)

def quickSort (seq):
"" "Quick sort Wrap function" ""
size = Len (seq)
_quicksort (seq, 0, size-1)
return seq

def merge (seq, left, Mid, right):
TMP = []
I, j = left, mid
While I < mid and J <= right:
If seq[i] < Seq[j]:
Tmp.append (Seq[i])
i + = 1
Else
Tmp.append (Seq[j])
J + = 1
If I < Mid:tmp.extend (seq[i:])
If J <= Right:tmp.extend (seq[j:])

SEQ[LEFT:RIGHT+1] = tmp[0:right-left+1]

def _mergesort (seq, left, right):
If left = = right:
Return
Else
Mid = (left + right)/2
_mergesort (seq, left, mid)
_mergesort (seq, mid + 1, right)
Merge (seq, left, mid+1, right)

#二路并归排序
def mergesort (seq):
size = Len (seq)
_mergesort (seq, 0, size-1)
return seq

if __name__ = = ' __main__ ':
s = [Random.randint (0,100) for I in Range (0,20)]
Print S
print "\ n"
Print Directselectsort (copy (s))
Print Directinsertsort (copy (s))
Print Bubblesort (copy (s))
Print QuickSort (copy (s))
Print mergesort (copy (s))


The results of the operation are as follows:
Copy CodeThe code is as follows:


e:\python_project\practice>sorting.py
[10, 47, 56, 76, 64, 84, 26, 8, 47, 51, 88, 81, 32, 95, 91, 29, 28, 69, 61, 45]


[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]

  • 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.