Example usage

Picture this: you are on a project or building a pipeline, and come across a list or dataframe column that needs to be sorted. You want to find the most computationally efficient way to sort this array, but the data structure and algorithms class you took in university is years behind you. Don’t know which sorting algorithm has the lowest time complexity for your specific use case? Pysorting has you covered! Our package contains all the main sorting function algortihms you might need right at your fingertip. Certain lists will be sorted quicker according to various algorithms, so import our package and start sorting away!

Curious about which sorting function performs best, and looking for concrete methods to compare different algorithms? Again, pysorting is here to help! We have a utlils function that lets you measure the speed of any sorting algorithm and give you the fastest for you specific use case. Want to skip the middle man, and looking to simply obtain the most efficient algorithm? Our package also lets you compare any two algorithms and will return the best one.

In these tutorials we will show you how to call the different sorting functions attached to the package, as well as the utility functions included.

Imports

First lets import all the functions we will need:

from pysorting import (bubble_sort, quick_sort, insertion_sort, 
                       shell_sort, sorting_time, find_fastest_sorting_function, 
                       is_sorted)

Sorting Functions

In this first part, we show an example on how to use one of our sorting functions. All functions follow the same convention, so this method is applicable to all sorting algorithms.

In this example, we will be sorting a toy unsorted list:

#first import the sorting function from the package
from pysorting import bubble_sort

# Sample toy list
toy_list = [1, -5, 9, 40, 2, 3, -69, 34 ]

#Call the function with the list and also specifying if you want the return in ascending or descending order
new_list = bubble_sort(toy_list, ascending=True)

#Prints out the result
print(new_list)
Done sorting the array in order.
[-69, -5, 1, 2, 3, 9, 34, 40]

Getting the run time of a Sorting Function

Here we show you how to get the runtime of a sorting algorithm.

Generally, the methodology is to pass one of the sorting functions into the sorting_time() function. The output will be the time taken to sort the list

from pysorting import quick_sort, insertion_sort, sorting_time

toy_list_2 = [3,2.5,1,4,-100, 5]


sort_time_quick = sorting_time(quick_sort, toy_list_2)
Function 'quick_sort' executed in 0.000024 seconds.

Comparing Sorting algorithms

In our next test example, we want to compare two different sorting algorithms to see which performs best.

The methodology is to pass both sorting functions, as well as the data we wish to use in comparing them into the find_fastest_sorting_function() function. The output will be a the name of the fastest algorithm, along with the time it took to sort the list.

from pysorting import insertion_sort, shell_sort, find_fastest_sorting_function


list_compare = [1, 2, 3, 5, 6, 4, 7, 8, 9, 11, 12, 10, 13, 14, 15, 16, 18, 17, 19, 20, 21, 22, 23, 24, 25]


fastest_func, time_taken = find_fastest_sorting_function(list_compare, insertion_sort, shell_sort)


print("\n")

print(f"The fastest function for sorting the above array is {fastest_func.__name__}")
Done sorting the array in order.
Function 'insertion_sort' executed in 0.000061 seconds.
Done sorting the array in ascending order.
Function 'shell_sort' executed in 0.000028 seconds.
╒════════════════╤═════════════╕
│ Function       │   Time take │
╞════════════════╪═════════════╡
│ insertion_sort │ 6.05583e-05 │
├────────────────┼─────────────┤
│ shell_sort     │ 2.7895e-05  │
╘════════════════╧═════════════╛


The fastest function for sorting the above array is shell_sort

Checking that the list is properly sorted

As our final use case, if you were to come across an overly long list to be sorted. You pass this list through a sorting algorithm, and now want some peace of mind that the returned list is in a proper order.

The function below returns true if the list is indeed sorted in the proper order:

from pysorting import is_sorted

sorted_list = [1,2,3,4,5,6,7,8,9]

is_sorted(sorted_list, ascending=True)
True
unsorted_list = [1,3,2,4,5,6,7,8,9]

is_sorted(unsorted_list,ascending=True)
False

And thats it! Hopefully these functions help you in your future coding endeavours!