BITS Pilani
CS F364: Design and Analysis of Algorithms

Introduction to Sorting & Asymptotic Analysis

Lecture 2 |2026-05-21

Introduction to Sorting

  • Introduction to Sorting
  • Asymptotic Analysis
  • Wrap-up

Takeaway from last class…

  • Algorithms are powerful!
  • Algorithm designer’s question: Can we do better?
  • interplay between rigor and intuition.

Introduction

Sorting

Re-arranging similar elements in a preferred order:

Either - increasing order or - decreasing order or - Lexicographic order

Bubble Sort-Description

Simplest sorting technique to arrange elements in the ascending order:

  • Start at the beginning: Compare the first two elements.
  • Compare and Swap: If the first element is greater than the second, swap them or leave them as they are.
  • Move to the next pair: Move to the next pair of elements and repeat the compare and swap operations.
  • Continue to the end of the list: Repeat the above step until you reach the end of the list.
  • Repeat: Go back to the beginning of the list and do the above four steps following the sequence.
  • Stop: Terminate when there is not a single swap operation in the current pass.

Bubble Sort-Pass-2

Bubble Sort-Pass-3

Bubble Sort-Pass-4

Bubble Sort-Pass-5

Bubble Sort-Algorithm-Implementation

Bubble Sort-Example

Example: 7, 4, 8, 2, 5, 3, 9

  • Pass-1: 4, 7, 2, 5, 3, 8, 9.
  • Pass-2: 4, 2, 5, 3, 7, 8, 9.
  • Pass-3: 2, 4, 3, 5, 7, 8, 9.
  • Pass-4: 2, 3, 4, 5, 7, 8, 9.
  • Pass-5: 2, 3, 4, 5, 7, 8, 9.

Bubble Sort-Time complexity

  • Outer loop iterates n times.
  • At i^{th} iteration of the outer loop, inner loop iterates at most n-i times.
  • Overall time complexity in the worst case is 1+2+3+...+(n-1) = O(n^2).

Insertion Sort-Description

  1. Start with the first element: Consider the first element as a sorted sublist.
  2. Select the next element: Take the next element in the list.
  3. Find the correct position: Compare this element with the elements in the sorted sublist from right to left.
  4. Shift elements: Shift all elements in the sorted sublist that are greater than the current element one position to the right.
  5. Insert the element: Insert the current element into its correct position in the sorted sublist.
  6. Repeat: Repeat steps 2 to 5 for each of the remaining elements in the list.

Insertion Sort

Example: 4 3 2 10 12 1 5 6

Insertion Sort

Example: 4 3 2 10 12 1 5 6

Insertion Sort-Algorithm-Implementation

Insertion sort-time complexity

  • n iterations in the outer loop.
  • around n iterations in the inner loop.

Overall time complexity is around n^2.

Can we do better?

  • MergeSort: A divide-and-conquer approach.
  • Recall the following picture from the last time:

Mergesort

Mergesort

MERGE SORT ( A, p, r)

  • if p<r then
    • q=\lfloor(p+r)/2\rfloor
    • MERGE SORT ( A, p, q)
    • MERGE SORT ( A, q + 1, r)
    • create subarrays S_1 and S_2
    • Merge (S_1,S_2,A)

Time complexity of MergeSort is around n\log{n}.
We will come back to the analysis and correctness proof little later!

n\log{n} vs n^2

Asymptotic Analysis

  • Introduction to Sorting
  • Asymptotic Analysis
  • Wrap-up

Introduction - Asymptotic analysis

  • The order of growth of the running time of an algorithm gives a simple characterization of the algorithm’s efficiency.
  • Asymptotic efficiency: We are concerned with how the running time of an algorithm increases with the size of the input in the limit, as the size of the input inreases without bound.
  • An algorithm is asymptotically more efficient will be the best choice for all but very small inputs.
  • Asymptotic running time of an algorithm are defined in terms of functions whose domains are the set of natural numbers N=\{0, 1, 2, \ldots\}. Such notations are convenient for describing the worst-case running-time funcion T(n), which is usually defined only on integer input sizes.

Big Oh

O-notation

For a given function g(n), we denote by O(g(n)) the set of functions
O(g(n)) = \{f(n) : \text{there exists positive constants } c \text{ and } n_0 \text{ such that } 0\leq f(n)\leq cg(n) \text{ for all } n\geq n_0\}.

  • We use O-notation when we have only an asymptotic upper bound.

Big Oh Examples

  • 3n^2-100n+10 = O(n^2) because 3n^2 > 3n^2-100n+10.
  • 3n^2-100n+10 = O(n^3) because 0.01n^3 > 3n^2-100n+10.
  • 3n^2-100n+10\neq O(n) because c\cdot n < 3n^2 when n>c.

Big Omega

\Omega-notation

For a given function g(n), we denote by \Omega(g(n)) the set of functions
\Omega(g(n)) = \{f(n) : \text{there exists positive constants } c \text{ and } n_0 \text{ such that } 0\leq cg(n)\leq f(n) \text{ for all } n\geq n_0\}.

  • We use \Omega-notation when we have only an asymptotic lower bound.

Big Omega Examples

  • 3n^2-100n+10 = \Omega(n^2) because 2.99n^2 < 3n^2-100n+10.
  • 3n^2-100n+10\neq\Omega(n^3) because 3n^2-100n+10 < n^3.
  • 3n^2-100n+10 = \Omega(n) because 10^{10^{10}}n < 3n^2-100n+10.

Big Theta

\Theta-notation

For a given function g(n), we denote by \Theta(g(n)) the set of functions
\Theta(g(n)) = \{f(n) : \text{there exists positive constants } c_1, c_2, \text{ and } n_0 \text{ such that } 0\leq c_1g(n)\leq f(n)\leq c_2g(n) \text{ for all } n\geq n_0\}.

  • The \Theta-notation asymptotically bounds a function from above and below.

Big Theta Example

  • 3n^2-100n+10 = \Theta(n^2) because of O and \Omega.
  • 3n^2-100n+10\neq\Theta(n^3) because of O only.
  • 3n^2-100n+10\neq\Theta(n) because of \Omega only.

Wrap-up

  • Introduction to Sorting
  • Asymptotic Analysis
  • Wrap-up

Discussion Time!

Thank You!

Let’s discuss questions, clarifications, or any topics from today’s lecture.