Insertion Sort - Lecture Slides | CSCE 350, Study notes of Computer Science

Material Type: Notes; Professor: Huang; Class: DATA STRUCTR&ALGORITHMS; Subject: Computer Science & Engineering; University: University of South Carolina - Columbia; Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 09/02/2009

koofers-user-6pg
koofers-user-6pg 🇺🇸

8 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CSCE 350:
Data Structures and Algorithms
Chin-Tser Huang
University of South Carolina
02/27/2009 2
Announcement
Reading assignment: Chapter 5.2
Homework 3 is returned today
High 4, Average 3.35
Homework 4 is assigned on February 25 and
is due on Wednesday, March 4 in class
Please download it from class website
Have your answers neatly typed
Explain your answer clearly and adequately by
showing the steps
02/27/2009 3
Variable-size-decrease
A size reduction pattern varies from one iteration of an
algorithm to another
Example: Euclid’s algorithm for computing the greatest
common divisor
gcd(
m
,
n
) = gcd(
n
,
m
mod
n
)
The arguments on the right-hand side are always
smaller than those on the left-hand side
But they are not smaller neither by a constant nor by a
constant factor
02/27/2009 4
Insertion Sort
This is a typical decrease-by-one technique
Assume
A
[0..
i-
1] has been sorted, how to
achieve the sorted
A
[0..
i
]?
Solution: insert the last element
A
[
i
] to the right
position
02/27/2009 5
Example
Sort 89 45 68 90 29 34 17
02/27/2009 6
Pseudocode of Insertion Sort
pf2

Partial preview of the text

Download Insertion Sort - Lecture Slides | CSCE 350 and more Study notes Computer Science in PDF only on Docsity!

CSCE 350:

Data Structures and Algorithms

Chin-Tser Huang

[email protected]

University of South Carolina

02/27/2009 2

Announcement

„ Reading assignment: Chapter 5.

„ Homework 3 is returned today

„ High 4, Average 3.

„ Homework 4 is assigned on February 25 and

is due on Wednesday, March 4 in class

„ Please download it from class website „ Have your answers neatly typed „ Explain your answer clearly and adequately by showing the steps

02/27/2009 3

Variable-size-decrease

„ A size reduction pattern varies from one iteration of an algorithm to another

„ Example: Euclid’s algorithm for computing the greatest common divisor

gcd(m,n) = gcd(n,m modn)

„ The arguments on the right-hand side are always smaller than those on the left-hand side „ But they are not smaller neither by a constant nor by a constant factor

02/27/2009 4

Insertion Sort

„ This is a typical decrease-by-one technique

„ AssumeA[0..i-1] has been sorted, how to

achieve the sortedA[0..i]?

„ Solution: insert the last elementA[i] to the right

position

02/27/2009 5

Example

„ Sort 89 45 68 90 29 34 17

02/27/2009 6

Pseudocode of Insertion Sort

02/27/2009 7

Analysis of Insertion Sort

„ Time efficiency

C worst(n) =n(n-1)/2 ∈ Θ(n 2 )

C avg(n) ≈ n 2 /4 ∈ Θ(n 2 )

C best(n) =n - 1 ∈ Θ(n) (also fast on almost sorted

arrays)

„ Space efficiency: in-place

„ Stability: yes „ Best elementary sorting algorithm overall

„ Binary insertion sort