Quick Sort Algorithm Analysis

Quick Sort Algorithm Analysis

insert image description here

Quick Sort

Quick sort is a classic divide-and-conquer algorithm, which recursively divides the array to be sorted into smaller sub-arrays, and then achieves the purpose of sorting through the selection of reference elements and the exchange of elements.

The core idea of ​​quick sort is to select the reference element and compare other elements with the reference element, put the ones smaller than the reference element on the left, and the ones larger than the reference element on the right, so that the position of the reference elements becomes orderly. Then recursively call quick sort on the sub-arrays on the left and right sides, so as to realize the sorting of the entire array.

Specific steps are as follows:

  1. Select reference element: select an element from the array to be sorted as the reference. Choosing an appropriate fiducial element can affect the efficiency of quicksort.
  2. Split operation: Divide the array into two parts, place the element less than or equal to the reference element on the left, and place the element greater than or equal to the reference element on the right. This step can be achieved by using the double pointer method or the digging method.
  3. Recursive sorting: Recursively call quick sorting for the divided left and right sub-arrays.
  4. Merge result: Merge the left sub-array, reference element and right sub-array to get the final sorting result.

The key to quicksort is the division operation, which places the reference element in the correct position by exchanging the positions of the elements multiple times. Through each round of division operation, the position of the benchmark element has been determined, the elements on the left are all less than or equal to the benchmark, and the elements on the right are all greater than or equal to the benchmark. Then recursively sort the left and right sub-arrays until the length of the sub-arrays is 1 or 0 and the recursion ends.

The advantage of quicksort is that its average time complexity is O(nlogn), and it has the characteristics of in-place sorting (in-place) without requiring additional space. However, the time complexity in the worst case is O(n^2). When the array to be sorted is already sorted or nearly sorted, the efficiency of quick sort will decrease. In order to avoid this situation, the reference element can be randomly selected or the middle of three numbers can be used to select the reference element to increase the stability and performance of the algorithm.

To sum up, quick sort is an efficient sorting algorithm, which is based on the idea of ​​divide and conquer, and realizes the sorting of arrays by selecting reference elements and dividing operations.

code example

Next, I will take Python code as an example to analyze several common sorting algorithms in detail. You can test the performance of the following types separately.

  1. Bubble Sort:
    Bubble sort is a simple and intuitive sorting algorithm that traverses the array to be sorted multiple times, comparing two adjacent elements each time and exchanging positions until the entire array is in order.
def bubble_sort(arr):
    n = len(arr)
    for i in range(n-1):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
    return arr
  1. Insertion sort (Insertion Sort):
    Insertion sort starts from the second element of the array, and inserts the following elements into the previously sorted part one by one until the entire array is in order.
def insertion_sort(arr):
    n = len(arr)
    for i in range(1, n):
        key = arr[i]
        j = i - 1
        while j >= 0 and arr[j] > key:
            arr[j+1] = arr[j]
            j -= 1
        arr[j+1] = key
    return arr
  1. Selection Sort:
    Selection sort finds the smallest element in the unsorted part each time and puts it at the end of the sorted part, repeating the process until the entire array is in order.
def selection_sort(arr):
    n = len(arr)
    for i in range(n):
        min_index = i
        for j in range(i+1, n):
            if arr[j] < arr[min_index]:
                min_index = j
        arr[i], arr[min_index] = arr[min_index], arr[i]
    return arr
  1. Quick sort (Quick Sort):
    Quick sort uses the divide and conquer method to quickly divide the array into two sub-arrays, one smaller than the reference element and one larger than the reference element, and then recursively sort the sub-arrays.
def quick_sort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr)//2]
    less = [x for x in arr if x < pivot]
    equal = [x for x in arr if x == pivot]
    greater = [x for x in arr if x > pivot]
    return quick_sort(less) + equal + quick_sort(greater)

These are the basic ideas and sample codes for several common sorting algorithms. It is worth noting that different sorting algorithms have different time complexity and space complexity characteristics, so in practical applications, it is necessary to choose the appropriate sorting algorithm according to the specific situation.

Guess you like

Origin blog.csdn.net/weixin_53742691/article/details/131713419