Skip to main content

A library for sorting algorithms.

Project description

ProfQ-Sorting

Usage

You can import the module like any other with:

import profq_sorting

Then you have access to all kinds of sorting algorithms.

For example, merge sort:

import profq_sorting

arr = [5, 2, 6, 1, 5]
sorted_arr = profqu_sorting.merge_sort(arr)

print(sorted_arr)
Output: [1, 2, 5, 5, 6]

There are many more algorithms which I have implemented, but here is a few that may interest you: mergesort, quicksort, radixsort and timsort.

Algorithms

Bubble Sort

from profq_sorting import bubble_sort

Bubble sort loops over the array, and for every iteration it loops over the array again, only offset by one. Then it checks every two elements and swaps them if they are out of order.

Bucket Sort

from profq_sorting import bucket_sort

My implementation for bucket sort can only handle values between 0 and 1. Bucket sort works by creating buckets for every tenth, then it categorizes each element by multiplying it by 10 to get the index. Then it sorts each bucket using insertion sort. Finally it appends all the buckets together to create one array that is sorted.

Counting Sort

from profq_sorting import counting_sort

Counting sort works by creating a table of each element in the array and then counts their occurrences. After that it does a cumulative sum to figure out what each elements index is. Then it loops over the array and each time an item comes up it adds it to the output array at the index calculated and then subtracts one from that index.

Insertion Sort

from profq_sorting import insertion_sort

Insertion sort divides the array into two parts: the sorted part and the unsorted part. The first array element begins as the first item in the sorted array. Then we just loop over the unsorted part of the array and insert each element at the right place in the sorted part.

Merge Sort

from profq_sorting import merge_sort

Merge sort splits the array into two parts through the middle, then it does so again and again until there is only one item left in each part. Then the algoritm merges the items back in the correct order until it reaches the sorted array.

Quick Sort

from profq_sorting import quick_sort

Quick sort starts with a pivot, it's an item that can be chosen in multiple ways as long as it's close to the median of the array. After being chosen every item to the left of the pivot should be less than it, and every item to the right should be greater. Then just repeat for the left side and the right side and continue doing so until you have reached a sorted array.

Radix Sort

from profq_sorting import radix_sort

Radix sort depends on counting sort to work. It basically sorts the array using the ones, then the tens and so on. It sorts these using the counting sort algorithm while keeping the order before.

Selection Sort

from profq_sorting import selection_sort

Selection sort works by looping over the array and with each iteration it will loop over the unsorted portion of the array. When it loops the first time it will select the minimum number in the array and put it first, then it just repeats this but without taking the sorted part into account.

Shell Sort

from profq_sorting import shell_sort

Shell sort works by sorting the items in an array with gaps in between, then it keeps decreasing these gaps until they are 0.

Timsort

from profq_sorting import timsort

This algorithm is really complicated and it took me a couple of days to wrap my head around so don't feel the need to understand it right away Gaurav Sen made a great series about Timsort and I highly suggest you to check it out.

What it does is it seperates the array into runs and sorts each run using insertion sort, then it merges the runs back into one big array using the merge function from merge sort. It's a hybrid sorting algortithm. And I know sounds really simple, but I can assure you it's not.

P.S. it's named timsort because Tim invented it in 2002 actually.

Conclusion

This is a package that I really only made so that I can say that I have made a Python package (can you believe it? I made a Python package!). And also to learn more about sorting algoritms! If you're interested in this topic there are a few Youtube channels/playlists I can recommend:

GeeksForGeeks has an awesome playlist detailing all kinds of sorting algoritms.

Michael Sambol also has a really cool series detailing sorting algorithms in a short amount of time.

It has been really fun learning about all these algorithms and I encourage you to try and implement some for yourself!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

profq_sorting-1.0.1.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

profq_sorting-1.0.1-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file profq_sorting-1.0.1.tar.gz.

File metadata

  • Download URL: profq_sorting-1.0.1.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.4

File hashes

Hashes for profq_sorting-1.0.1.tar.gz
Algorithm Hash digest
SHA256 8c5ab7d7e95a36727bf375ce406f6b1e9ae66403703490ef8216a6c408be87a0
MD5 f730e676ca5a114457ee8128f89cab06
BLAKE2b-256 031d5ecf37f14cf7f725a19ee21cfbccee530b97985ce9d565a66ad177c357f8

See more details on using hashes here.

File details

Details for the file profq_sorting-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: profq_sorting-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.4

File hashes

Hashes for profq_sorting-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d9c97f710fff19feac6b89690d2d1367ca4cdaa309d0cba58f855ef98e3b3d86
MD5 88115984ca3e1809dfee33a178bd3ff3
BLAKE2b-256 f9f4b79455af2dfc38e4f2924f218738910e1b88d1af1f965924a98d980d4cb7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page