Python implementation bubble_ Select_ Insert_ Quick Sort Method

Keywords: Python Algorithm Permutation

Python implements multiple sorting methods

Bubble sort

  1. Overview and Principle
    Bubble Sort is a basic sorting algorithm.
    Bubble sorting compares two adjacent elements in the list in turn, replacing the order of the adjacent elements according to the sorting method.
    To reach the beginning of the list after a round of comparison, make the largest or smallest elements appear as "bubbles", and reach the order list after n rounds of comparison.

  2. Code implementation (ascending)

def bubble_sort(demo):
    for i in range(1, len(demo)):
    # i denotes the number of bubbling wheels
        for j in range(0,len(demo)-i):
        # j represents the element index for each comparison
            if demo[j]>demo[j+1]:
            # Swap when small index elements are larger than large index elements
                demo[j],demo[j+1] = demo[j+1],demo[j]
    print(demo)
test_list = [0,9,2,5,3,6,1]
bubble_sort(test_list)
[0, 1, 2, 3, 5, 6, 9]  # Output Order List
  1. Stability and time complexity
  • stability
    The sorting process does not exchange equal elements when there are equal elements in the list to be sorted, so bubble sorting is a stable sort method.
  • Time Complexity
    For the worst case of sorting, the list to be sorted is completely i n reverse order, which requires n-1 rounds of sorting, each round requires n-i exchanges, i.e., the average number of exchanges per round n/2, the time complexity is T(n)=n(n-1)/2, so the time complexity of bubble sorting is O(n^2);

Select Sort

  1. Overview and Principle
    Selection Sort is a basic sorting algorithm.
    Select Sort to find the largest or smallest element in the unsorted list and place it at the beginning of the list.
    Then select the largest or smallest element in the remaining unsorted list and place it at the beginning of the remaining list until it is sorted completely;

  2. Code implementation (ascending)

def select_sort(demo):
    for i in range(len(demo)-1):
    # i indicates the number of rounds to choose
        min_index = i
        # min_index represents the index that filters out the smallest elements
        for j in range(i+1,len(demo)): 
        # j represents the element index for each comparison
            if demo[j]<demo[min_index]:
            # When the index of the smallest element J to be compared is updated to j when it is smaller than the known minimum element
                min_index = j
        if min_index != i:
        # If the minimum element index changes after a round of comparison, the minimum element is swapped with the starting position element
            demo[i],demo[min_index] = demo[min_index],demo[i]
    print(demo)
test_list = [0,9,2,5,3,6,1]
select_sort(test_list)
[0, 1, 2, 3, 5, 6, 9]  # Output Order List
  1. Stability and time complexity
  • stability
    When there are equal elements in the list to be sorted, the sorting process exchanges the same elements, such as the list [5(0), 5(1), 1], and then changes to [1,5(1), 5(0)] after a round of selection sorting, so the selection sorting is unstable.
  • Time Complexity
    Selection sort requires n-1 rounds of sorting, and n-i exchange operations are needed i n each round, i.e. n/2 of average exchange times per round, then the time complexity is T(n)=n(n-1)/2, so the time complexity of selection sort is O(n^2);

Insert Sort

  1. Overview and Principle
    Insert Sort is a basic sorting algorithm.
    Insert Sort selects the first element in the unsorted list to compare with the sorted list element in turn, and substitutes it according to the sort until it is inserted in the appropriate place, that is, into the sorted list;
    Then insert the first element in the remaining unsorted list, and then complete the sorting of all elements.

  2. Code implementation (ascending)

def insert_sort(demo):
    for i in range(1,len(demo)):
    # i indicates the number of rounds to insert
        for j in range(i,0,-1): 
        # j means the index of the element to be compared, starting at the end of the sorted list
            if demo[j]<demo[j-1]:
                demo[j],demo[j-1] = demo[j-1],demo[j]
            else:
                break
            print(demo)
    print(demo)
test_list = [0,9,2,5,3,6,1]
insert_sort(test_list)
[0, 1, 2, 3, 5, 6, 9]  # Output Order List
  1. Stability and time complexity
  • stability
    When there are equal elements in the list to be sorted, the sorting process exchanges the same elements, such as the list [5', 5,1], and after a round of selection sorting becomes [1,5,5'], so the selection sorting is unstable.
  • Time Complexity
    Selection sort requires n-1 rounds of sorting, and n-i exchange operations are needed i n each round, i.e. n/2 of average exchange times per round, then the time complexity is T(n)=n(n-1)/2, so the time complexity of selection sort is O(n^2);

Quick Sort

  1. Overview and Principle
    Quick Sort is an improvement on the bubble sort algorithm.
    First, select an element in the unordered list as the base value element, and divide the list to be sorted into two parts through a round of sorting.
    All data in one part is smaller than the base value element, and all data in the other part is larger than the base value.
    Then the two parts of the unordered list are sorted rapidly by recursion idea, and the whole list is sorted in order.

  2. Code implementation (ascending)

def quick_sort(demo):
    if len(demo) > 1:  
    # Sort lists when they are longer than 1
        mid = demo[len(demo) // 2]
        # Select the base element, this example selects the middle value as the base element
        left, right = [], [] 
        # Initialize empty list on left and right sides of base element
        demo.remove(mid)
        # Remove base elements from the original list
        for i in demo:
        # Iterate through the list elements in turn, with values less than the base element in the left list and values greater than the base element in the right list
            left.append(i) if i < mid else right.append(i)
        return quick_sort(left) + [mid] + quick_sort(right)
        # Return to sorted list
    else:
        return demo
        # Recursive end condition is that the list to be sorted has only one element
test_list = [0,9,2,5,3,6,1]
test_list = quick_sort(test_list)
print(test_list)
[0, 1, 2, 3, 5, 6, 9]  # Output Order List
  1. Stability and time complexity
  • stability
    When there are equal elements in the list to be sorted, the sorting process exchanges the same elements, such as the list [6,5', 5,1,7], the base value 5, and the sorting becomes [1,5,5', 6,7], so the quick sorting is an unstable sorting method.
  • Time Complexity
    Quick sorting only correctly ranks one element at a time. The average number of swaps per round of sorting is n/2, and the time complexity is T(n)=n(n-1)/2, so the time complexity of quick sorting is O(n^2);

Posted by Scip on Mon, 18 Oct 2021 09:58:53 -0700