Introduction to Sorting
CS-310: Analysis of Algorithm
Waheed Iqbal
Punjab University College of Information Technology (PUCIT)
University of the Punjab, Lahore, Pakistan.
Credit
These notes contain material from Chapter 2, Design and
Analysis of Algorithms by Cormen, Leiserson, Rivest,
and Stein (3rd Edition).
Socks Pairing Problem
Socks Pairing Problem (Cont.)
• Thing about applying Hashing to solve socks pairing
problem…..
Sorting Problem
Insertion Sort (Concept)
Insertion Sort (Algorithm)
Analysing Algorithm
• Analysing algorithm means predicting resources the
algorithm requires, for example:
• memory, communication bandwidth, CPU cycles etc
• RAM (random-access machine) model: generic one
process model which assumes instructions execute one
after another
• The RAM model contains instructions commonly found in real
computers: arithmetic, data movement (load, store, copy), and
control (conditional and unconditional branch, subroutine call and
return).
• Each such instruction takes a constant amount of time.
Insertion Sort (Analysis)
Insertion Sort (Analysis)
Running Time?
Insertion Sort (Analysis Cont.)
• What are the best and worst-case running times of
INSERTION-SORT?
• Lets try to solve T(n)for best case and worst case.
Insertion Sort (Analysis Cont.)
Insertion Sort Best Case Running Time
Insertion Sort Worst Case Running Time
Merge Sort (Introduction)
Divide-and-conquer based algorithm with O(nlogn) running
time complexity.
Divide: divide the input into number of subparts
Conquer: solve sub-problems recursively
Combine: combine the solution of sub problems into the
solution for the original problem
Merge Sort (Concept)
Merge Sort (Algorithm)
Merge Sort (Merge Call)
Merge Sort (Merge Call Simplified)
Source: Coursera course “Design and Analysis” course available at coursera by Tim Roughgarden, Stanford University.
Merge Sort Running Time Complexity
• Lets discuss how Merge Sort running time complexity is
O(nlogn)!
1. How many levels of this recursion tree have as a
function of n (size of input array)?
2. Running time complexity of merge call?