Sorting Algorithms: Bubble Sort, Quick Sort, Selection Sort

1. Bubble Sort

        Bubble sort is a simple sorting algorithm. It iteratively walks through the array to be sorted, comparing two elements at a time and swapping them if they are in the wrong order. The work of visiting the sequence is repeated until there is no need to exchange, that is to say, the sequence has been sorted. The name of this algorithm comes from the fact that the smaller elements will slowly "float" to the top of the sequence through exchange.

1.1 Algorithm description

  • Compare adjacent elements. If the first is greater than the second, swap them both;
  • Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end, so the element at the end should be the largest number;
  • Repeat the above steps for all elements except the last one;
  • Repeat steps 1~3 until sorting is complete.

1.2 Code implementation

# while循环
def bubblesort(a):
    long=len(a)
    while long>0:
        for i in range(long-1):
            k=a[i+1]
            if k<a[i]:
                a[i],a[i+1]=k,a[i]
        long-=1
    return a


# for循环
def bubblesort(a):
    long=len(a)
    for j in range(-long,0):
        for i in range(long-1):
            k=a[i+1]
            if k<a[i]:
                a[i],a[i+1]=k,a[i]
    return a

2. Quick Sort

        The basic idea of ​​quick sorting: the records to be sorted are separated into two independent parts by one-pass sorting, and the keywords of one part of the records are smaller than the keywords of the other part, then the two parts of the records can be sorted separately to achieve The whole sequence is in order.

2.1 Algorithm description

Quicksort uses a divide-and-conquer method to divide a string (list) into two sub-strings (sub-lists). The specific algorithm is described as follows:

  • Pick an element from the sequence, called the "pivot" (pivot);
  • Reorder the sequence, all elements smaller than the reference value are placed in front of the reference value, and all elements larger than the reference value are placed behind the reference value (the same number can go to either side). After this partition exits, the benchmark is in the middle of the sequence. This is called a partition operation;
  • Recursively sort subarrays with elements less than the base value and subarrays with elements greater than the base value.

2.2 Code implementation 

def quicksort(a):
    if len(a)<2:
        return a
    else:
        base=a[0]
        left=[i for i in a[1::] if i<base]
        right=[i for i in a[1::] if i>base]
        return quicksort(left)+[base]+quicksort(right)

 3. Select Sort

        Selection sort (Selection-sort) is a simple and intuitive sorting algorithm. How it works: first find the smallest (largest) element in the unsorted sequence, store it at the beginning of the sorted sequence, then continue to find the smallest (largest) element from the remaining unsorted elements, and then put it in the sorted sequence end of . And so on until all elements are sorted.

3.1 Algorithm description

The direct selection and sorting of n records can obtain ordered results through n-1 direct selection and sorting. The specific algorithm is described as follows:

  • Initial state: the unordered area is R[1..n], and the ordered area is empty;
  • When the i-th sorting (i=1,2,3...n-1) starts, the current ordered area and unordered area are R[1..i-1] and R(i..n) respectively. This sorting process selects the record R[k] with the smallest key from the current unordered area, and exchanges it with the first record R in the unordered area, so that R[1..i] and R[i+1 ..n) respectively become a new ordered area with the number of records increased by 1 and a new disordered area with the number of records decreased by 1;
  • The n-1 trip is over, and the array is sorted.

3.2 Code implementation 

def selectionsort(a):
    leng=len(a)
    for i in range(leng):
        min=i
        for j in range(i+1,leng):
            if a[j]<a[min]:
                min=j
        a[i],a[min]=a[min],a[i]
    return a

Guess you like

Origin blog.csdn.net/qq_44954371/article/details/126491683