A must-see for interview questions! 5 sorting algorithms in Python and their implementation code

Sorting is a necessary knowledge and skill for every IT engineer and developer. Not only must pass the programming interview, but also understand the algorithm itself. Different sorting algorithms perfectly demonstrate how the algorithm design has such a big impact on the complexity, speed and efficiency of the program.

Let's take a look at the top 5 ranking algorithms, which are also the most common, often asked in interviews, and see how to implement them in Python!

 

1. Bubble sort

Bubble sorting is one of the most commonly taught in the CS introductory course because it clearly explains the working principle of sorting, while being simple and easy to understand. Bubble sort will step through the list and compare adjacent pairs of elements. If the elements are in the wrong order, they will be swapped. Repeat the traversal of the unsorted part of the list until the list is sorted. Because bubble sorting repeatedly passes through unsorted parts of the list, its worst-case complexity is O(n²).

 

2. Select Sort

Selection sort is also quite simple, better than bubble sort. If you want to choose between these two, it is best to use the default "right selection sort". Using selection sorting, we divide the input list/array into two parts: a sublist of sorted items and a sublist of remaining items that make up the rest of the list.

We first find the smallest element in the child list is not sorted, and place it at the end of the sorted sub-lists. Therefore, we continuously get the smallest unsorted element and put it in the sorted sublist in sorted order. This process will be repeated until the list is completely sorted.

 

3. Insertion sort

Insertion sort is faster than bubble sort and selection sort, and it can be said to be simpler. Just like when playing a card game, the process of shuffling is repeated insertion sort! In each iteration of the loop, insertion sort removes an element from the array. Then find the position of the element in another sorted array and insert it. It repeats this process until no input elements remain.

 

4. Merge sort

Merge sort is an example of a perfect divide and conquer algorithm. The use of this algorithm only requires the following two main steps:

  • (1) Continuously split the unsorted list until there are N sublists, where each sublist has 1 "unsorted" element, and N is the number of elements in the original array.
  • (2) Repeated merging, that is, merging two sub-lists together at once to generate a new sorted sub-list until all elements are completely merged into a sorted array.

 

5. Quick sort

Quick sort is also a divide and conquer algorithm, just like merge sort. Although it is a bit complicated, in most standard implementations, its execution speed is much faster than merge sort, and it rarely reaches the worst-case complexity of O(n²). It has three main steps:

  • (1) We first select an element from the array and call it pivot.
  • (2) Move all elements smaller than the axis to the left of the axis; move all elements larger than the axis to the right of the axis. This is called a partition operation.
  • (3) Recursively apply the above 2 steps to each sub-array of elements whose value is smaller or larger than the value of the previous axis. If you are interested in Python, you can add my WeChat: abb436574, and receive a set of learning materials and video courses for free~

Guess you like

Origin blog.csdn.net/weixin_45820912/article/details/108364883