Sorting Algorithm: CSC 203 - Algorithms and Complexity
Sorting Algorithm: CSC 203 - Algorithms and Complexity
Sorting Algorithm: CSC 203 - Algorithms and Complexity
Sorting Algorithm
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing
cards in your hands. The array is virtually split into a sorted and an unsorted part. Values
from the unsorted part are picked and placed at the correct position in the sorted part.
Algorithm
To sort an array of size n in ascending order:
1: Iterate from arr[1] to arr[n] over the array.
2: Compare the current element (key) to its predecessor.
3: If the key element is smaller than its predecessor, compare it to the elements before.
4.Move the greater elements one position up to make space for the swapped element
However, insertion sort provides several advantages:
• Quicksort is a comparison sort, meaning that it can sort items of any type for which a
"less-than" relation (formally, a total order) is defined. Efficient implementations of
Quicksort are not a stable sort, meaning that the relative order of equal sort items is not
preserved.
An animation of the quicksort algorithm sorting an array of randomized values. The red bars mark the pivot
element; at the start of the animation, the element farthest to the right hand side is chosen as the pivot.
Class Sorting algorithm
Worst-case performance O(n2)
Best-case performance O(n log n) (simple partition)
or O(n) (three-way partition and equal keys)
Average performance O(n log n)
Worst-case space complexity O(n) auxiliary (naive)
O(log n) auxiliary (Hoare 1962)
Pseudocode
Example 2
Example 3
Example 1.
Perform 1) Insertion 2) Merge 3) Quicksort to the given array below:
Y[20] = 99, 45, 3, -2, 55, 66, 1, 0, 5, 89, 99, 38, 24, 78, 2, 9, 8, 4, 79, 58, 33
THANKS…