Logo
Pattern

Discover published sets by community

Explore tens of thousands of sets crafted by our community.

Sorting Algorithms

10

Flashcards

0/10

Still learning
StarStarStarStar

Merge Sort

StarStarStarStar

Merge Sort is a divide and conquer algorithm that divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves. The merge function is key to the algorithm. Time complexity is O(nlogn)O(n \log n) in all cases, and space complexity is O(n)O(n) due to additional arrays used in the merge step.

StarStarStarStar

Heap Sort

StarStarStarStar

Builds a max heap from the input data, then repeatedly extracts the largest element from the heap and replaces it with the last item of the heap followed by readjusting the heap. This process is repeated until the heap is empty. Time complexity is O(nlogn)O(n \log n), and space complexity is O(1)O(1).

StarStarStarStar

Shell Sort

StarStarStarStar

An in-place comparison sort. It starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared. This method moves elements to the correct position faster than a simple nearest-neighbor exchange. Time complexity ranges from O(n)O(n) to O(n2)O(n^2) depending on the gap sequence, with a space complexity of O(1)O(1).

StarStarStarStar

Quick Sort

StarStarStarStar

Quick Sort picks an element as a pivot and partitions the array around the pivot. The pivot splits the array, with all elements less than the pivot to its left and all greater elements to its right. This partitioning is done recursively. Average time complexity is O(nlogn)O(n \log n), but in the worst case, it can be O(n2)O(n^2). Space complexity is O(logn)O(\log n) on average for the call stack, though it can be O(n)O(n) in the worst case.

StarStarStarStar

Insertion Sort

StarStarStarStar

Builds a sorted array one element at a time by repeatedly taking one element from the input data and inserting it into the correct position in the already-sorted list. Time complexity is O(n2)O(n^2) for the worst case, and O(n)O(n) for the best case when the data is already mostly sorted. Space complexity is O(1)O(1).

StarStarStarStar

Selection Sort

StarStarStarStar

It divides the input list into two parts: a sorted sublist of items which is built up from left to right at the front of the list, and a sublist of the remaining unsorted items. On each iteration, the smallest (or largest, depending on sorting order) element from the unsorted sublist is selected and moved to the sorted sublist. Time complexity is O(n2)O(n^2), and space complexity is O(1)O(1).

StarStarStarStar

Bubble Sort

StarStarStarStar

Repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed. Time complexity is O(n2)O(n^2), and space complexity is O(1)O(1).

StarStarStarStar

Radix Sort

StarStarStarStar

Non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. It uses counting sort as a subroutine to sort. Time complexity can be as low as O(nk)O(nk) where nn is the number of elements and kk is the digit length of the number. Space complexity is O(n+k)O(n+k).

StarStarStarStar

Counting Sort

StarStarStarStar

An integer sorting algorithm that counts the number of objects that have distinct key values, and uses arithmetic to determine the positions of each key value in the output sequence. Its time complexity is O(n+k)O(n+k) where nn is the number of elements in the input array and kk is the range of input. Space complexity is also O(n+k)O(n+k).

StarStarStarStar

Bucket Sort

StarStarStarStar

Scatters elements into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sort. Time complexity ranges from O(n)O(n) to O(n2)O(n^2) and largely depends on the distribution of the data. Space complexity is O(n+k)O(n+k), where kk is the number of buckets.

Know
0
Still learning
Click to flip
Know
0
Logo

© Hypatia.Tech. 2024 All rights reserved.