Sorting Techniques
1
Sorting
OUTPUT
INPUT
sequence of numbers
a1, a2, a3,.,an
2
5
4
10
Sort
7
Correctness
Correctness
For any given input the algorithm
For any given input the algorithm
halts with the output:
halts with the output
b1 < b2 < b3 < .< bn
b1

*8053*
8083
(Pages : 2)
Reg. No. : .
Name : .
Fourth Semester B.Tech. Degree Examination, December 2009
(2003 Scheme)
03.405 : DATA STRUCTURES & PROGRAMMING METHODOLOGY (R.F.)
Time : 3 Hours
Max. Marks : 100
PART A
Answer all questions. All question carry

Searching and Sorting
Eric Roberts
CS 106A
March 5, 2010
Onceuponatime.
The Millennium Challenge Problems
Searching and Sorting
Searching
Chapter 12 looks at two operations on arrayssearching and
sortingboth of which turn out to be important in a wide
ra

Stepwise Refinement
Eric Roberts
CS 106A
January 8, 2010
Outline
1. Review Karel statement forms
2. Exercise: The putBeeperLine method
3. Stepwise refinement
4. Exercise: The BanishWinter program
5. Preconditions and postconditions
6. Story time: Past Kar

Trees
Outline
Binary trees
Binary search trees
AVL trees
B trees
Applications
04/13/12
Data Structure: Trees
2
Trees
Tree ADT
A tree is a collection (may be empty) of nodes,
containing:
a distinguished node called the root r,
zero or more non-empty subt

CS 332: Algorithms
Linear-Time Sorting Continued
Medians and Order Statistics
David Luebke
1
Review: Comparison Sorts
Comparison sorts: O(n lg n) at best
Model sort with decision tree
Path down tree = execution trace of algorithm
Leaves of tree = poss

CS 332: Algorithms
Linear-Time Sorting Algorithms
David Luebke
1
Sorting So Far
q
Insertion sort:
s
s
s
s
s
s
Easy to code
Fast on small inputs (less than ~50 elements)
Fast on nearly-sorted inputs
O(n2) worst case
O(n2) average (equally-likely inputs) ca

Stacks
Outline
Linked-list Implementation
Array Implementation
Applications
04/13/12
Data Structure: Stacks
2
Stack ADT
If N=0, it is an empty stack.
Methods
Push: add the element on
the top of stack
Pop: take the element from
the top of stack
Top:

CS 332: Algorithms
S-S Shortest Path: Dijkstras Algorithm
Disjoint-Set Union
Amortized Analysis
David Luebke
1
Review:
Single-Source Shortest Path
Problem: given a weighted directed graph G,
find the minimum-weight path from a given
source vertex s to an

CS 332: Algorithms
Review for Exam 1
David Luebke
1
Administrative
Reminder: homework 3 due today
Reminder: Exam 1 Wednesday, Feb 13
1 8.5x11 crib sheet allowed
Both sides, mechanical reproduction okay
You will turn it in with the exam
David Luebke
2

CS 332: Algorithms
Quicksort
David Luebke
1
Review: Analyzing Quicksort
q
What will be the worst case for the algorithm?
s
q
What will be the best case for the algorithm?
s
q
Partition is balanced
Which is more likely?
s
q
Partition is always unbalanced
T

CS 332: Algorithms
Quicksort
David Luebke
1
Homework 2
Assigned today, due next Wednesday
q Will be on web page shortly after class
q Go over now
q
David Luebke
2
Review: Quicksort
Sorts in place
q Sorts O(n lg n) in the average case
q Sorts O(n2) in the

Queues
Outline
Array Implementation
Applications
04/13/12
Data Structure: Queues
2
Queue ADT
List A1, A2, , AN
If N=0, it is an empty stack.
Methods
enqueue: add the element
at the back of the queue
dequeue: take the element
from the front of the queu

CS 332: Algorithms
Topological Sort
Minimum Spanning Trees
David Luebke
1
Review: Breadth-First Search
BFS(G, s) cfw_
initialize vertices;
Q = cfw_s;
/ Q is a queue (duh); initialize to s
while (Q not empty) cfw_
u = RemoveTop(Q);
for each v u->adj cfw_
i

CS 332: Algorithms
Merge Sort
Solving Recurrences
The Master Theorem
David Luebke
1
Administrative Question
Who here cannot make Monday-Wednesday
office hours at 10 AM?
q If nobody, should we change class time?
q
David Luebke
2
Homework 1
q
Homework 1 wil

Lists
Outline
Array Implementation
Singly linked lists
Doubly linked lists
Circular linked lists
04/13/12
Data Structure: Lists
2
List ADT
List of size N: A1, A2, , AN
If N=0, it is an empty list.
Methods
find
insert
remove
makeEmpty
findKth
04/13/1

CS 332: Algorithms
Merge Sort
Solving Recurrences
The Master Theorem
David Luebke
1
Administrative Question
Who here cannot make Monday-Wednesday
office hours at 10 AM?
q If nobody, should we change class time?
q
David Luebke
2
Homework 1
q
Homework 1 wil

CS 332: Algorithms
Introduction
Proof By Induction
Asymptotic notation
David Luebke
1
The Course
Purpose: a rigorous introduction to the design
and analysis of algorithms
Not a lab or programming course
Not a math course, either
Textbook: Introduction t

CS 332: Algorithms
Asymptotic Performance
David Luebke
1
Review: Asymptotic Performance
Asymptotic performance: How does algorithm
behave as the problem size gets very large?
o Running time
o Memory/storage requirements
Remember that we use the RAM model

CS 332: Algorithms
Heapsort
Priority Queues
Quicksort
David Luebke
1
Review: Heaps
q
A heap is a complete binary tree, usually
represented as an array:
16
4
10
14
2
7
8
3
1
A = 16 14 10 8
David Luebke
9
7
9
3
2
4
2
1
Review: Heaps
q
To represent a heap as

CS 332: Algorithms
Introduction to heapsort
David Luebke
1
Review: The Master Theorem
q
Given: a divide and conquer algorithm
s
s
q
An algorithm that divides the problem of size n
into a subproblems, each of size n/b
Let the cost of each stage (i.e., the

Heaps
Priority Queues
Outline
Binary heaps
Binomial queues
Leftist heaps
04/13/12
Data Structure: Heaps
2
Binary Heaps
Heap order property
For every root node N, the key in the parent
of N is smaller than or equal to the key in N.
5
12
15
21
16
19
22
28
2

CS 332: Algorithms
Graph Algorithms
David Luebke
1
Administrative
Test postponed to Friday
Homework:
Turned in last night by midnight: full credit
Turned in tonight by midnight: 1 day late, 10% off
Turned in tomorrow night: 2 days late, 30% off
Extr

CS 332: Algorithms
Graph Algorithms
David Luebke
1
Review: Depth-First Search
Depth-first search is another strategy for
exploring a graph
Explore deeper in the graph whenever possible
Edges are explored out of the most recently
discovered vertex v tha

More Linked Lists
Chapter 9
Linked List Variants (9.1)
In some applications, it is convenient to keep access to both
the first node and the last node in the list.
L
9
first
17
22
26
last
mySize
5
This would work nicely for a linked-list implementation of

CS 332: Algorithms
Go over exam
Binary Search Trees
David Luebke
1
Exam
Hand back, go over exam
David Luebke
2
Review: Dynamic Sets
Next few lectures will focus on data structures
rather than straight algorithms
In particular, structures for dynamic se

CS 332: Algorithms
Medians and Order Statistics
Structures for Dynamic Sets
David Luebke
1
Homework 3
On the web shortly
Due Wednesday at the beginning of class (test)
David Luebke
2
Review: Radix Sort
Radix sort:
Assumption: input has d digits rangin