Happy New Year 2020, everyone. My name is ryuichi69.
I wrote this sentence today after practicing the output and explanation of the algorithm. To be honest, it is difficult to write in an easy-to-understand manner, and if there are any parts that are difficult to understand in the explanation, omissions in requirements, etc., please contact us. p>
First, sorting means sorting the elements of an array in ascending or descending order. p> There are many types of
sorting methods, but quicksort is to sort by subdividing into an array of larger ones and an array of smaller ones starting from the reference value of the array. It is a method to go b>. p>
Quicksort example h4>
For example, if you have an array a = [5,6,3,1,8], consider quicksorting it in ascending order. Furthermore, the median value in the array is used as the reference value. p>
Here, the median value of the elements of array a is 3. With this 3 as the boundary, the elements smaller than 3 are divided into two in half, such as blue below and the elements larger than 3 are yellow below. Furthermore, as shown in the second division in the figure below, the array is recursively divided in half, and the recursion is stopped when the number of elements in the array becomes one. Finally, it is completed by integrating into one array. p>
Here is a summary of the implementation procedure. P>
n be a natural number and i be an integer satisfying 0 ≤ i ≤ (n-1). At this time, sort the array a [i] with n elements in ascending order by quicksort. p>
a = [7,3,10,5,9,1,6,4,8,2]
[1,2,3,4,5,6,7,8,9,10] p>
The source code is @ suecharo's " Sort algorithm and implementation in Python --Qiita It is based on the quick sort of ". I didn't know how to sort the array in two, so I cheated. p>
quickSort.py
import os
import statistics
def quickSort(arr):
#An array of elements smaller than the median of the array
left = []
#An array of elements larger than the median of the array
right = []
#Recursion stop condition
#Stop when the number of elements is 1 or less after recursively dividing the array
if len(arr) <= 1:
return arr
#Median of array(Median)To get
median = int(statistics.median(arr))
#A variable for counting the number of medians contained in an array
medianFlg = 0
for element in arr:
#Array elements(element)Is less than the median
#Store the value in the array left
if element < median:
left.append(element)
#Array elements(element)Is greater than the median
#Store the value in the array right
elif element > median:
right.append(element)
else:
#If element is the median of the array
#Flag as many as the median value added to the return value+1 Continue
medianFlg += 1
#If you want to see how the array is divided,
#It is good to check with print at this timing
# print(left)
# print(right)
#Recursion is performed for each array left and array right
#Divide the array into smaller arrays
left = quickSort(left)
right = quickSort(right)
#Array left, median[median], Returns a concatenation of the array right
return left + [median] * medianFlg + right
#for test
if __name__ == "__main__":
print(quickSort([7,3,10,5,9,1,6,4,8,2]))
Recommended Posts