How to Create a Quicksort in Terminal using Python

In this tutorial, we will learn how to program Quicksort in Python and implement it in the terminal. The objective is to efficiently sort a list using the Quicksort algorithm. This tutorial will guide you through the process step by step, demonstrating different approaches to sorting arrays. By the end of this tutorial, you will have a solid understanding of how to implement and optimize Quicksort effectively in Python. So, let’s get started!

This topic is straightforward to understand. Just follow the instructions I provide, and you will complete it with ease. The program I will demonstrate will show you the correct and efficient way to implement Quicksort for sorting a list of arrays. By following the step-by-step process, you will gain a clear understanding of how the Quicksort algorithm works, including choosing a pivot, partitioning the array, and recursively sorting subarrays. So, let’s dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. def partition(array, low, high):
  2.  
  3.    
  4.     pivot = array[high]
  5.  
  6.    
  7.     i = low - 1
  8.     for j in range(low, high):
  9.         if array[j] <= pivot:
  10.             i = i + 1
  11.             (array[i], array[j]) = (array[j], array[i])
  12.  
  13.     (array[i + 1], array[high]) = (array[high], array[i + 1])
  14.     return i + 1
  15.  
  16.  
  17. def quickSort(array, low, high):
  18.     if low < high:
  19.         pi = partition(array, low, high)
  20.         quickSort(array, low, pi - 1)
  21.         quickSort(array, pi + 1, high)
  22.  
  23.  
  24. while True:
  25.     print("\n================== Create a Quicksort ==================\n")
  26.  
  27.     data = [1, 6, 3, 1, 10, 9, -3, -2]
  28.     print("Unsorted Array")
  29.     print(data)
  30.  
  31.     size = len(data)
  32.  
  33.     quickSort(data, 0, size - 1)
  34.  
  35.     print('\nSorted Array in Ascending Order:')
  36.     print(data)
  37.  
  38.     opt = input("\nDo you want to try again?(yes/no): ")
  39.        
  40.     if opt.lower() == 'yes':
  41.         ret=False
  42.     elif opt.lower() == 'no':
  43.         ret=True
  44.         print("Exiting program....")
  45.     else:
  46.         print("Please enter yes/no:")
  47.         break
  48.  
  49.     if ret == False:
  50.         continue

This program implements the QuickSort algorithm to sort an array in ascending order. It repeatedly selects a pivot element, partitions the array so that smaller elements are on the left and larger elements are on the right, and then recursively sorts the partitions. The program runs in a loop, displaying an unsorted array, sorting it using QuickSort, and asking the user if they want to repeat the process.

Output:

There you have it we successfully created How to Create a Quicksort in Terminal using Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment