Basic Sorting Algorithm: Python implementation

Source: Internet
Author: User

Basic sorting algorithms, including Bubble sorting, insert sorting, select sorting, heap sorting, and quick sorting.

 

[Bubble Sorting]

Complexity is n * n

# Coding: utf8
# Author: HaxtraZ
# Description: Bubble Sorting


Def bubblesort1 ():
# Locate a minimum element each time and place it in the array Header
N = len ()
For I in range (0, n-1 ):
Swapped = False
For j in range (n-1, I,-1 ):
If a [j] <a [J-1]:
A [j], a [J-1] = a [J-1], a [j]
Swapped = True
If not swapped: break


Def bubblesort2 ():
# This version is explained in P137 C ++ 2004
# Locate the largest element and put it at the end of the array each time
# Boundary Optimization
N = len ()
For I in range (0, n-1 ):
Swapped = False
For j in range (0, n-i-1 ):
If a [j]> a [j + 1]:
A [j], a [j + 1] = a [j + 1], a [j]
Swapped = True
If not swapped: break


Def bubblesort3 ():
# This version is from Wikipedia.
# The outer loop is inherently faulty. If it is range (len (a)-,-1)
# When the input data is 3, 5, 1, the result is incorrect.
# Of course, I have modified this error on Wikipedia.
For j in range (len (a)-1, 0,-1 ):
For I in range (0, j ):
If a [I]> a [I + 1]:
A [I], a [I + 1] = a [I + 1], a [I]
  

Insert sorting]

Complexity is n * n

# Coding: utf8
# Author: HaxtraZ


Def insertion_sort1 ():
# Linear insertion sorting
For j in range (1, len ()):
Key = a [j]
I = j-1
While I> = 0 and a [I]> key:
A [I + 1] = a [I]
I = I-1
A [I + 1] = key

Def binInsertSort ():
# Binary insert sorting
N = len ()
For j in range (1, n ):
Key = a [j]
I = j-1

If key> a [I]:
Continue
L, r = 0, I
While l <= r:
# Print l, r
Mid = (l + r)/2
If key <a [mid]:
R = mid-1
Else:
L = mid + 1
K = j
While k> l:
A [k] = a [k-1]
K = k-1

A [l] = key
  

[Select sorting]

Complexity is n * n

# Coding: utf8
# Author: HaxtraZ
# Description: Select sorting


Def selectsort1 ():
# Finding the minimum element each time
N = len ()
For I in range (0, n-1 ):
For j in range (I + 1, n ):
Minpos = I # subscripts used to record the smallest element
If a [j] <a [minpos]:
Minpos = j
# If a [j] And a [minpos] are exchanged here, the bubblesort

If minpos! = I:
A [minpos], a [I] = a [I], a [minpos]


Def selectsort2 ():
# Find the largest element each time
N = len (n)
For I in range (n-1, 0,-1 ):
Maxpos = 0
For j in range (1, I + 1 ):
If a [j]> a [maxpos]:
Maxpos = j

If maxpos! = I:
A [I], a [maxpos] = a [maxpos], a [I]
  

[Heap sorting]

Complexity is nlogn

# Coding: utf8
# Author: HaxtraZ
# Description: heap sorting
# Modified from introduction to algorithms 2nd Edition


Def LEFT (I ):
Return 2 * I + 1


Def RIGHT (I ):
Return 2 * I + 2


Def PARENT (I ):
Return (I-1)/2


Def MAX_HEAPIFY (a, I, heapsize ):
L = LEFT (I)
R = RIGHT (I)
If l Largest = l
Else:
Largest = I

If r Largest = r

If largest! = I:
A [I], a [largest] = a [largest], a [I]
MAX_HEAPIFY (a, largest, heapsize)


Def BUILD_MAX_HEAP ():
Heapsize = len ()
I = PARENT (len (a)-1)
While I> = 0:
MAX_HEAPIFY (a, I, heapsize)
I-= 1


Def HEAP_SORT ():
BUILD_MAX_HEAP ()
N = len ()
Heapsize = n
For I in range (n-1, 0,-1 ):
A [0], a [I] = a [I], a [0]
Heapsize-= 1
MAX_HEAPIFY (a, 0, heapsize)


A = [,]
HEAP_SORT ()
Print
  

[Quick sorting]

Complexity is nlogn

# Coding: utf8
# Version1
 
Def quickSort (alist ):
QuickSortHelper (alist, 0, len (alist)-1)

Def quickSortHelper (alist, first, last ):
If first <last:

Splitpoint = partition (alist, first, last)

QuickSortHelper (alist, first, splitpoint-1)
QuickSortHelper (alist, splitpoint + 1, last)


Def partition (alist, first, last ):
Required tvalue = alist [first]

Leftmark = first + 1
Rightmark = last

Done = False
While not done:

While leftmark <= rightmark and \
Alist [leftmark] <= effectvalue:
Leftmark = leftmark + 1

While alist [rightmark]> = effectvalue and \
Rightmark> = leftmark:
Rightmark = rightmark-1

If rightmark <leftmark:
Done = True
Else:
Temp = alist [leftmark]
Alist [leftmark] = alist [rightmark]
Alist [rightmark] = temp

Temp = alist [first]
Alist [first] = alist [rightmark]
Alist [rightmark] = temp


Return rightmark

Alist = [54,26, 93,17, 77,31, 44,55, 20]
QuickSort (alist)
Print (alist)
  
 

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.