100% found this document useful (1 vote)
46 views19 pages

4 Lecture 2 Sorting

Uploaded by

Bilal Shabbir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
46 views19 pages

4 Lecture 2 Sorting

Uploaded by

Bilal Shabbir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Design and Analysis of Algorithm

FALL 2024

Lecture No:02(A)

Sorting Algorithms (Insertion Sort)


The Sorting Problem

 Input:

– A sequence of n numbers a1, a2, . . . , an

 Output:

– A permutation (reordering) a1’, a2’, . . . , an’ of the

input sequence such that a1’ ≤ a2’ ≤ · · · ≤ an’


Examples

 Sorting Books in Library (Dewey


system)
 Sorting Individuals by Height (Feet and
Inches)
 Sorting Movies in Blockbuster
(Alphabetical)
 Sorting Numbers (Sequential)
Why Study Sorting
Algorithms?

 Thereare a variety of situations that we


can encounter
– Do we have randomly ordered keys?
– Are all keys distinct?
– How large is the set of keys to be ordered?
– Need guaranteed performance?

 Various
algorithms are better suited to
some of these situations

4
Types of Sorting Algorithms

There are many, many different types of


sorting algorithms, but the primary ones
are:

Bubble Sort ●
Merge Sort

Selection ●
Quick Sort
Sort ●
Heap Sort

Insertion Sort
Some Definitions

 Internal Sort
– The data to be sorted is all stored in the
computer’s main memory.
 External Sort
– Some of the data to be sorted might be
stored in some external, slower, device.
 In Place Sort
– The amount of extra space required to sort
the data is constant with the input size.

6
Insertion Sort

 Idea: like sorting a hand of playing cards


– Start with an empty left hand and the cards
facing down on the table.
– Remove one card at a time from the table, and
insert it into the correct position in the left
hand
 compare it with each of the cards already in the
hand, from right to left
– The cards held in the left hand are sorted
 thesecards were originally the top cards of the pile
on the table

7
Insertion Sort

To insert 12, we need


to make room for it by
moving first 36 and
6 10 24 36 then 24.

12

8
Insertion Sort

6 10 24 36

12

9
Insertion Sort

6 10 24 3
6

12

10
Insertion Sort
input array
5 2 4 6 1
3
at each iteration, the array is divided in two sub-arrays:
left sub-array right sub-array

sorted unsorted

11
Insertion Sort

12
INSERTION-SORT
Alg.: INSERTION-SORT(A) 1 2 3 4 5 6 7 8

for i ← 2 to n a1 a2 a3 a4 a5 a6 a7 a8
do key ← A[ i ] key

Insert A[ i ] into the sorted sequence A[1 . . i -1]

j←i-1
while j > 0 and A[j] > key
do A[j + 1] ← A[j]
j←j–1
A[j + 1] ← key
13  Insertion sort – sorts the elements in place
14
Sorting Algorithms …….. Insertion
Sort
Statement Execution Cost
proc insertionSort(a as array)
n  |a| 1
for i  2 to n do n times
key  a[i] n-1 times
ji–1 n-1 times
while j > 0 and a[j] > 1 + 2 + 3 + 4 + …. + n times
key do
a[j+1]  a[j] 1 + 2 + 3 + 4 + …. +n-1
times
jj-1 1 + 2 + 3 + 4 + …. + n-1
times
next
15 a[j+1]  key n-1 times
Sorting Algorithms …….. Insertion
Sort

16
Insertion Sort - Summary

 Advantages
– Good running time for “almost sorted”
arrays (n)
 Disadvantages
 (n2) running time in worst and average
case
  n2/2 comparisons and exchanges

17
Summary

 If sorting algorithm using main memory only , it is


called internal sorting technique.
 If sorting algorithm using main memory as well
virtual memory , it is called external sorting
technique.
 If sorting technique using either main or virtual
and its demand for extra space remain constant
i.e. demand for space not change with increase or
decrease in input size.
 There are several sorting techniques such as
insertion, bubble, selection and quick sort merge
sort, heap sort etc.
18
In Next Lecturer

 Innext lecture, we will discuss about


bubble and selection sorting
techniques and its analysis

19

You might also like