QuickSort quick sort detailed analysis

QuickSort quick sort detailed analysis


Quick Sort CARHoare invention, although roughly the same idea (which is why we first introduced the median sort), but in fact really simple sort than the median.
In quick sort, we are no longer looking for value, but by some policies (and sometimes random, sometimes the left, sometimes middle) to select an element, the element array is cut into two sub-arrays . Fast row comprising two steps, as shown below. The central value of the first array is divided into two arrays, left subarray elements are equal to or less than the central value, the right sub-array elements are equal to or greater than the central value. Each sub-array is then sorted recursively.

 

 


Figure: quick sort Detailed

 

2 code implementation (python version)

 

# !/usr/bin/python
# -*- coding: UTF-8 -*-
# Auther: Dacheng
# date  :2020.4.4
import random


import time; # introduction time module

def random_int_list(start, stop, length):
  start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start))
  length = int(abs(length)) if length else 0
  random_list = []
  for i in range(length):
    random_list.append(random.randint(start, stop))
  return random_list

def QuickSort(arr):
    if len(arr) < 2:
        return arr
    # Select datum, which can pick, choose the middle of the ease of understanding
    mid = arr[len(arr) // 2]
    # Define two columns about the reference value
    left, right = [], []
    # Removed from the original array reference value
    arr.remove(mid)
    for item in arr:
    # Greater than the reference value put on the right
        if item >= mid:
            right.append(item)
        else: # smaller than the reference value put on the left
            left.append(item)
Compare iterative #
    return QuickSort(left) + [mid] + QuickSort(right)


starttime = time.time()
c=random_int_list(1,500,100)
endtime=time.time()

d=QuickSort(c)
print ( "quick sort running time:", endtime-starttime)
print ( "random array:", c)
print ( "quicksort the array:", d)



3. Run results

Quicksort Run time: 0.00033402442932128906
random array: [103, 28, 221, 424, 327, 374, 135, 246, 251, 408, 109, 74, 142, 157, 160, 182, 314, 354, 474, 140, 377, 96, 257, 82, 24, 459, 370, 205, 496, 287, 481, 135, 432, 159, 56, 281, 85, 209, 423, 4, 276, 109, 321, 21, 499, 114, 391, 4, 256, 482, 487, 298, 191, 330, 126, 296, 38, 228, 401, 72, 131, 171, 133, 350, 289, 400, 16, 423, 412, 108, 366, 398, 337, 308, 219, 314, 176, 339, 432, 178, 498, 343, 224, 272, 154, 160, 247, 177, 59, 426, 201, 135, 127, 311, 123, 356, 350, 47, 82]
Array after the quick sort: [4, 4, 16, 21, 24, 28, 38, 47, 56, 59, 72, 74, 82, 82, 85, 96, 103, 108, 109, 109, 114, 123 , 126, 127, 131, 133, 135, 135, 135, 140, 142, 154, 157, 159, 160, 160, 171, 176, 177, 178, 182, 191, 201, 205, 209, 219, 221 , 224, 228, 246, 247, 251, 256, 257, 272, 276, 281, 287, 289, 296, 298, 308, 311, 312, 314, 314, 321, 327, 330, 337, 339, 343 , 350, 350, 354, 356, 366, 370, 374, 377, 391, 398, 400, 401, 408, 412, 423, 423, 424, 426, 432, 432, 459, 474, 481, 482, 487 , 496, 498, 499]
 

 

 

Published 301 original articles · won praise 16 · views 30000 +

Guess you like

Origin blog.csdn.net/keny88888/article/details/105306157