Javatpoint Logo
Javatpoint Logo

3D Visualisation of Quick Sort using Matplotlib in Python

A popular and effective sorting algorithm called Quick Sort uses a divide-and-conquer approach to organize the items in a list or array. Although Quick Sort is commonly implemented as a text-based algorithm, you may use Python's Matplotlib to visualize the method in three dimensions to better understand how it functions.

This visualization adds a third dimension to the conventional two-dimensional representations of the sorting process by allowing you to visualize it in three dimensions. An overview of how to do this is provided below:

By examining and comparing the number of operations required to compare and swap the pieces, visualizing algorithms helps one better comprehend them. Since 3D algorithm visualization is less frequent, we'll use Matplotlib to plot bar graphs and animate them to represent the array's members.

Data Representation: Create a 3D scatter plot to first depict your list or array. Each data point is a 3D coordinate, with the x-axis denoting the element's index, the y-axis denoting its value, and the z-axis initially set to a fixed value (for example, zero).

Partitioning: The primary operation of Quick Sort is partitioning, which involves choosing a pivot element and moving the other elements so that all of those smaller than the pivot are on one side and all of those larger than the pivot are on the other. Moving the pivot element up and down the y-axis as the data points are moved along the x-axis allows you to animate this process in your 3D visualization.

Recursion: Recursive algorithms are used in Quick Sort. Each recursive call that the algorithm makes can be visualized in your visualization as a separate 3D scatter plot as the program continues. This makes it easier to understand how the algorithm divides the problem into smaller problems.

Let's look at the Quick Sort 3D Visualisations:

Approach:

  • We'll create an array that contains random elements.
  • For reasons of visualization, the yield statement will be used in place of the return statement when the algorithm is applied to that array.
  • Following comparison and swapping, the array's current states will be produced. So, a generator object will be returned by the process.
  • We will illustrate comparing and switching of the array using Matplotlib animation.
  • The graph will then be plotted, returning a Poly3dCollection object for use in subsequent animation.

Implementation

Output:

enter array size: 5

3D Visualisation of Quick Sort using Matplotlib in Python





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA