Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the Visualization of sorting algorithm by python

Shulou Source: shulou.com Published: 2022-06-03 10:55:22 11月15日 Update

This article is about how python implements the visualization of sorting algorithms. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The main contents are as follows: 1. To realize the visualization of the classification algorithm, it is only necessary to color mark the change of each data.

2. Draw it as the data of a columnar image, empty the picture of the previous frame, cycle until the last ordered picture is displayed, and our sorting algorithm is completed in a visual loop.

You need a two-dimensional list (frames), where the elements are the list of data for each frame.

You also need to convert each data to class, with its own value properties and value properties, as well as the set_color () method.

Example

Def sort (li): # the elements in such an assigned frames list are lists one by one, and the data in each frame is an initial list assigned by the element frames = [li] ds = copy.deepcopy (li) # Used to store data after each frame change # Bubble sort: for i in range (32): for j in range (32-I-1): ds_r = copy.deepcopy (ds) # assign the data in a ds ds_ r [j] .set _ color ('# DC143C') # set the color of the data to'# DC143C' Ds_ r [j + 1] .set _ color ('# 7FFFAA') frames.append (ds_r) # appends the changed data of color to frmaes if DSS [j] .value > ds [j + 1] .value: ds [j + 1] Ds [j] = ds [j], ds [j + 1] frames.append (ds) # append the last sorted data to return frames. Thank you for reading! This is the end of this article on "how to achieve the visualization of sorting algorithms in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Data algorithms visualization sorting elements changes content attributes more pictures articles loops good practical loops just images examples that is articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information macOS OPPO Reno Linux MySQL