Please enable JavaScript.
Coggle requires JavaScript to display documents.
Lecture 5 & 6 - Sorting Algorithms (Bubble Sort (IMPROVED BUBBLE SORT,…
Lecture 5 & 6 - Sorting Algorithms
Arranging an unordered collection into an ordered one
Insertion Sort
Like deck of cards, place card to correct position in already holding cards
consider elements one at a time, place element in correct position by moving larger elements to the right
(N^2)/4 = O(N^2)
efficient if input is almost sorted
Bubble Sort
Bubble largest value to end using pair-wise comparisons and swapping
Move from front to end
Wastes time imparting some order to the unsorted part of array
N + N/2 + k = O(N^2)
IMPROVED BUBBLE SORT
add a boolean flag if no swaps have been made then stop algorithm
Selection Sort
Find largest/smallest element in array and swap to last/first location of array. Repeat
N-1 * N/2 = O(N^2)
Wastes most time looking for smallest element in array
Algo said to be stable if the duplicate keys stay in the same order after sorting has been completed
DIVIDE AND CONQUER ALGO
Merge Sort
Divide array in half , merge sort recursively other parts and merge back for a sorted array
very efficient with respect to time
O (n logn)
But requires array size of original array
Not an in-place algo
Quick Sort
-Recursive
less comparisons than elementary sorts
choose pivot element
IS a[i] < v? If yes then increment i, else STOP
is j>i AND a[j]>=4? If yes decrement j, else swap a[i], a[j]