Sorting Algorithm: CSC 203 - Algorithms and Complexity

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

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:

• Simple implementation: Jon Bentley shows a three-line C version, and a five-line 


optimized version[1]
• Efficient for (quite) small data sets, much like other quadratic sorting algorithms
• More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms
such as selection sort or bubble sort
• Adaptive, i.e., efficient for data sets that are already substantially sorted: the 
time complexity is O(kn) when each element in the input is no more than k places
away from its sorted position
• Stable; i.e., does not change the relative order of elements with equal keys
• In-place; i.e., only requires a constant amount O(1) of additional memory space
• Online; i.e., can sort a list as it receives it
The insertion sort algorithm sorting a 30 element array.

Worst-case performance О(n2) comparisons and swaps


Best-case performance O(n) comparisons, O(1) swaps
Average performance О(n2) comparisons and swaps
Worst-case space complexity
Pseudocode for Insertion Sort
Example 2
In computer science, Merge sort is an efficient, general-purpose, comparison-
based sorting algorithm. Most implementations produce a stable sort, which
means that the order of equal elements is the same in the input and output. Merge
sort is a divide and conquer algorithm that was invented by John von Neumann in
1945.
Algorithm:
MergeSort(arr[], l, r) If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
Time – Space Complexity
Class Sorting algorithm
Data structure Array
Worst-case performance O(n log n)
Best-case performance O(n log n) typical, O(n) natural variant
Average performance O(n log n)
Worst-case space complexity О(n) total with O(n) auxiliary, O(1) auxiliary with linked
lists[
Pseudocode
Example 2.
Example 3.
Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm.
Developed by British computer scientist Tony Hoare in 1959[1] and published in 1961,[2] it is
still a commonly used algorithm for sorting. When implemented well, it can be about two
or three times faster than its main competitors, merge sort and heapsort.[3]

• Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from


the array and partitioning the other elements into two sub-arrays, according to whether
they are less than or greater than the pivot. The sub-arrays are then sorted recursively.
This can be done in-place, requiring small additional amounts of memory to perform the
sorting.

• 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…

You might also like