Abstract:In programming, sorting is an important algorithm that helps us locate data faster and easier. In this article, we’ll use the sorting algorithm classifier to sort our arrays and see how they work.

This article is shared from the Huawei cloud community “Python sorting algorithm guide”, the original author: Tang Li.

In programming, sorting is an important algorithm that helps us locate data faster and easier. In this article, we’ll use the sorting algorithm classifier to sort our arrays and see how they work. In order to ensure the readability of this paper, only four sorting algorithms are introduced.

  • Bubble sort
  • Insert sort.
  • Merge sort.
  • Quick sort

Bubble sort

Bubble Sort is a simple sorting algorithm that compares the order of two adjacent objects, swapping the positions of adjacent objects that are not in the expected order. Here’s how it works:

  • Compare the first and second objects, and if the first is greater than the second, swap them.
  • The second object is compared to the third to check for the same condition. And so on until you get to the last number in the array.
  • Repeat this process so that the array is arranged from left to right, from smallest to largest.

The following code

For I in range(len(array)): def (array): For j in range(0, len(array) -i-1): if array[j] > array[j + 1]: Temp = array[j] array[j] = array[j+1] array[j+1] = temp = array[j+1] array[j+1] = temp = array[j+ 2] array[j+1] = temp = array[j+1] array[j+1] = temp = array[j+1] 1] bubbleSort(data) print('Sorted Array') print(data) #output: [1, 2, 3, 4, 5]

Insertion sort

Insertion sort is also very simple, it is divided into sorted and unsorted two parts, the unsorted part of the elements selected and correctly placed in the sorted part. It’s like a card game where we have sort cards. Here’s how it works:

  • Looked through the array to find the index of the lowest element and swapped it with the first element of the array.
  • Find the other lowest element in the array (excluding the first element), swap it with the second element, and repeat until the last element in the array.
  • In this way, the lowest element in the array will move to the left, and the largest element will move to the right, so the array is ordered.

The following code

InsertionSort (array) def insertionSort(array): for step in range(1, len(array)): While j >= 0 and key < array[j]: Array [j + 1] = array[j] j = j - 1 array[j + 1] = key data = [11, 4, 3, 2, 12] insertionSort(data) print("sorted array") print(data) #output: [2, 3, 4, 11, 12]

Merge sort

Merge sort is the most commonly used sorting algorithm based on the principle of divide and conquer algorithm. We split the array into parts, then sort them, and finally merge the subparts into a sorted array. To understand this, here’s how it works:

  • Break the array into small chunks until there are no individual elements in each chunk.
  • Compare each array, placing the minimum value on the left and the maximum value on the right.
  • If that’s hard to understand, check out this GIF.

The following code

Def mergeSort(array) if len(array) > 1: R = len(array)//2 L = array[:r] M = array[r:] mergeSort(L) I = j = k = 0 While I < len(L) and j < len(M): if L[I] < M[j]: if L[I] < M[j]: if L[I] < M[j]: Array [k] = L[I] I += 1 else: array[k] = M[j] j += 1 k += 1 array[k] = L[i] i += 1 k += 1 while j < len(M): array[k] = M[j] j += 1 k += 1 array = [8, 6, 14, 12, 10, 3] mergeSort(array) print("Sorted array: ") print(array) #output: [3, 6, 8, 10, 12, 14]

Quick sort

Like merge sort, quicksort is also a sort algorithm based on the principle of divide and conquer algorithm. It selects an element as a pivot and partitions the array around the pivot. Here’s how it works:

  • Choose a turning point, which can be randomly selected. Let’s say we choose the last element of the array as the pivot.
  • Put all items smaller than the axis on the left, and items larger than the axis on the right.
  • Repeat on the left and right sides of the pivot.
Def partition(array, lowest, highest) def partition(array, lowest, highest) For j in range(lowest, highest) = array[highest] # Set pointer to I = lowest - 1 if array[j] <= pivot: Array [I], array[I], array[I], array[I], array[I], array[I], array[I] Array [highest]) = (array[highest], array[I + 1]) = (array[highest], array[I + 1]) if lowest < highest: QuickSort (Array, lowest, highest) QuickSort (Array, PI + 1, Highest) Array = [9, 8, 3, 2, 1, 10, 7, 6, 19] size = len(array) quickSort(array, 0, size - 1) print('Sorted Array is below') print(array) #output [1, 2, 3, 6, 7, 8, 9, 10, 19]

That’s all for this article. Thank you for reading it. If it helps you, I hope to click “like” ~

The original address: https://python.plainenglish.i…

Click on the attention, the first time to understand Huawei cloud fresh technology ~