0% found this document useful (0 votes)
38 views

Lecture-04 05 - Sorting Techniques Analysis

This document discusses various sorting algorithms and analyzes their time complexities. It covers insertion sort, merge sort, and compares insertion sort to merge sort. Insertion sort has worst, average, and best case times of Θ(n2), Θ(n2), and Θ(n) respectively. Merge sort always has a time of Θ(n log n). For large inputs, merge sort is faster than insertion sort.

Uploaded by

灭霸
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Lecture-04 05 - Sorting Techniques Analysis

This document discusses various sorting algorithms and analyzes their time complexities. It covers insertion sort, merge sort, and compares insertion sort to merge sort. Insertion sort has worst, average, and best case times of Θ(n2), Θ(n2), and Θ(n) respectively. Merge sort always has a time of Θ(n log n). For large inputs, merge sort is faster than insertion sort.

Uploaded by

灭霸
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Algorithmics

CT065-3.5-3

Sorting Techniques & Analysis


Level 3 – Computing (Software Engineering)
Topic and Structure of the
Lesson
• Various sorting techniques commonly
used.
– Bubble Sort
– Insertion Sort
– Merge Sort
• Evaluation of the computation time of the
algorithms

Module Code and Module Title Title of Slides


Insertion Sort Analysis

Module Code and Module Title Title of Slides Slide 10 (of 26)
Insertion Sort Analysis
Worst case:
n
Input reverse sorted
T(n) = jΣ= 2Θ(j) = Θ(n2) [arithmetic series]

Averagen case: All permutations equally likely


T(n) = jΣ= 2Θ(j/2) = Θ(n2)

Best case:
n
Input already sorted
T(n) = jΣ= 2Θ(1) = Θ(n)

So, is insertion sort a fast sorting algorithm?


• Moderately so, for small n.
• Not at all, for large n.
Module Code and Module Title Title of Slides Slide 11 (of 26)
Merge Sort Analysis

T(n) MERGE-SORT (A, n)►A[1 . . n]


Θ(1) 1. If n = 1, done.

2T(n/2) 2. Recursively sort A[1.. n/2 ]


and A[ n/2 +1 . . n ].
Θ(n) 3. “Merge” the 2 sorted lists.
Takes linear time to merge sorted lists

Recurrence for Merge Sort


Θ(1), if n = 1; What are the upper
T(n) = bounds on merge
2T(n/2) + Θ(n) if n > 1. sort?
Module Code and Module Title Title of Slides Slide 12 (of 26)
Merge Sort Analysis

Module Code and Module Title Title of Slides Slide 13 (of 26)
Merge Sort vs. Insertion Sort
Worst case: Input reverse sorted
T(n) = Θ(n lg n)

Average case: All permutations equally likely


T(n) = Θ(n lg n)

Best case: Input already sorted


T(n) = Θ(n lg n)

So, is merge sort a faster sorting algorithm compared to


insertion sort?
• Moderately so, for small n.
• Yes, for large n (Θ(n lg n) vs. Θ(n2)) and definitely in the
average case
Module Code and Module Title Title of Slides Slide 14 (of 26)
Quick Review and Practice

• List the kinds of algorithmic analyses


• What are the types of asymptotic notation?

In the last lecture, we developed the pseudocode


for Selection Sort. Conduct an algorithmic
analysis of Selection Sort, complete with
asymptotic notation. Give the worst-case, average
case, and best case scenarios.
Module Code and Module Title Title of Slides Slide 15 (of 26)
Summary

• Various sorting techniques commonly


used.
– Bubble Sort
– Insertion Sort
– Merge Sort
• Evaluation of the computation time of the
algorithms

Module Code and Module Title Title of Slides


Next Lesson

• Introduction to graph theory


• Representation of graphs
• Discussion on traversal methods

Module Code and Module Title Title of Slides

You might also like