Sorting Algorithms
Sorting Algorithms
Sorting
• Sorting is a process that organizes a collection of data
into either ascending or descending order.
• Formally
• Input: A sequence of n numbers <a1,a2,…,an>
• Output: A reordering <a1,a2,…,an> of the sequence such that
a1 ≤ a2 ≤ … ≤ an
• Given the input <6, 3, 1, 7>, the algorithm should produce
<1, 3, 6, 7>
• Called an instance of the problem
Sorting Algorithms
• We will learn:
– Selection Sort
– Insertion Sort
– Bubble Sort
– Merge Sort
– Quick Sort
These are among the most fundamental sorting
algorithms
Selection Sort
• Partition the input list into a sorted and unsorted
part (initially sorted part is empty)
• Select the smallest element and put it to the end of
the sorted part
• Increase the size of the sorted part by one
• Repeat this n-1 times to sort a list of n elements
Selection Sort (cont.)
SelectionSort(A, n) {
for i 0 to n-1
min i
for j i+1 to n
if (A[j] < A[min])
min j
swap(A[i], A[min])
}
swap( &lhs, &rhs )
{
tmp lhs
lhs rhs
rhs tmp
}
Sorted Unsorted
23 78 45 8 32 56 Original List
After pass 1
8 78 45 23 32 56
8 23 45 78 32 56 After pass 2
After pass 3
8 23 32 78 45 56
After pass 4
8 23 32 45 78 56
After pass 5
8 23 32 45 56 78
Selection Sort -- Analysis
• What is the complexity of selection sort?
• Does it have different best, average, and worst case
complexities?
Selection Sort – Analysis (cont.)
• Selection sort is O(n2) for all three cases (prove this)
• Therefore it is not very efficient
Insertion Sort
• Insertion sort is a simple sorting algorithm that is
appropriate for small inputs.
– Most common sorting technique used by card players.
• Again, the list is divided into two parts: sorted and
unsorted.
• In each pass, the first element of the unsorted part
is picked up, transferred to the sorted sublist, and
inserted at the appropriate place.
• A list of n elements will take at most n-1 passes to
sort the data.
Insertion Sort Algorithm
InsertionSort(A, n)
{
for i 1 to n
temp a[i]
for j i where j>0 and temp < a[j-1]
a[j] a[j-1] // shift elements
a[j] temp // insert
}
Insertion Sort
Sorted Unsorted
23 78 45 8 32 56 Original List
After pass 1
23 78 45 8 32 56
23 45 78 8 32 56 After pass 2
After pass 3
8 23 45 78 32 56
After pass 4
8 23 32 45 78 56
After pass 5
8 23 32 45 56 78
Insertion Sort – Analysis
• Running time depends on not only the size of the array but also
the contents of the array.
• Best-case: O(n)
– Array is already sorted in ascending order.
• Worst-case: O(n2)
– Array is in reverse order:
• Average-case: O(n2)
– We have to look at all possible initial data organizations.
Analysis of insertion sort
• Which running time will be used to characterize this
algorithm?
– Best, worst or average?
• Worst:
– Longest running time (this is the upper limit for the algorithm)
– It is guaranteed that the algorithm will not be worse than this.
• Sometimes we are interested in average case. But there are
some problems with the average case.
– It is difficult to figure out the average case. i.e. what is average
input?
– Are we going to assume all possible inputs are equally likely?
– In fact for most algorithms average case is same as the worst case.