{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example usage\n", "\n", "\n", "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!\n", "\n", "Curious about which sorting function performs best, and looking for concrete methods to compare different algorithms?\n", "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.\n", "\n", "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.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports\n", "First lets import all the functions we will need:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pysorting import (bubble_sort, quick_sort, insertion_sort, \n", " shell_sort, sorting_time, find_fastest_sorting_function, \n", " is_sorted)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sorting Functions\n", "\n", "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.\n", "\n", "In this example, we will be sorting a toy unsorted list:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Done sorting the array in order.\n", "[-69, -5, 1, 2, 3, 9, 34, 40]\n" ] } ], "source": [ "#first import the sorting function from the package\n", "from pysorting import bubble_sort\n", "\n", "# Sample toy list\n", "toy_list = [1, -5, 9, 40, 2, 3, -69, 34 ]\n", "\n", "#Call the function with the list and also specifying if you want the return in ascending or descending order\n", "new_list = bubble_sort(toy_list, ascending=True)\n", "\n", "#Prints out the result\n", "print(new_list)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting the run time of a Sorting Function\n", "\n", "Here we show you how to get the runtime of a sorting algorithm.\n", "\n", "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" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Function 'quick_sort' executed in 0.000018 seconds.\n", "Done sorting the array in order.\n", "Function 'insertion_sort' executed in 0.000012 seconds.\n" ] } ], "source": [ "from pysorting import quick_sort, insertion_sort, sorting_time\n", "\n", "toy_list_2 = [3,2.5,1,4,-100, 5]\n", "\n", "\n", "sort_time_quick = sorting_time(quick_sort, toy_list_2)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comparing Sorting algorithms\n", "In our next test example, we want to compare two different sorting algorithms to see which performs best.\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Done sorting the array in order.\n", "Function 'insertion_sort' executed in 0.000061 seconds.\n", "Function 'shell_sort' executed in 0.000007 seconds.\n", "╒════════════════╤═════════════╕\n", "│ Function │ Time take │\n", "╞════════════════╪═════════════╡\n", "│ insertion_sort │ 6.10352e-05 │\n", "├────────────────┼─────────────┤\n", "│ shell_sort │ 6.9141e-06 │\n", "╘════════════════╧═════════════╛\n", "\n", "\n", "The fastest function for sorting the above array is shell_sort\n" ] } ], "source": [ "from pysorting import insertion_sort, shell_sort, find_fastest_sorting_function\n", "\n", "\n", "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]\n", "\n", "\n", "fastest_func, time_taken = find_fastest_sorting_function(list_compare, insertion_sort, shell_sort)\n", "\n", "\n", "print(\"\\n\")\n", "\n", "print(f\"The fastest function for sorting the above array is {fastest_func.__name__}\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Checking that the list is properly sorted\n", "\n", "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.\n", "\n", "The function below returns true if the list is indeed sorted in the proper order:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pysorting import is_sorted\n", "\n", "sorted_list = [1,2,3,4,5,6,7,8,9]\n", "\n", "is_sorted(sorted_list, ascending=True)\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unsorted_list = [1,3,2,4,5,6,7,8,9]\n", "\n", "is_sorted(unsorted_list,ascending=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And thats it! Hopefully these functions help you in your future coding endeavours!" ] } ], "metadata": { "kernelspec": { "display_name": "pysorting", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.21" } }, "nbformat": 4, "nbformat_minor": 4 }