Sort Algorithms
Merge
# of divisions and # of merges = n-1
Bubble
uses a nested loop to compare neighboring items and make swaps as neighbors are out of natural order
Merge
uses twice as much memory due to temporary array storage
Merge Sort
A recursive algorithm that continually splits a list in half, until each half is either empty or one item, at which point the two halves are merged together in natural order, back up the division process until the entire list has been sorted.
Bubble Sort
A simple (and relatively slow) sorting algorithm that repeatedly loops through a list of values, comparing adjacent values and swapping them if they are in the wrong order.
Selection Sort
A sorting routine that uses a nested loop process to systematically select the best value among the unsorted elements of the array for the next position in the array, starting with position zero all the way to the end.
Insertion Sort
A type of sort that uses a nested loop process to systematically find the best place in the current array for an unsorted item.
Sort
An algorithm used to arrange elements of an array into natural order, such as numeric or alphabetical.
Quick Sort
An efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. Sometimes called partition-exchange sort.
Quick Sort
Best: O (N log N) stays balanced Avg: O (N log N) stays fairly balanced Worst: O (N^2) all values on one side
Bubble
Best: O (N) list already in order (1 pass) Avg: O (N^2) random Worst: O (N^2) reverse
Insertion
Best: O (N) list already in order (3 steps each pass) Avg: O (N^2) random Worst: O (N^2) reverse
Selection
Best: O (N^2) list in order Avg: O (N^2) random Worst: O (N^2) reverse
Bubble
K goes thru list comparing adjacent and swapping as needed, telling Joker to lie down (false) each time a swap is performed
Insertion
K looks down the list and shifts the values up the list until every value has found its best position
Insertion
K starts on 2nd, checks to see if J position better for K value
Quick Sort
L and R look at their position values until swap, then move inwards 1 step until they cross
Quick Sort
L and R markers check if their position's value is lesser or greater than pivot
Quick Sort
L and/or R markers move until find position/value that is incorrectly lesser/greater than pivot, and they swap
Merge
O (N log N) - O (N log N) - O (N log N)
Quick Sort
O (N log N) - O (N log N) - O (N log N)
Bubble
O (N) - O (N^2) - O (N^2)
Insertion
O (N) - O (N^2) - O (N^2)
Selection
O (N^2) - O (N^2) - O (N^2)
Swap
The process in sort routines of exchanging two values in an array, using a temporary variable, taking three steps to execute.
Bubble
volatile, simple and slow, best to verify sorted O (N), worst when random or reversed O (N^2)
Merge
a divide and conquer approach is used to break apart the list and sort smaller portions of it
Insertion
additional values are inserted each pass into a portion of the list that maintains a sorted order
Bubble
after each pass, the next highest unsorted value should be in the correct position
Bubble
compare adjacent values, and if they are out of order, swap them
Insertion
compare places all the way back to the beginning, shifting cards to the right until find the best place for the value we are seeking
Selection
compares each to whole rest of list, until gets a clear pass
Merge
divide and conquer a list, merging portions of the list in sorted order
Quick Sort
divide elements into sublists sorted around a pivot value
Merge
divides elements into sublists and then reassembles them into sorted order
Merge
divides lists into sublists of only one element before merging them back together into one sorted list
Selection
find the best (lowest) value, swap, J/Q/K
Selection
find the next smallest value in the remaining unsorted elements
Selection
finds the best value for the current position with each pass
Selection
goes thru whole list on each pass finding best (lowest value) for each index (next smallest value is always swapped with current index)
Insertion
inserts value to best position
Insertion
lower values sorted first (inserted to the left)
Quick Sort
moves lesser values to left of pivot, greater values to right of pivot, at which point, pivot value is in correct place
Insertion
nested loop to insert each unsorted item in a list into its proper position
Merge
no sorting or decision making about ordering is done until the division process is done and merge process takes over
Quick Sort
partition, swap
Quick Sort
partitions lists and swaps values on either side of a pivot value
Merge
recursive, divide in half, then merge
Insertion
relatively stable, best to verify sorted O (N), worst when random or reversed O (N^2)
Insertion
saves a copy (temp) instead of swap function
Insertion
select the next best (lower) value and insert them into a sorted sublist
Selection
select the next best value of a list and swap it into a portion of the list that has been sorted
Selection
selects the next "best" value and swaps it inot its correct position with each pass
Merge
situation of data doesn't matter (random, reverse), it works the same in any order O (N log N)
Selection
stable but slow, Always O (N^2), quadratic
Insertion
start on 2nd, save temp, shift to right
Quick Sort
swap elements on either side of the pivot that are on the wrong side
Bubble
swap, adjacent, highest, Joker
Merge
uses a divide and conquer recursive approach, repeatedly dividing the list in half until it is looking at only two individual items, then merges them back into one sorted list, working back up through each division, merging each pair of sorted sections together into the larger section
Selection
uses a nested loop to "select" the best data for a particular position, then swap the best data with the value in that place
Quick Sort
when L and R markers cross and partitions are of single value, then the current halves are sorted
Quick Sort
when L and R markers meet, they swap out the pivot