Have you "heard" these classic sorting algorithms?

Hello everyone, welcome to Crossin's programming classroom!

Algorithms are an important part of the programming body of knowledge. After you have mastered some programming basics, you must understand algorithm-related knowledge in order to be able to write more efficient code. The sorting algorithm is a very basic content in the algorithm. Many interviewers also use sorting to examine the basic algorithmic ability of the interviewer.

Common sorting algorithms are: bubble sort, selection sort, insertion sort, quick sort, etc.

Beginners may have a little difficulty when they are just exposed to these algorithms. But if there are some intuitive visual demonstrations, it is very helpful to quickly understand and master the sorting algorithm.

We have implemented a visual sorting algorithm demonstration program in Python, and it can not only "see" but also "listen". Let me show you the execution process of five classic sorting algorithms: bubble sort, selection sort, insertion sort, quick sort, and merge sort.

Show results:

[Obsessive Compulsive Disorder Welfare] Have you "listened" to these classic sorting algorithms?

Remember to turn on your voice, there are little easter eggs at the end of the credits

a835b787f7eac22995d91733128fb592.gif

db7ae3fbca24858b1fb4959a73a6a977.gif

587799db9320332be58478f460cb8a33.gif

4ac858a9db1d948b5144e9deaaee7a6c.gif

0f12e2250a76ca6d5532ddda1bdb4306.gifThese are all classic sorting algorithms. Discussions about their implementation and algorithm complexity are mentioned in almost every algorithm book. You can also find countless articles by searching directly on the Internet, so this article will not repeat them here. A repeat. If you are interested in algorithms, you can tell them in the message area, and you will also consider writing some content about algorithms in the future.

The idea of ​​this program comes from a foreign video:

https://www.bilibili.com/video/av685670

Implementation principle:

The demo code mainly uses two libraries: matplotlib and  pyaudio .

matplotlib is a widely used Python charting library, here we use animation to achieve animation effects.

Core code snippet:

from matplotlib import animation


anis = animation.FuncAnimation(
    self.fig, 
    self._animate, 
    init_func=self._ini_animate,
    frames=self.frames, 
    interval=self.interval,
    repeat=False
)
# anis.save("a.gif", writer='imagemagick', fps=60, bitrate=-1)
plt.show()

FuncAnimation can generate chart-based animations. The main function is the second parameter, which needs to provide a function to specify the data of each frame in the animation.

The pyaudio library is used to implement sound effects during sorting.

Core code snippet:

import pyaudio
from makesound import play_tone


self.stream = self.audio.open(format=pyaudio.paFloat32,
            channels=1, rate=44100, output=1)
play_tone(self.stream, frequency=f)

The play_tone function plays tones of different frequencies according to the passed frequency parameter. In the program, the frequency is calculated from the corresponding value operated during sorting.

Instructions:

The fire library is also used in the program to realize the operation through the command line + parameters.

Run example:

$ python start.py sortex quick_sort

The second parameter is the sorting algorithm, and the corresponding value is:

Bubble sort - bubble_sort

Selection sort - select_sort

Insertion sort - insert_sort

Quick sort - quick_sort

Merge sort - merge_sort

In addition, it also supports custom parameters, which are used to set the original sequence of sorting, animation interval, etc.:

$ python start.py sortex quick_sort [1,3,5,4,2,6]
$ python start.py sortex quick_sort --interval=50

The complete code has been uploaded to github. If you are not familiar with sorting algorithms, you can try to modify the sorting data, execution speed and other parameters in the code, and observe the process and efficiency of different sorting algorithms, so as to understand these sorting algorithms. And if you already have a certain understanding of the algorithm, you can consider adding more algorithm demonstrations on this framework.

The main code and visualization of this program are implemented by @Provin.M.

Source code: snippet/algo.vi at master crossin/snippet GitHub


Crossin's new book "Action on Code: Learning PYTHON Programming from Zero Basics (CHATGPT Edition)" is now on the market.

This book strives to be easy to understand, so that zero-based "novice" who has no programming experience at all can learn Python. The content starts from the most basic steps of environment construction, and gradually goes deep into common practical applications. While explaining the knowledge points, it is equipped with corresponding code examples, so that readers can learn and practice to deepen their understanding.

The book covers Python environment construction, basic grammar, common data types, practical modules, regular expressions, object-oriented programming, multi-task programming and other knowledge points. In addition, three practical projects of crawler, GUI and game are provided.

The book also innovatively uses ChatGPT as an aid to programming learning, leading readers to explore a new mode of learning programming in the AI ​​era.

0c113ed197e6b853eb94efdae7f533a8.jpeg

Thank you for retweeting and liking ~

Guess you like

Origin blog.csdn.net/qq_40523737/article/details/131179734