Various implementations of sorting algorithms.
Each sorting algorithm is implemented in a separate module, and the directory includes a test script.
Algorithms:
- Time complexity: O(n^2)
- Memory complexity: O(1)
Bubble sort (sinking sort), is a simple sorting algorithm that repeatedly steps through the input nums
array element by element, comparing the current element with the one after it, swapping their values if needed. These passes through the nums
array are repeated until no swaps have to be performed during a pass, meaning that the array has become fully sorted. The algorithm, which is a comparison sort, is named for the way the larger elements "bubble" up to the top of the list.
Algorithm:
- Start from the first element (index 0) and compare it with the next element.
- If the current element is greater than the next element, swap them.
- Move to the next pair of elements and repeat the comparison and swapping process until you reach the end of the array.
- After the first iteration, the largest element will be at the end of the
nums
array. - Repeat steps 1-4 for the remaining elements until the entire
nums
array is sorted. - Continue this process until no more swaps are needed, indicating that the array is fully sorted.
- Time complexity: O(n^2)
- Memory complexity: O(1)
- Time complexity: O(n^2)
- Memory complexity: O(1)
- Time complexity: O(n log n)
- Memory complexity: O(n)
- Time complexity:
- Worst-case: O(n^2)
- Average: O(n log n)
- Memory complexity:
- Worst-case: O(log n)
- Average: O(n)
Quick sort is a highly efficient sorting algorithm. It is based on partitioning arrays into smaller sub-arrays. It works by selecting a pivot
element from the array and partitioning the other elements into two sub-arrays according to whether they are less than or greater than the pivot
. The sub-arrays are then recursively sorted. The key operation in quicksort is the partitioning process, which rearranges the elements around the pivot
. The efficiency of quick sort heavily depends on the choice of the pivot
element.
Algorithm:
- *Choose a
pivot
element from thenums
array. - Reorder the
nums
array so that all elements with values less than thepivot
come before it, and all elements with values greater than thepivot
come after it (equal values can go either way). After this partitioning, thepivot
is in its final position. - Recursively apply these steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.
- Continue partitioning and sorting the sub-arrays recursively until the entire
nums
array is sorted.
*Pivot selection methods:
- Leftmost of rightmost (first or last) element. Inefficient performance on already sorted or nearly sorted arrays.
- Random element. Choosing a random element from the array as the pivot helps in reducing the chance of worst-case scenarios. This approach is simple to implement and effective in practice.
- Time complexity: O(n log n)
- Memory complexity: O(1)
- Time complexity: O(n+k) (k - non-negatives range)
- Memory complexity: O(n+k)
- Time complexity: O(n * w) (n - number of keys, w - key length)
- Memory complexity: O(n + w)
bubble_sort.py
- Bubble sort algorithm.quicksort.py
- Quicksiort algorithm.test_sort_algorithms.py
- Test for different sort algorithms.
Torshin Sergey @torshin5ergey