7/31/2019 Sorting and DS
1/170
Mahindra Satyam 2010
Sorting Techniques
7/31/2019 Sorting and DS
2/170
Mahindra Satyam 2010
Introduction to
Algorithms
7/31/2019 Sorting and DS
3/170
3
Mahindra Satyam 2010
What is an Algorithm?
An algorithm is a finite set of instructions which, if followed, accomplish a
particular task.
In addition every algorithm must satisfy the following criteria:
Input:
there are zero or more quantities which are externally supplied;
Output:at least one quantity is produced;
Definiteness:each instruction must be clear and unambiguous;
7/31/2019 Sorting and DS
4/170
4
Mahindra Satyam 2010
What is an Algorithm?
Finiteness:
If we trace out the instructions of an algorithm, then for all cases thealgorithm will terminate after a finite number of steps;
Effectiveness:Every instruction must be sufficiently basic that it can in principle be
carried out by a person using only pencil and paper.It is not enough that each operation be definite, but it must also be
feasible.
7/31/2019 Sorting and DS
5/170
5
Mahindra Satyam 2010
Ex: Algorithm: To find out biggest of 10 numbers:
Step 1: num[10]
Step 2:big =0,i=0
Step 3: if num[i] > big
big = num[i]
Step 4: repeat step 3 for i=1 to 10
Step 5:print big
Step 6: end
Note: There may be multiple solutions to a problem.
How do you find, which solution is Optimal (Better) ?
7/31/2019 Sorting and DS
6/170
6
Mahindra Satyam 2010
Ex: Algorithm: To find out biggest of 10 numbers:
Step 1: num[10]
Step 2:big =0,i=0
Step 3: if num[i] > big
big = num[i]
Step 4: repeat step 3 for i=1 to 10
Step 5:print big
Step 6: end
Note: There may be multiple solutions to a problem.
How do you find, which solution is Optimal (Better) ?
Ans: TIME COMPLEXITY
7/31/2019 Sorting and DS
7/170
Mahindra Satyam 2010
How to Determine
Complexities ?
7/31/2019 Sorting and DS
8/1708
Mahindra Satyam 2010
Sequence of statements
statement 1;
statement 2;...
statement k;
Number of Statements = k
The total time is found by adding the times for all statements:
total time = time(statement 1) + time(statement 2) + ... + time(statement k)
If each statement is "simple" (only involves basic operations) then
the time for each statement is constant and the total time is also constant:
O(1).
7/31/2019 Sorting and DS
9/1709
Mahindra Satyam 2010
if-then-else statements
if (condition) {
sequence of statements 1
}
else {
sequence of statements 2
}
Here, either sequence 1 will execute, or sequence 2 will execute.
Therefore, the worst-case time is the slowest of the two possibilities:
max(time(sequence 1), time(sequence 2)).
Ex: if sequence 1 is O(N)
and sequence 2 is O(1)the worst-case time for the whole if-then-else statement would be
O(N).
7/31/2019 Sorting and DS
10/17010
Mahindra Satyam 2010
for loops
for (i = 0; i < N; i++) {
sequence of statements}
The loop executes N times,so the sequence of statements also executes N times.
Since we assume the statements are O(1),
the total time for the for loop is N * O(1),
which is O(N) overall.
7/31/2019 Sorting and DS
11/17011
Mahindra Satyam 2010
Nested loops - inner loop iterations are is independent of outer loops index
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {sequence of statements
}
}
The outer loop executes N times.
Every time the outer loop executes, the inner loop executes M times.As a result, the statements in the inner loop execute a total of N * M times.
Thus, the complexity is O(N * M).
In a common special case where
the stopping condition of the inner loop is j < N instead of j < M
(i.e., the inner loop also executes N times),
the total complexity for the two loops is O(N2).
7/31/2019 Sorting and DS
12/17012
Mahindra Satyam 2010
Nested loops - inner loop iterations dependent on index of outer loop
for (i = 0; i < N; i++) {
for (j = i+1; j < N; j++) {sequence of statements
}
}
Here we can't just multiply the number of iterations of the outer loop times
the number of iterations of the inner loop, because the inner loop has a
different number of iterations each time.
7/31/2019 Sorting and DS
13/17013
Mahindra Satyam 2010
Nested loops - inner loop iterations dependent on index of outer loop contd
The following table gives you, how many iterations that inner loop has
So the total number of times the sequence of statements executes is:N + N-1 + N-2 + ... + 3 + 2 + 1.
Hence the total is O(N2).
Value of i Number of iterations of inner loop
0 N
1 N-1
2 N-2
... ...N-2 2
N-1 1
7/31/2019 Sorting and DS
14/170
Mahindra Satyam 2010
Ex:Calculating TimeComplexity of Searching
7/31/2019 Sorting and DS
15/17015
Mahindra Satyam 2010
How to Calculate Time Complexity of Linear Search of10, 5, 15, 6, 18 ?
Function to Find a key using Linear Search
int linearSearch(int a[],int n,int key)
{
int i;
for(i=0;i
7/31/2019 Sorting and DS
16/17016
Mahindra Satyam 2010
How to Calculate Time Complexity of Linear Search
Big Oh (O) notation -> 2n+3 => O(n) => upperbound
Best case O(1)
Worst case O(n)
Average case :
1st Input 12nd Input 2
3rd Input 3
nth Input n
1 + 2 + 3 + n n(n+1)
--------------------- = --------- = (n+1) = O(n)
n 2n
7/31/2019 Sorting and DS
17/170
Mahindra Satyam 2010
Complexities of some of
SORT & SEARCHTechniques
7/31/2019 Sorting and DS
18/17018
Mahindra Satyam 2010
Time Complexities of various SORT techniques
Technique / Case Best Case Average Case Worst Case
Bubble Sort O(1) O(n2) O(n2)
Selection Sort O(1) O(n2
) O(n2
)
Insertion Sort O(1) O(n2) O(n2)
Merge Sort O(n log n) O(n log n) O(n log n)
Quick Sort O(nlog n) O(n log n) O(n2)
7/31/2019 Sorting and DS
19/17019
Mahindra Satyam 2010
Time Complexities of various SEARCH techniques
Technique / Case Best Case Average Case Worst Case
Binary Search O(1) O( log n) O( log n)
Linear Search O(1) O(n) O(n)
7/31/2019 Sorting and DS
20/17020
Mahindra Satyam 2010
Priority of Complexities
Constant Time O(1)
Logarithmic Time O(log n)
Linear Time O(n) O(n log n)
Quadratic Time O(n2) O(n3)
Exponential Time O(2n)
Factorial Time O(n!)
Best
Worst
7/31/2019 Sorting and DS
21/17021
Mahindra Satyam 2010
The Sorting Problem
Input:
A sequence ofnnumbers a1, a2, . . . , an
Output:
A permutation (reordering) a1, a2, . . . , an of the input sequence such that a1 a2
an
7/31/2019 Sorting and DS
22/17022
Mahindra Satyam 2010
Structure of data
7/31/2019 Sorting and DS
23/17023
Mahindra Satyam 2010
Why Study Sorting Algorithms?
There are 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
7/31/2019 Sorting and DS
24/17024
Mahindra Satyam 2010
Some Definitions
Internal Sort The data to be sorted is all stored in the computers
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.
St bilit
7/31/2019 Sorting and DS
25/17025
Mahindra Satyam 2010
Stability
A STABLE sort preserves relative order of records with
equal keys
Sorted on first key:
Sort file on second key:
Records with key value
3 are not in order on
first key!!
7/31/2019 Sorting and DS
26/170
26
Mahindra Satyam 2010
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
these cards were originally the top cards of the pile on the
table
7/31/2019 Sorting and DS
27/170
27
Mahindra Satyam 2010
To insert 12, we need tomake room for it by movingfirst 36 and then 24.
Insertion Sort
7/31/2019 Sorting and DS
28/170
28
Mahindra Satyam 2010
Insertion Sort
7/31/2019 Sorting and DS
29/170
29
Mahindra Satyam 2010
Insertion Sort
7/31/2019 Sorting and DS
30/170
30
Mahindra Satyam 2010
Insertion Sort
5 2 4 6 1 3
input array
left sub-array right sub-array
at each iteration, the array is divided in two sub-arrays:
sorted
unsorted
7/31/2019 Sorting and DS
31/170
31
Mahindra Satyam 2010
Insertion Sort
7/31/2019 Sorting and DS
32/170
Mahindra Satyam 2010
Bubble Sort
7/31/2019 Sorting and DS
33/170
33
Mahindra Satyam 2010
5 7 6 4 3
0 1 2 3 4
PASS - 1
temp
In each pass, the adjacent
elements are compared.
If the element at (j+1)th position
is larger than jth element,
interchange occurs
7/31/2019 Sorting and DS
34/170
34
Mahindra Satyam 2010
5 6 4 3 7
0 1 2 3 4
PASS - 1
temp
5 6 4 3 7PASS - 2
In second pass, the
adjacent elements are
compared excluding the
last sorted element.
7/31/2019 Sorting and DS
35/170
35
Mahindra Satyam 2010
5 6 4 3 7
0 1 2 3 4
PASS - 1
5 4 3 6 7PASS - 2
5 4 3 6 7PASS - 3
temp
In third pass, the adjacent
elements are compared
excluding the last sorted
element.
7/31/2019 Sorting and DS
36/170
36
Mahindra Satyam 2010
5 6 4 3 7
0 1 2 3 4
PASS - 1
5 4 3 6 7PASS - 2
4 3 5 6 7PASS - 3
temp
4 3 5 6 7PASS - 4
In fourth pass, the
adjacent elements are
compared excluding the
last sorted element.
7/31/2019 Sorting and DS
37/170
37
Mahindra Satyam 2010
5 6 4 3 7PASS - 1
5 4 3 6 7PASS - 2
4 3 5 6 7PASS - 3
3 4 5 6 7PASS - 4
Analysis
No. of comparisons
(n-1)
(n-2)
(1)
Thus, the total no. of comparisons for the elements to be arranged in
order are
[(n-1)+(n-2)++1]= (n-1) , and hence O(n^2)
5 7 6 4 3
0 1 2 3 4
7/31/2019 Sorting and DS
38/170
Mahindra Satyam 2010
Merge SORT
7/31/2019 Sorting and DS
39/170
39
Mahindra Satyam 2010
Divide and Conquer
1. Base case: the problem is small enough,solve directly
2. Divide the problem into two or more
similar and smallersubproblems
3. Recursively solve the subproblems
4. Combine solutions to the subproblems
7/31/2019 Sorting and DS
40/170
40
Mahindra Satyam 2010
Divide and Conquer - Sort
Problem:Input: A[n]unsorted array ofn 1 integers.Output:A[n]sorted in non-decreasing order
7/31/2019 Sorting and DS
41/170
7/31/2019 Sorting and DS
42/170
42
Mahindra Satyam 2010
Merge Sort: Idea
Merge
Recursively
sort
Divide into
two halvesFirstPart SecondPart
FirstPart SecondPart
A:
A is sorted!
M S t Al ith
7/31/2019 Sorting and DS
43/170
43
Mahindra Satyam 2010
Merge Sort: Algorithm
Merge-Sort (A, n)
if n=1 return
else
n1 n2 n/2
create array L[n1], R[n2]
for i 0 to n1-1 do L[i] A[i]
for j 0 to n2-1 do R[j] A[n1+j]
Merge-Sort(L, n1)Merge-Sort(R, n2)
Merge(A, L, n1, R, n2 )
Space: n
Recursive Call
Time: n
7/31/2019 Sorting and DS
44/170
44
Mahindra Satyam 2010
Merge-Sort: Merge
Sorted Sorted
merge
A:
L: R:
Sorted
Merge-Sort: Merge Example
7/31/2019 Sorting and DS
45/170
45
Mahindra Satyam 2010
L: R:1 2 6 8 3 4 5 7
A:
Merge-Sort: Merge Example
7/31/2019 Sorting and DS
46/170
46
Mahindra Satyam 2010
3 5 15 28 10 14
L:
A:
3 15 28 30 6 10 14 22
R:
i=0 j=0
k=0
1 2 6 8 3 4 5 7
1
7/31/2019 Sorting and DS
47/170
Merge-Sort: Merge Example
7/31/2019 Sorting and DS
48/170
48
Mahindra Satyam 2010
1 2 15 28 30 6 10 14
L:
A:
6 10 14 22
R:
i=2 j=0
k=2
1 2 6 8 3 4 5 7
3
Merge-Sort: Merge Example
7/31/2019 Sorting and DS
49/170
49
Mahindra Satyam 2010
1 2 3 6 10 14
L:
A:
6 10 14 22
R:
i=2 j=1
k=3
1 2 6 8 3 4 5 7
4
Merge-Sort: Merge Example
7/31/2019 Sorting and DS
50/170
50
Mahindra Satyam 2010
1 2 3 4 6 10 14
L:
A:
6 10 14 22
R:
j=2
k=4
1 2 6 8 3 4 5 7
i=2
5
Merge-Sort: Merge Example
7/31/2019 Sorting and DS
51/170
51
Mahindra Satyam 2010
1 2 3 4 5 6 10 14
L:
A:
6 10 14 22
R:
i=2 j=3
k=5
1 2 6 8 3 4 5 7
6
Merge-Sort: Merge Example
7/31/2019 Sorting and DS
52/170
52
Mahindra Satyam 2010
1 2 3 4 5 6 14
L:
A:
6 10 14 22
R:
i=3 j=3
k=6
1 2 6 8 3 4 5 7
7
Merge-Sort: Merge Example
7/31/2019 Sorting and DS
53/170
53
Mahindra Satyam 2010
1 2 3 4 5 5 7 14
L:
A:
3 5 15 28 6 10 14 22
R:
1 2 6 8 3 4 5 7
8
i=3 j=4
k=7
Merge-Sort: Merge Example
7/31/2019 Sorting and DS
54/170
54
Mahindra Satyam 2010
1 2 3 4 5 6 7 8
L:
A:
3 5 15 28 6 10 14 22
R:
1 2 6 8 3 4 5 7
i=4 j=4
k=8
7/31/2019 Sorting and DS
55/170
55
Mahindra Satyam 2010
merge(A,L,n1,R,n2)
i j 0for k 0 to n1+n2-1if i < n1if j = n2 or L[i] R[j]
A[k] L[i]i i + 1
elseif j < n2
A[k] R[j]j j + 1
Number of iterations: (n1+n2)Total time: c(n1+n2) for some c
M S t E ti E l
7/31/2019 Sorting and DS
56/170
56
Mahindra Satyam 2010
Merge-Sort Execution Example
6 2 8 4 3 7 5 16 2 8 4 3 7 5 1
Divide
7/31/2019 Sorting and DS
57/170
57
Mahindra Satyam 2010
6 2 8 4
3 7 5 1
6 2 8 4
Merge-Sort Execution Example
Recursive call , divide
7/31/2019 Sorting and DS
58/170
58
Mahindra Satyam 2010
3 7 5 1
8 4
6 26 2
Recursive call , divide
Merge-Sort Execution Example
M S E i E l
7/31/2019 Sorting and DS
59/170
59
Mahindra Satyam 2010
3 7 5 1
8 4
6
2
Merge-Sort Execution Example
Recursive call , base case
M S t E ti E l
7/31/2019 Sorting and DS
60/170
60
Mahindra Satyam 2010
3 7 5 1
8 4
6 2
Merge-Sort Execution Example
Recursive call return
M S t E ti E l
7/31/2019 Sorting and DS
61/170
61
Mahindra Satyam 2010
3 7 5 1
8 4
6
2
Merge-Sort Execution Example
Recursive call , base case
M S t E ti E l
7/31/2019 Sorting and DS
62/170
62
Mahindra Satyam 2010
3 7 5 1
8 4
6 2
Merge-Sort Execution Example
Recursive call return
M S t E ti E l
7/31/2019 Sorting and DS
63/170
63
Mahindra Satyam 2010
3 7 5 1
8 4
2 6
Merge-Sort Execution Example
Merge
M S t E ti E l
7/31/2019 Sorting and DS
64/170
64
Mahindra Satyam 2010
3 7 5 1
8 42 6
Merge-Sort Execution Example
Recursive call return
M S t E ti E l
7/31/2019 Sorting and DS
65/170
65
Mahindra Satyam 2010
3 7 5 1
8 4
2 6
Merge-Sort Execution Example
Recursive call
48
, divide
M S t E ti E l
7/31/2019 Sorting and DS
66/170
66
Mahindra Satyam 2010
3 7 5 1
4
2 6
8
Merge-Sort Execution Example
Recursive call, base case
7/31/2019 Sorting and DS
67/170
Merge Sort Execution Example
7/31/2019 Sorting and DS
68/170
68
Mahindra Satyam 2010
4
2 6
8
Merge-Sort Execution Example
Recursive call, base case
Merge Sort Execution Example
7/31/2019 Sorting and DS
69/170
69
Mahindra Satyam 2010
3 7 5 1
4
2 6
8
Merge-Sort Execution Example
Recursive call return
Merge Sort Execution Example
7/31/2019 Sorting and DS
70/170
70
Mahindra Satyam 2010
3 7 5 1
2 6
4 8
Merge-Sort Execution Example
merge
Merge Sort Execution Example
7/31/2019 Sorting and DS
71/170
71
Mahindra Satyam 2010
3 7 5 1
2 6 4 8
Merge-Sort Execution Example
Recursive call return
Merge Sort Execution Example
7/31/2019 Sorting and DS
72/170
72
Mahindra Satyam 2010
3 7 5 1
2 4 6 8
Merge-Sort Execution Example
merge
Merge Sort Execution Example
7/31/2019 Sorting and DS
73/170
73
Mahindra Satyam 2010
3 7 5 12 4 6 8
Merge-Sort Execution Example
Recursive call return
Merge Sort Execution Example
7/31/2019 Sorting and DS
74/170
74
Mahindra Satyam 2010
3 7 5 1
2 4 6 8
Merge-Sort Execution Example
Recursive call
Merge Sort Execution Example
7/31/2019 Sorting and DS
75/170
75
Mahindra Satyam 2010
1 3 5 7
2 4 6 8
Merge-Sort Execution Example
Merge Sort Execution Example
7/31/2019 Sorting and DS
76/170
76
Mahindra Satyam 2010
1 3 5 72 4 6 8
Merge-Sort Execution Example
Recursive call return
Merge Sort Execution Example
7/31/2019 Sorting and DS
77/170
77
Mahindra Satyam 2010
1 2 3 4 5 6 7 8
Merge-Sort Execution Example
merge
Merge Sort Analysis
7/31/2019 Sorting and DS
78/170
78
Mahindra Satyam 2010
Merge-Sort Analysis
Time, dividen
2 n/2 = n
4 n/4 = n
n/2 2 = n
log n levels
Total time for divide: n log n
n
n/2 n/2
n/4 n/4 n/4 n/4
Merge Sort Analysis
7/31/2019 Sorting and DS
79/170
79
Mahindra Satyam 2010
Merge-Sort Analysis
Time, mergingcn
2 cn/2 = n
4 cn/4 = n
n/2 2c = n
log n levels
Total running time: order of nlogn
Total Space: order of n
Total time for merging: cn log n
n
n/2 n/2
n/4 n/4 n/4 n/4
7/31/2019 Sorting and DS
80/170
Mahindra Satyam 2010
Quick sort
Quick Sort
7/31/2019 Sorting and DS
81/170
81
Mahindra Satyam 2010
Quick Sort
It is based on the divide and conquer paradigm.
Divide: The array A[p..r] is partitioned (rearranged) into nonempty subarrays A[p..q] and A[q+1..r] such that each element of A[p..q] is lessthan or equal to each element of A[q+1..r].
The index q is computed as a part of this partitioning.
Conquer: Two sub arrays A[p..q] and A[q+1..r] are sorted by recursivecalls to quick sort.
Combine: Since the sub arrays are sorted in place, no work is needed tocombine them, the entire array A[p..r] is now sorted.
7/31/2019 Sorting and DS
82/170
82
Mahindra Satyam 2010
QUICKSORT (A, p, r)
1. if p < r
2. then q PARTITION (A, p, r)
3. QUICKSORT (A, p, q)
4. QUICKSORT (A, q+1, r)
Note: QUICKSORT(A,0,n-1) sorts the entire array A.
7/31/2019 Sorting and DS
83/170
83
Mahindra Satyam 2010
11 8 12 14 5 1 4 13
0 1 2 3 4 5 6 7
11
pivot
ublb
5 8 4 1 11 14 12 13
0 1 2 3 4 5 6 7
11
pivot
Partition Algorithm
7/31/2019 Sorting and DS
84/170
84
Mahindra Satyam 2010
down
11 8 12 14 5 1 4 13
up
0 1 2 3 4 5 6 7
Set pivot as the first element in the list,
pivot = x[lb]
Set ^down to the first element (^lb), and ^up to the last element (^ub),which hypothetically considered is set to infinity
11
pivot
ublb
Partition Algorithm
Partition Algorithm
7/31/2019 Sorting and DS
85/170
85
Mahindra Satyam 2010
while(down < up)
{
while(x[down] pivot) up--;
if(down < up ) interchange x[down] with x[up]
}
down
11 8 12 14 5 1 4 13
up
0 1 2 3 4 5 6 7
11
pivot
ublb
Partition Algorithm
Partition Algorithm
7/31/2019 Sorting and DS
86/170
86
Mahindra Satyam 2010
down
11 8 12 14 5 1 4 13
up
0 1 2 3 4 5 6 7
while(down < up)
{
while(x[down] pivot) up--;
if(down < up ) interchange x[down] with x[up]
}
11
pivot
ublb
Partition Algorithm
Partition Algorithm
7/31/2019 Sorting and DS
87/170
87
Mahindra Satyam 2010
down
11 8 12 14 5 1 4 13
up
0 1 2 3 4 5 6 7
while(down < up)
{
while(x[down] pivot) up--;
if(down < up ) interchange x[down] with x[up]
}
11
pivot
ublb
g
Partition Algorithm
7/31/2019 Sorting and DS
88/170
88
Mahindra Satyam 2010
while(down < up)
{
while(x[down] pivot) up--;
if(down < up ) interchange x[down] with x[up]
}
11 8 4 14 5 1 12 13
0 1 2 3 4 5 6 7
pivot
down up
ublb
11
g
Partition Algorithm
7/31/2019 Sorting and DS
89/170
89
Mahindra Satyam 2010
down
11 8 4 14 5 1 12 13
0 1 2 3 4 5 6 7
while(down < up)
{
while(x[down] pivot) up--;
if(down < up ) interchange x[down] with x[up]
}
11
pivot
up
ublb
g
Partition Algorithm
7/31/2019 Sorting and DS
90/170
90
Mahindra Satyam 2010
down
11 8 4 14 5 1 12 13
0 1 2 3 4 5 6 7
while(down < up)
{
while(x[down] pivot) up--;
if(down < up ) interchange x[down] with x[up]
}
11
pivot
up
ublb
g
Partition Algorithm
7/31/2019 Sorting and DS
91/170
91
Mahindra Satyam 2010
down
11 8 4 14 5 1 12 13
0 1 2 3 4 5 6 7
while(down < up)
{
while(x[down] pivot) up--;
if(down < up ) interchange x[down] with x[up]
}
11
pivot
up
ublb
g
Partition Algorithm
7/31/2019 Sorting and DS
92/170
92
Mahindra Satyam 2010
down
11 8 4 1 5 14 12 13
0 1 2 3 4 5 6 7
while(down < up)
{
while(x[down] pivot) up--;
if(down < up ) interchange x[down] with x[up]
}
11
pivot
up
ublb
g
Partition Algorithm
7/31/2019 Sorting and DS
93/170
93
Mahindra Satyam 2010
updown
11 8 4 1 5 14 12 13
0 1 2 3 4 5 6 7
while(down < up)
{
while(x[down] pivot) up--;
if(down < up ) interchange x[down] with x[up]
}
11
pivot
ublb
Partition Algorithm
7/31/2019 Sorting and DS
94/170
94
Mahindra Satyam 2010
downup
11 8 4 1 5 14 12 13
0 1 2 3 4 5 6 7
while(down < up)
{
while(x[down] pivot) up--;
if(down < up ) interchange x[down] with x[up]
}
11
pivot
ublb
Partition Algorithm
7/31/2019 Sorting and DS
95/170
95
Mahindra Satyam 2010
downup
11 8 4 1 5 14 12 13
0 1 2 3 4 5 6 7
while(down < up)
{
while(x[down] pivot) up--;
if(down < up ) interchange x[down] with x[up]
}
11
pivot
ublb
Partition Algorithm
7/31/2019 Sorting and DS
96/170
96 Mahindra Satyam 2010
downup
11 8 4 1 5 14 12 13
0 1 2 3 4 5 6 7
while(down < up)
{
while(x[down] pivot) up--;
if(down < up ) interchange x[down] with x[up]
}
interchange x[up] with x[lb]
11
pivot
ublb
Partition Algorithm
7/31/2019 Sorting and DS
97/170
97 Mahindra Satyam 2010
downup
5 8 4 1 11 14 12 13
0 1 2 3 4 5 6 7
while(down < up)
{
while(x[down] pivot) up--;
if(down < up ) interchange x[down] with x[up]
}
interchange x[up] with x[lb]
11
pivot
ublb
Quick Sort
7/31/2019 Sorting and DS
98/170
Mahindra Satyam 2010
11 8 12 14 5 1 4 13
0 1 2 3 4 5 6 7
11
pivot
5 8 4 1 11 14 12 13
Quick Sort
7/31/2019 Sorting and DS
99/170
99 Mahindra Satyam 2010
11 8 12 14 5 1 4 13
0 1 2 3 4 5 6 7
5 8 4 1 11 14 12 13
4 1 85
Quick Sort
7/31/2019 Sorting and DS
100/170
100 Mahindra Satyam 2010
11 8 12 14 5 1 4 13
0 1 2 3 4 5 6 7
5 8 4 1 11 14 12 13
1 4
4 1 5 8
Quick Sort
7/31/2019 Sorting and DS
101/170
101 Mahindra Satyam 2010
11 8 12 14 5 1 4 13
0 1 2 3 4 5 6 7
5 8 4 1 11 14 12 13
1 4
4 1 5
1
8
7/31/2019 Sorting and DS
102/170
Mahindra Satyam 2010
Datastructures
Data structure
7/31/2019 Sorting and DS
103/170
103 Mahindra Satyam 2010
A data structureis an aggregation of atomicand composite data into a set with defined
relationships.
Structuremeans a set of rules that holds datatogether.
Data structure
7/31/2019 Sorting and DS
104/170
104 Mahindra Satyam 2010
A combination of elements in which eachis either a data type or another data
structure.
A set of associations or relationships
(structure) involving the combined
elements.
Data Structure
7/31/2019 Sorting and DS
105/170
105 Mahindra Satyam 2010
7/31/2019 Sorting and DS
106/170
106 Mahindra Satyam 2010
Abstraction
7/31/2019 Sorting and DS
107/170
107 Mahindra Satyam 2010
The concept of abstraction means
We know whata data type can doHowit is done is hidden
Abstract Data Type (ADT)
7/31/2019 Sorting and DS
108/170
108 Mahindra Satyam 2010
Declaration of dataDeclaration of operations
Encapsulation of data and operations
7/31/2019 Sorting and DS
109/170
109 Mahindra Satyam 2010
operations
Data structure
Basic operations of linear lists
7/31/2019 Sorting and DS
110/170
110
Mahindra Satyam 2010
1. Insertion2. Deletion
3. Retrieval
4. Traversal
Example: Banking Application
7/31/2019 Sorting and DS
111/170
111
Mahindra Satyam 2010
Operations are (typically):
Open accounts (far less often than access)
Close accounts (far less often than access)
Access account toAdd money
Access account to Withdraw money
Example: Banking Application
7/31/2019 Sorting and DS
112/170
112
Mahindra Satyam 2010
Teller and ATM transactions are expected to take little time.
Opening or closing an account can take much longer (perhaps up to an hour).
Example: Banking Application
7/31/2019 Sorting and DS
113/170
113
Mahindra Satyam 2010
When considering the choice of data structure to use in the database system that
manages the accounts, we are looking for a data structure that:
Is inefficient for deletion
Highly efficient for search
Moderately efficient for insertion
Example: Banking Application
7/31/2019 Sorting and DS
114/170
114
Mahindra Satyam 2010
1. One data structure that meets these requirements is the
hash table (chapter 9).2. Records are accessible by account number (called anexact-match query)
3. Hash tables allow for extremely fast exact-match search.4. Hash tables also support efficient insertion of new records.5. Deletions can also be supported efficiently (but too many
deletions lead to some degradation in performancerequiring the hash table to be reorganized).
Example: City Database
7/31/2019 Sorting and DS
115/170
115
Mahindra Satyam 2010
1.Database system for cities and towns.
2.Users find information about a particular place byname (exact-match query)
3.Users also find all places that match a particularvalue (or range of values), such as location or
population size (called a range query).
Example: City Database
7/31/2019 Sorting and DS
116/170
116
Mahindra Satyam 2010
The database must answer queries quickly enough to
satisfy the patience of a typical user.For an exact-match query, a few seconds is
satisfactory
For a range queries, the entire operation may be
allowed to take longer, perhaps on the order of a
minute.
Example: City Database
7/31/2019 Sorting and DS
117/170
117
Mahindra Satyam 2010
The hash table is inappropriate for implementing the
city database because: It cannot perform efficient range queries
The B+ tree (section 10) supports large databases: Insertion
Deletion Range queries
If the database is created once and then neverchanged, a simple linear index would be more
appropriate.
Selecting a Data Structure
7/31/2019 Sorting and DS
118/170
118
Mahindra Satyam 2010
Select a data structure as follows:1. Analyze the problem to determine the resource
constraints a solution must meet.
2. Determine the basic operations that must besupported. Quantify the resource constraints foreach operation.
3. Select the data structure that best meets theserequirements.
Some Questions to Ask
7/31/2019 Sorting and DS
119/170
119
Mahindra Satyam 2010
1. Are all data inserted into the data structure at thebeginning, or are insertions intersparsed withother operations?
2. Can data be deleted?3. Are all data processed in some well-defined
order, or is random access allowed?
Data Structure Philosophy
7/31/2019 Sorting and DS
120/170
120
Mahindra Satyam 2010
Each data structure has costs and benefits.Rarely is one data structure better than another inall situations.A data structure requires: space for each data item it stores,
time to perform each basic operation, programming effort.
Data Structure Philosophy
7/31/2019 Sorting and DS
121/170
121
Mahindra Satyam 2010
Each problem has constraints on available
space and time. Only after a careful analysis of problem
characteristics can we know the best datastructure for the task.
Bank example: Start account: a few minutes Transactions: a few seconds Close account: overnight
7/31/2019 Sorting and DS
122/170
Mahindra Satyam 2010
Linked List
Objectives
7/31/2019 Sorting and DS
123/170
123
Mahindra Satyam 2010
1. Understanding efficient memory utilization with Linked representation
2. Time complexities with respect to insertion, deletion and search
Linked List - Insertion at the front
7/31/2019 Sorting and DS
124/170
124
Mahindra Satyam 2010
Consider a linked list of 3 nodes.
Let first be the pointer to first node.
Let us consider inserting a new node ( withdata as 5 ) at the front.
10 200 20 300 30 NULL
100 200 300
first
Linked List - Insertion at the front
7/31/2019 Sorting and DS
125/170
125
Mahindra Satyam 2010
100
insertAtFirst(*first,val)
1. curr = getNode(val)
2. curr->next=first
3. first = curr
10 200 20 300 30 NULL
100 200 300
first
5 NULL
150
curr
Linked List - Insertion at the rear
http://localhost/var/www/apps/conversion/current/Collateral/Phase%20-%20I/Problem%20Solving%20-%20SE%20Practitioner%E2%80%99s%20approach/Linked-List-getNode.ppthttp://localhost/var/www/apps/conversion/current/Collateral/Phase%20-%20I/Problem%20Solving%20-%20SE%20Practitioner%E2%80%99s%20approach/Linked-List-getNode.ppt7/31/2019 Sorting and DS
126/170
126
Mahindra Satyam 2010
10 200 20 300 30 NULL
100 200 300
Linked List - Insertion at the rear
7/31/2019 Sorting and DS
127/170
127
Mahindra Satyam 2010
Consider a linked list of 3 nodes.
Let p be the pointer to first node.
10 200 20 300 30 NULL
100 200 300
p
Linked List - Insertion at the rear
7/31/2019 Sorting and DS
128/170
128
Mahindra Satyam 2010
10 200 20 300 30 NULL
100 200 300
Take another pointer temp (t) which is also pointing
to first node.
Make the pointer temp (t) to point to last node.
p t
Linked List - Insertion at the rear
7/31/2019 Sorting and DS
129/170
129
Mahindra Satyam 2010
10 200 20 300 30 NULL
100 200 300
While(t->next != NULL)
t = t->next;
p t
Linked List - Insertion at the rear
7/31/2019 Sorting and DS
130/170
130
Mahindra Satyam 2010
10 200 20 300 30 NULL
100 200 300
While(t->next != NULL)
t = t->next;
p t
Linked List - Insertion at the rear
7/31/2019 Sorting and DS
131/170
131
Mahindra Satyam 2010
10 200 20 300 30 NULL
100 200 300
While(t->next != NULL)
t = t->next;
p t
Now t points to last node in the list.
Linked List - Insertion at the rear
7/31/2019 Sorting and DS
132/170
132
Mahindra Satyam 2010
10 200 20 300 30 NULL
100 200 300
p t
r data = 40
r->next = NULL
40 NULL
400
r
Linked List - Insertion at the rear
7/31/2019 Sorting and DS
133/170
133
Mahindra Satyam 2010
10 200 20 300 30 NULL
100 200 300
p t
Make r as next node of t.
t->next = r;
40 NULL
400
r
400
Linked List - Insertion in-between
7/31/2019 Sorting and DS
134/170
134
Mahindra Satyam 2010
10 200 20 300 30 NULL
100 200 300
Consider a linked list of 3 nodes.
Let first be the pointer to first node.
Let us consider the insertion of a new node (data = 25)at position 3 (i.e. before node 30)
firstInsertion of a
new node with
data=25.
Linked List - Insertion in-between
7/31/2019 Sorting and DS
135/170
135
Mahindra Satyam 2010
10 200 20 300 30 NULL
100 200 300
Take two pointers temp (t) and old (o) pointing to first
node.
Navigate the pointers temp and old such that temp pointsto the correct position (sorted position ) for the newelement and old points to its previous node.
first to
Linked List - Insertion in-between
7/31/2019 Sorting and DS
136/170
136
Mahindra Satyam 2010
10 200 20 300 30 NULL
100 200 300
while(temp!=NULL && temp->data next;
tfirst o
25
val
Linked List - Insertion in-between
7/31/2019 Sorting and DS
137/170
137
Mahindra Satyam 2010
10 200 20 300 30 NULL
100 200 300
while(temp!=NULL && temp->data next;
tfirst o
25
val
Linked List - Insertion in-between
7/31/2019 Sorting and DS
138/170
138
Mahindra Satyam 2010
10 200 20 300 30 NULL
100 200 300
while(temp!=NULL && temp->data next;
tfirst o
25
val
Linked List - Insertion in-between
7/31/2019 Sorting and DS
139/170
139
Mahindra Satyam 2010
300
400400
10 200 20 300 30 NULL
100 200 300
curr = getNode(val);
curr -> next = t;
o->next = curr;
tfirst o
25 NULL
curr
X
http://localhost/var/www/apps/conversion/current/Collateral/Phase%20-%20I/Problem%20Solving%20-%20SE%20Practitioner%E2%80%99s%20approach/Linked-List-getNode.ppthttp://localhost/var/www/apps/conversion/current/Collateral/Phase%20-%20I/Problem%20Solving%20-%20SE%20Practitioner%E2%80%99s%20approach/Linked-List-getNode.ppt7/31/2019 Sorting and DS
140/170
Mahindra Satyam 2010
STACKS
Stacks
7/31/2019 Sorting and DS
141/170
141
Mahindra Satyam 2010
Stacks are a special form of collection
with LIFO semanticsTwo methods int push( Stack s, void *item );
- add item to the top of the stack
void *pop( Stack s );
- remove an item from the top of the stackLike a plate stacker
Other methods
int IsEmpty( Stack s );
/* Return TRUE if empty */void *Top( Stack s );
/* Return the item at the top,
without deleting it */
Stacks - Implementation
7/31/2019 Sorting and DS
142/170
142
Mahindra Satyam 2010
Arrays
Provide a stack capacity to the constructor
Flexibility limited butmatches many real uses Capacity limited by some constraint Memory in your computer
Size of the plate stacker, etc
push, pop methods Variants ofAddToC, DeleteFromCLinked list also possible
Stack:
basically a Collection with special semantics!
Stacks - Relevance
7/31/2019 Sorting and DS
143/170
143
Mahindra Satyam 2010
Stacks appear in computer programs
Key to call / return in functions & procedures Stack frame allows recursive calls
Call: push stack frame
Return: pop stack frame
Stack frame Function arguments
Return address
Local variables
Stack Frames - Functions in HLL
7/31/2019 Sorting and DS
144/170
144
Mahindra Satyam 2010
Program
function f( int x, int y) {
int a;
if ( term_cond ) return ;a = .;return g( a );
}
function g( int z ) {
int p, q;
p = . ; q = . ;
return f(p,q);}
Contextfor execution of f
Recursion
7/31/2019 Sorting and DS
145/170
145
Mahindra Satyam 2010
Very useful technique
Definition of mathematical functionsDefinition of data structures
Recursive structures are naturally
processed by recursive functions!
Recursion
7/31/2019 Sorting and DS
146/170
146
Mahindra Satyam 2010
Very useful technique
Definition of mathematical functions Definition of data structures
Recursive structures are naturally processed by recursive functions!
Recursively defined functions factorial
Fibonacci
GCD by Euclids algorithm Fourier Transform
Games
Towers of Hanoi Chess
Recursion - Example
7/31/2019 Sorting and DS
147/170
147
Mahindra Satyam 2010
Fibonacci Numbers
fib( n ) = if ( n = 0 ) then 1else if ( n = 1 ) then 1else fib(n-1) + fib(n-2)
int fib( n ) {
if ( n < 2 ) return 1;
else return fib(n-1) + fib(n-2);
}
Pseudo-code
C
Simple, elegant solution!
Recursion - Example
7/31/2019 Sorting and DS
148/170
148
Mahindra Satyam 2010
Fibonacci Numbers
int fib( n ) {
if ( n < 2 ) return 1;
else return fib(n-1) + fib(n-2);
}
C
However, many recursive functions,egbinary search, are simple, elegant and efficient!
7/31/2019 Sorting and DS
149/170
Mahindra Satyam 2010
QUEUES
7/31/2019 Sorting and DS
150/170
Queue ADT
7/31/2019 Sorting and DS
151/170
151
Mahindra Satyam 2010
Like a stack, a queueis also a list. However, with
a queue, insertion is done at one end, whiledeletion is performed at the other end.
Accessing the elements of queues follows a First
In, First Out (FIFO) order.
Like customers standing in a check-out line in astore, the first customer in is the first customer
served.
The Queue ADT
7/31/2019 Sorting and DS
152/170
152
Mahindra Satyam 2010
Another form of restricted list
Insertion is done at one end, whereas deletion is performedat the other end
Basic operations:
enqueue: insert an element at the rear of the list
dequeue: delete the element at the front of the list
First-in First-out (FIFO) list
Enqueue and Dequeue
Primary queue operations: Enqueue and Dequeue
7/31/2019 Sorting and DS
153/170
153
Mahindra Satyam 2010
Primary queue operations: Enqueue and Dequeue
Like check-out lines in a store, a queue has a front and a rear.
Enqueue
Insert an element at the rearof the queue
Dequeue
Remove an element from the front of the queue
Insert
(Enqueue)Remove
(Dequeue) rearfront
Implementation of Queue
7/31/2019 Sorting and DS
154/170
154
Mahindra Satyam 2010
Just as stacks can be implemented as arrays or
linked lists, so with queues.Dynamic queues have the same advantages over
static queues as dynamic stacks have overstatic
stacks
Queue Implementation of Array
There are several different algorithms to implement
7/31/2019 Sorting and DS
155/170
155
Mahindra Satyam 2010
There are several different algorithms to implement
Enqueue and Dequeue
Nave way
When enqueuing, the front index is always fixed and the
rear index moves forward in the array.
front
rear
Enqueue(3)
3
front
rear
Enqueue(6)
3 6
front
rear
Enqueue(9)
3 6 9
Queue Implementation of Array
Nave way
7/31/2019 Sorting and DS
156/170
156
Mahindra Satyam 2010
Nave way When enqueuing, the front index is always fixed and the
rear index moves forward in the array. When dequeuing, the element at the front the queue is
removed. Move all the elements after it by one position.(Inefficient!!!)
Dequeue()
front
rear
6 9
Dequeue() Dequeue()
front
rear
9
rear = -1
front
7/31/2019 Sorting and DS
157/170
Mahindra Satyam 2010
TREES
The British Constitution
Crown
7/31/2019 Sorting and DS
158/170
158
Mahindra Satyam 2010
Crown
Church of
EnglandCabinet
House of
Commons
House of
Lords
Supreme
Court
Ministers
County
CouncilMetropolitan
police
County Borough
Council
Rural District
Council
More Trees Examples
U i / Wi d fil t t
7/31/2019 Sorting and DS
159/170
159
Mahindra Satyam 2010
Unix / Windows file structure
Definition of Tree
7/31/2019 Sorting and DS
160/170
160
Mahindra Satyam 2010
A tree is a finite set of one or more nodessuch that:
There is a specially designated node calledthe root.
The remaining nodes are partitioned into n>=0disjoint sets T1, ..., Tn, where each of these sets isa tree.
We call T1, ..., Tn the subtrees of the root.
Level and Depth
7/31/2019 Sorting and DS
161/170
161
Mahindra Satyam 2010
K L
E F
B
G
C
M
H I J
D
A
Level
1
2
3
4
node (13)degree of a node
leaf (terminal)
nonterminal
parent
childrensibling
degree of a tree (3)
ancestor
level of a node
height of a tree (4)
3
2 1 3
2 0 0 1 0 0
0 0 0
1
2 2 2
3 3 3 3 3 3
4 4 4
Terminology
7/31/2019 Sorting and DS
162/170
162
Mahindra Satyam 2010
The degree of a node is the number of subtrees
of the node The degree of A is 3; the degree of C is 1.
The node with degree 0 is a leaf or terminalnode.
A node that has subtrees is theparentof theroots of the subtrees.
The roots of these subtrees are the children ofthe node.
Children of the same parent are siblings.
The ancestors of a node are all the nodesalong the path from the root to the node.
Tree Properties
Property Value
7/31/2019 Sorting and DS
163/170
163
Mahindra Satyam 2010
A
B C
D
G
E F
IH
Number of nodes
HeightRoot Node
Leaves
Interior nodes
Number of levels
Ancestors of HDescendants of B
Siblings of E
Right subtree
Representation of Trees
7/31/2019 Sorting and DS
164/170
164
Mahindra Satyam 2010
List Representation( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
The root comes first, followed by a list of sub-trees
data link 1 link 2 ... link n
How many link fields areneeded in such a representation?
A Tree Node
Every tree node:
7/31/2019 Sorting and DS
165/170
165
Mahindra Satyam 2010
Every tree node:
object useful information
children pointers to its children nodes
O
O O
O
O
Left Child - Right Sibling
7/31/2019 Sorting and DS
166/170
166
Mahindra Satyam 2010
A
B C D
E F G H I J
K L M
data
left child right sibling
Tree ADT
Objects: any type of objects can be stored in a tree
7/31/2019 Sorting and DS
167/170
167
Mahindra Satyam 2010
Objects: any type of objects can be stored in a tree
Methods:accessor methods root() return the root of the tree
parent(p) return the parent of a node
children(p)returns the children of a node
query methods size()returns the number of nodes in the tree
isEmpty() - returns true if the tree is empty
elements()returns all elements
isRoot(p), isInternal(p), isExternal(p)
Tree Implementation
typedef struct tnode {
7/31/2019 Sorting and DS
168/170
168
Mahindra Satyam 2010
typedef struct tnode {
int key;
struct tnode* lchild;struct tnode* sibling;
} *ptnode;
- Create a tree with three nodes (one root & two children)
- Insert a new node (in tree with root R, as a new child at level L)- Delete a node (in tree with root R, the first child at level L)
Tree Traversal
Two main methods:
7/31/2019 Sorting and DS
169/170
169
Mahindra Satyam 2010
Two main methods: Preorder PostorderRecursive definition
PREorder:
visit the root traverse in preorder the children (subtrees)
POSTorder traverse in postorder the children (subtrees)
visit the root
Th k
7/31/2019 Sorting and DS
170/170
mahindrasatyam.com
Safe Harbor
This document contains forward-looking statements within the meaning of section 27A of Securities Act of 1933, as amended, and
section 21E of the Securities Exchange Act of 1934, as amended. The forward-looking statements contained herein are subject to
certain risks and uncertainties that could cause actual results to differ materially from those reflected in the forward-looking
statements. We undertake no duty to update any forward-looking statements. For a discussion of the risks associated with our
business, please see the discussions under the heading Risk Factors in our report on Form 6-K concerning the quarter ended
September 30, 2008, furnished to the Securities and Exchange Commission on 07 November, 2008, and the other reports filed with
the Securities and Exchange Commission from time to time. These filings are available at http://www.sec.gov
Thank you
Top Related