Sorting Flashcards
Selection Sort
- Simplest sort algorithm
- Continually swap smallest element into leftmost position
Steps
- Create outer loop over entire array
- Create inner loop starting from j = i
- Compare element at index j with every other element
- Swap smallest element into index j
Complexity
Time
Best ⇔ Average ⇔ Worst: O(n2)
Space
O(1)
Bubble Sort
- Very simple sorting algorithm that moves (bubbles) largest element to the end with each pass
Steps
- Create an loop of size n
- Create an inner loop starting from 0
- Compare element at index i with index i+1
- Swap if necessary
- Continue swapping until largest element is at far right
- Repeat n times with progressively smaller array (ignoring sorted values at end)
- Can exit early when no swaps are necessary on a pass
Complexity
Time
Best: O(n) ♦ Average: O(n2) ♦ Worst: O(n2)
Space
O(1)
Insertion Sort (Pairwise Swaps)
- Variation of insertion sort using pairwise swapping
Steps
- Divide list into sorted and unsorted by placing marker at index 1
- Start looping through unsorted items, checking if item is in right place
- If not, start inner loop
- Repeatedly pairwise swap up sorted portion until order is ok, or you reach start of array
- Return to main loop, proceed to next unsorted item
Complexity
Time
Best: O(n) ♦ Average: O(n2) ♦ Worst: O(n2)
Space
O(1)
Insertion Sort (Shifting)
- Variation of insertion sort using shifting
Steps
- Divide list into sorted and unsorted by placing marker at index 1
- Start looping through unsorted items, checking if item is in right place
- If not, vacate spot (store value in variable) and initiate an inner loop
- Shift element at i-1 into hole at i
- Check whether new hole (now at i-1) is correct insertion spot
- Continue shifting elements right until order is ok, or you reach start of array
- Copy element at marker to its new location
- Return to main loop, proceed to next unsorted item
Complexity
Time
Best: O(n) ♦ Average: O(n2) ♦ Worst: O(n2)
Space
O(1)
Shellsort
- Generalization of insertion sort
- Uses gaps to move elements multiple spaces at a time
Steps
- Determine size of gap using knuth’s sequence
- Apply rule h = 3h + 1, up to h < N
- Divide array ino 2 sections, “unsorted” and “sorted” (if you will) by starting loop from h (instead of 1)
- For each item i, compare with i - h, loop and swap as necessary
- Exit inner loop, and continue processing elements from i
- After each sweep, divide h by 3, using integer division
- Continue starting loop from new h until h == 1 (normal insertion sort)
Complexity
Time
Best: O(n log n) ♦ Worst: O(n3/2)
Space
O(1)
*Analysis is an open topic
*Analysis depends on gap sequence used; this assumes knuth’s sequence
Mergesort
- Comparison-based sorting algorithm
- Recursively implemented
- Utilizes divide and conquer paradigm
Steps
- If size of subarray is < 2, return (subarray is already “sorted”)
- Divide array into two halves
- Conquer by performing mergesort on each half
- Merge sorted halves
- Allocated new array aux
- Copy smaller element from each half into aux
- After copying, one array will have extra elements
- Loop through both arrays, copying remainder of elements into aux
- Set array = aux
Complexity
Time
Best ⇔ Average ⇔ Worst: O(n log n)
Space
O(n)
*Stack space is log(n) (depth of the recursive tree)
*Merge operation requires O(n) (in call stack, space for merge has not yet been allocated)
Quicksort
- Comparison-based sorting algorithm
- Uses divide & conquer paradigm
- In-place, unlike mergesort
Steps
- If size of subarray is < 2, return
- Partition array around a pivot
- Split array into two halves around pivot
- Run quicksort on each half (start - pivot-1) and (pivot+1 - end), ignoring pivot
Complexity
Time
Best: O(n log n) ♦ Average: O(n log n) ♦ Worst: O(n2)
Space
Best ⇔ Average: O(log n) ♦ Worst: O(n)
*n2 worst case occurs when array is sorted in reverse order, and partition operation continually divides array at beginning
*Requires constant (ie: no) aux space (in-place operation)
*Stack depth is log(n) in best and average case, but in worst case, array in each recursive call is only 1 smaller than previous, so height of tree is n (instead of log(n))
Parition
- Paritioning is a subprocedure of quicksort
- Partition achieves two things
- It reorganizes elements around pivot
- It returns pivot index
Steps
- Select a pivot “arbitrarily”, should select the end
- Place pivotIndex at start
- Iterate through arr from start to end - 1
- If element at i is <= pivot, swap with pivotIndex
- Increment pivotIndex
- After loop is finished, swap end with pivotIndex
- Return pivotIndex
Heapsort
- Comparison-based sorting algorithm that uses a heap
Steps
- Organize data into array
- Perform buildheap to transform array into max heap
- Iteravely delete max from heap
- But instead of decrementing array, store removed element in location vacated by swapped leaf
- Keep going until size of heap is 1 (already sorted)
Complexity
Time
Best ⇔ Average ⇔ Worst: O(n log n)
Space
O(1)
Bucket Sort
- Sorting algorithm that distributes elements into buckets of linked lists
- Similar to a hash table with separate chaining
Steps
- Initialize array of empty buckets
- Number of buckets is arbtrary
- Create some sort mapping of elements to buckets
- Each bucket can represent a key (0-9, etc.), a letter, or a range of values
- Loop through input array
- Add element from input to each bucket based on mapping
- Loop through bucket array, sorting linked list in each bucket
- Use insertion sort, or (less commonly) recursively use bucket sort
- Concatenate linked lists in each bucket while preserving their order
Complexity
Time
Best: O(n + k) ♦ Average: O(n + k) ♦ Worst: O(n2)
Space
O(n + k)
*k is the number of buckets
*n2 running time in worst case is because of insertion sort
Counting Sort
- Sorting algorithm used to sort elements (keys) within a specific range
- Elements in range are usually integers (0-9 in binary, or 0,1 in decimal) or letters (a-z), etc.
- Used to store individual values, not words (1991, ‘book’)
- Requires an input array, a count array and a temp array
Steps
- Create count array, a historgram of key frequencies
- Loop through input, counting number of times each key appears
- Compute the starting index of each key
- Loop through count array, setting count equal to sum of previous frequencies up to index - 1
- Copy elements from input array to temp array using indices
- Increment starting index in count array every time copy is made
- Return output
Complexity
Time
Best ⇔ Average ⇔ Worst: O(n + k)
Space
O(n + k)
*k is the number of keys
Radix Sort
- A sorting algorithm that sorts integers/strings by least significant digits/letters
- Elements in arrary should consist of letters/digits defined within some range (0-9, 0-1, a-z)
- Implemented with counting sort or (less commonly) bucket sort
Steps:
- Figure out length of longest word (element)
- Create loop of size word-length from least significant position (position d) up to position 0
- Iteraveley perform counting sort (or bucket sort) on input array at position d
Complexity
Time
Best: O(n) ♦ Average: O(w(n + k)) ♦ Worst: O(w(n + k))
Space
O(n + k)
*Complexity assumes counting sort
*Linear result requires some analysis, but involves minimizing running time by choosing “best k”
*w is the length of the word; k is the number of keys (size of “alphabet”)