This article mainly introduces python code examples for quick sorting.
1. algorithm description:
1. first, extract a number from the series as the reference number.
2. in the partitioning process, place all the numbers greater than this number to its right, and all the numbers smaller than or equal to it to its left.
3. repeat the second step for the left and right intervals until each interval has only one number.
II. python quick sorting code
The code is as follows:
#! /Usr/bin/python
#-*-Coding: UTF-8 -*-
Def sub_sort (array, low, high ):
Key = array [low]
While low
While low High-= 1
While low Array [low] = array [high]
Low + = 1
Array [high] = array [low]
Array [low] = key
Return low
Def quick_sort (array, low, high ):
If low
Key_index = sub_sort (array, low, high)
Quick_sort (array, low, key_index)
Quick_sort (array, key_index + 1, high)
If _ name _ = '_ main __':
Array = [, 3]
Print array
Quick_sort (array, 0, len (array)-1)
Print array
Result:
[8, 10, 9, 6, 4, 16, 5, 13, 26, 18, 2, 45, 34, 23, 1, 7, 3]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 16, 18, 23, 26, 34, 45]