Sei sulla pagina 1di 170

Sorting Techniques

Mahindra Satyam 2010

Introduction to

Algorithms

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;

Mahindra Satyam 2010 3

What is an Algorithm? Finiteness: If we trace out the instructions of an algorithm, then for all cases the algorithm 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.

Mahindra Satyam 2010 4

Ex: Algorithm: To find out biggest of 10 numbers:

Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Note:

num[10] big =0,i=0 if num[i] > big big = num[i] repeat step 3 for i=1 to 10 print big end

There may be multiple solutions to a problem.

How do you find, which solution is Optimal (Better) ?

Mahindra Satyam 2010 5

Ex: Algorithm: To find out biggest of 10 numbers:

Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Note:

num[10] big =0,i=0 if num[i] > big big = num[i] repeat step 3 for i=1 to 10 print big end

There may be multiple solutions to a problem.

How do you find, which solution is Optimal (Better) ? Ans:


Mahindra Satyam 2010 6

TIME COMPLEXITY

How to Determine Complexities ?

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).

Mahindra Satyam 2010 8

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).
Mahindra Satyam 2010 9

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 the total time for the for loop is
which is O(N) overall.

O(1), N * O(1),

Mahindra Satyam 2010 10

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 Every time the outer loop executes, the inner loop executes M As a result, the statements in the inner loop execute a total of N * M 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). times. times. times.

Mahindra Satyam 2010 11

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.

Mahindra Satyam 2010 12

Nested loops -

inner loop iterations dependent on index of outer loop contd

The following table gives you, how many iterations that inner loop has
Value of i Number of iterations of inner loop

0 1 2 ... N-2 N-1

N N-1 N-2 ... 2 1

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).

Mahindra Satyam 2010 13

Ex: Calculating Time Complexity of Searching

Mahindra Satyam 2010

How to Calculate Time Complexity of Linear Search of 10, 5, 15, 6, 18 ?


Function to Find a key using Linear Search

int linearSearch(int a[],int { int i; for(i=0;i<n;i++) { if( a[i]==key) return i; } return -1; }

n,int key) 1 n n 1 1
- ignore - ignore - ignore

Unit of Time

Total

: 2n + 3

n = very large values, say 100000


Mahindra Satyam 2010

2n + 3 ~ 2n
15

How to Calculate Time Complexity of Linear Search


Big Oh (O) notation -> 2n+3 => O(n) => upperbound Best case Worst case O(1) O(n)

Average case : 1st Input 2nd Input 3rd Input nth Input

1 2 3 n n(n+1) --------- = (n+1) = O(n) 2n

1+2+3+n --------------------n

Mahindra Satyam 2010 16

Complexities of some of

SORT & SEARCH


Techniques

Mahindra Satyam 2010

Time Complexities of various SORT techniques

Technique / Case

Best Case

Average Case

Worst Case

Bubble Sort
Selection Sort Insertion Sort Merge Sort Quick Sort
Mahindra Satyam 2010

O(1)
O(1) O(1) O(n log n) O(nlog n)

O(n2)
O(n2) O(n2) O(n log n) O(n log n)

O(n2)
O(n2) O(n2) O(n log n) O(n2)

18

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)

Mahindra Satyam 2010 19

Priority of Complexities

Constant Time Logarithmic Time

O(1) O(log n)

Best

Linear Time
Quadratic Time Exponential Time Factorial Time

O(n)
O(n2) O(2n) O(n!)

O(n log n)
O(n3)

Worst

Mahindra Satyam 2010 20

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

Mahindra Satyam 2010 21

Structure of data

Mahindra Satyam 2010 22

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

Mahindra Satyam 2010 23

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.
Mahindra Satyam 2010 24

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!!

Mahindra Satyam 2010 25

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

Mahindra Satyam 2010 26

Insertion Sort

To insert 12, we need to make room for it by moving first 36 and then 24.

Mahindra Satyam 2010 27

Insertion Sort

Mahindra Satyam 2010 28

Insertion Sort

Mahindra Satyam 2010 29

Insertion Sort

input array

at each iteration, the array is divided in two sub-arrays: left sub-array

right sub-array

sorted

unsorted

Mahindra Satyam 2010 30

Insertion Sort

Mahindra Satyam 2010 31

Bubble Sort

Mahindra Satyam 2010

0 PASS - 1 5

1 7

2 6

3 4

4 3 In each pass, the adjacent elements are compared.

If the element at (j+1)th position is larger than jth element, interchange occurs

temp

Mahindra Satyam 2010 33

0 PASS - 1 5

1 6

2 4

3 3

4 7 In second pass, the adjacent elements are compared excluding the last sorted element.

PASS - 2

temp

Mahindra Satyam 2010 34

0 PASS - 1 5

1 6

2 4

3 3

4 7 In third pass, the adjacent elements are compared excluding the last sorted element.

PASS - 2

PASS - 3

temp

Mahindra Satyam 2010 35

0 PASS - 1 5

1 6

2 4

3 3

4 7 In fourth pass, the adjacent elements are compared excluding the last sorted element.

PASS - 2

PASS - 3

PASS - 4

Mahindra Satyam 2010

temp

36

Analysis
0 5 1 7 2 6 3 4 4 3 (n-1) No. of comparisons

PASS - 1

PASS - 2

(n-2)

PASS - 3

7 (1)

PASS - 4

Thus, the total no. of comparisons for the elements to be arranged in order are
Mahindra Satyam 2010

[(n-1)+(n-2)++1]= (n-1) , and hence O(n^2)


37

Merge SORT

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 smaller subproblems 3. Recursively solve the subproblems 4. Combine solutions to the subproblems
Mahindra Satyam 2010 39

Divide and Conquer - Sort

Problem: Input: A[n] unsorted array of n 1 integers. Output: A[n] sorted in non-decreasing order

Mahindra Satyam 2010 40

Divide and Conquer - Sort


Base case single element (n=1), return Divide A into two subarrays: FirstPart, SecondPart Two Subproblems:
sort the FirstPart sort the SecondPart

Recursively
sort FirstPart sort SecondPart

Combine sorted FirstPart and sorted second part


Mahindra Satyam 2010 41

Merge Sort: Idea

Divide into two halves Recursively sort

A:

FirstPart

SecondPart

FirstPart

SecondPart

Merge

A is sorted!

Mahindra Satyam 2010 42

Merge Sort: Algorithm

Merge-Sort (A, n) if n=1 return else Space: n 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) Recursive Call Merge-Sort(R, n2) Merge(A, L, n1, R, n2 )
Mahindra Satyam 2010 43

Time: n

Merge-Sort: Merge

Sorted

A: merge
Sorted

Sorted

L:
Mahindra Satyam 2010

R:

44

Merge-Sort: Merge Example

A:

L:

R: 3

Mahindra Satyam 2010 45

Merge-Sort: Merge Example

A:
3 1
k=0

15 28

10 14

L:
1 3
i=0
Mahindra Satyam 2010

R:
15 28 30 2 6 8 3 6
j=0
46

10 14 22 4 5 7

Merge-Sort: Merge Example

A:
1 2 5
k=1

15 28 30

10 14

L:
1 3 2 5
i=1
Mahindra Satyam 2010

R:
15 28 6 8 3 6
j=0
47

10 14 22 4 5 7

Merge-Sort: Merge Example

A:
1 2 3 15 28 30
k=2

10 14

L:
1 2 6
i=2
Mahindra Satyam 2010

R:
8 3 6
j=0
48

10 14 22 4 5 7

Merge-Sort: Merge Example

A:
1 2 3 4
k=3

10 14

L:
1 2 6
i=2
Mahindra Satyam 2010

R:
8 3 6 10 14 22 4 5 7
j=1
49

Merge-Sort: Merge Example

A:
1 2 3 4 5
k=4

10 14

L:
1 2 6
i=2
Mahindra Satyam 2010

R:
8 3 6 10 14 22 4 5 7
j=2
50

Merge-Sort: Merge Example

A:
1 2 3 4 5 6
k=5

10 14

L:
1 2 6
i=2
Mahindra Satyam 2010

R:
8 3 6 10 14 22 4 5 7
j=3
51

Merge-Sort: Merge Example

A:
1 2 3 4 5 6 7
k=6

14

L:
1 2 6 8
i=3
Mahindra Satyam 2010

R:
3 6 10 14 22 4 5 7
j=3
52

Merge-Sort: Merge Example

A:
1 2 3 4 5 5 7 8 14
k=7

L:
1 3 2 5 15 28 6 8
i=3
Mahindra Satyam 2010

R:
3 6 10 14 22 4 5 7
j=4
53

Merge-Sort: Merge Example

A:
1 2 3 4 5 6 7 8
k=8

L:
1 3 2 5 15 28 6 8
i=4
Mahindra Satyam 2010

R:
3 6 10 14 22 4 5 7
j=4
54

merge(A,L,n1,R,n2)

Number of iterations: (n1+n2) i j 0 for k 0 to n1+n2-1 time: c(n1+n2) for some c Total if i < n1 if j = n2 or L[i] R[j] A[k] L[i] i i + 1 else if j < n2 A[k] R[j] j j + 1

Mahindra Satyam 2010 55

Merge-Sort Execution Example


Divide
6 2 8 4 33 7 7 55 11

Mahindra Satyam 2010 56

Merge-Sort Execution Example


Recursive call , divide
3 7 5 1

88

44

Mahindra Satyam 2010 57

Merge-Sort Execution Example


Recursive call , divide
3 7 5 1

Mahindra Satyam 2010 58

Merge-Sort Execution Example


Recursive call , base case
3 7 5 1

Mahindra Satyam 2010 59

Merge-Sort Execution Example


Recursive call return
3 7 5 1

Mahindra Satyam 2010 60

Merge-Sort Execution Example


Recursive call , base case
3 7 5 1

Mahindra Satyam 2010 61

Merge-Sort Execution Example


Recursive call return
3 7 5 1

Mahindra Satyam 2010 62

Merge-Sort Execution Example


Merge
3 7 5 1

Mahindra Satyam 2010 63

Merge-Sort Execution Example


Recursive call return
3 7 5 1

Mahindra Satyam 2010 64

Merge-Sort Execution Example


Recursive call , divide
3 7 5 1

Mahindra Satyam 2010 65

Merge-Sort Execution Example


Recursive call, base case
3 7 5 1

Mahindra Satyam 2010 66

Merge-Sort Execution Example


Recursive call return
3 7 5 1

Mahindra Satyam 2010 67

Merge-Sort Execution Example


Recursive call, base case

Mahindra Satyam 2010 68

Merge-Sort Execution Example


Recursive call return
3 7 5 1

Mahindra Satyam 2010 69

Merge-Sort Execution Example


merge
3 7 5 1

Mahindra Satyam 2010 70

Merge-Sort Execution Example


Recursive call return
3 7 5 1

Mahindra Satyam 2010 71

Merge-Sort Execution Example


merge
3 7 5 1

Mahindra Satyam 2010 72

Merge-Sort Execution Example


Recursive call return
2 4 6 8 3 7 5 1

Mahindra Satyam 2010 73

Merge-Sort Execution Example


Recursive call
2 4 6 8

Mahindra Satyam 2010 74

Merge-Sort Execution Example


2 4 6 8

Mahindra Satyam 2010 75

Merge-Sort Execution Example


Recursive call return
2 4 6 8 1 3 5 7

Mahindra Satyam 2010 76

Merge-Sort Execution Example


merge
1 2 3 4 5 6 7 8

Mahindra Satyam 2010 77

Merge-Sort Analysis
Time, divide
n n

n/2

n/2

2 n/2 = n log n levels

n/4

n/4

n/4

n/4

4 n/4 = n

n/2 2 = n Total time for divide: n log n

Mahindra Satyam 2010 78

Merge-Sort Analysis
Time, merging
n cn

n/2

n/2

2 cn/2 = n log n levels

n/4

n/4

n/4

n/4

4 cn/4 = n

n/2 2c = n Total time for merging: cn log n

Total running time: order of nlogn Total Space: order of n


Mahindra Satyam 2010 79

Quick sort

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 sub arrays A[p..q] and A[q+1..r] such that each element of A[p..q] is less than 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 recursive calls to quick sort.
Combine: Since the sub arrays are sorted in place, no work is needed to combine them, the entire array A[p..r] is now sorted.

Mahindra Satyam 2010 81

QUICKSORT (A, p, r) 1. 2. 3. 4. if p < r then q PARTITION (A, p, r) QUICKSORT (A, p, q) QUICKSORT (A, q+1, r)

Note: QUICKSORT(A,0,n-1) sorts the entire array A.

Mahindra Satyam 2010 82

lb 0 1 2 3 4 5 6

ub 7 13

11

12

14

11
pivot

7 13

11

14

12

11
pivot

Mahindra Satyam 2010 83

Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7

11

12

14

13

11
pivot

down

up

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

Mahindra Satyam 2010 84

Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7

11

12

14

13

11
pivot

down

up

while(down < up) { while(x[down] <= pivot) down++;

while(x[up] > pivot)

up--;

if(down < up ) interchange x[down] with x[up] }


Mahindra Satyam 2010 85

Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7

11

12

14

13

11
pivot

down

up

while(down < up) { while(x[down] <= pivot) down++;

while(x[up] > pivot)

up--;

if(down < up ) interchange x[down] with x[up] }


Mahindra Satyam 2010 86

Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7

11

12

14

13

11
pivot

down

up

while(down < up) { while(x[down] <= pivot) down++;

while(x[up] > pivot)

up--;

if(down < up ) interchange x[down] with x[up] }


Mahindra Satyam 2010 87

Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7

11

14

12

13

11
pivot

down

up

while(down < up) { while(x[down] <= pivot) down++;

while(x[up] > pivot)

up--;

if(down < up ) interchange x[down] with x[up] }


Mahindra Satyam 2010 88

Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7

11

14

12

13

11
pivot

down

up

while(down < up) { while(x[down] <= pivot) down++; while(x[up] > pivot) up--;

if(down < up ) interchange x[down] with x[up] }


Mahindra Satyam 2010 89

Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7

11

14

12

13

11
pivot

down

up

while(down < up) { while(x[down] <= pivot) down++;

while(x[up] > pivot)

up--;

if(down < up ) interchange x[down] with x[up] }


Mahindra Satyam 2010 90

Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7

11

14

12

13

11
pivot

down

up

while(down < up) { while(x[down] <= pivot) down++;

while(x[up] > pivot)

up--;

if(down < up ) interchange x[down] with x[up] }


Mahindra Satyam 2010 91

Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7

11

14

12

13

11
pivot

down

up

while(down < up) { while(x[down] <= pivot) down++;

while(x[up] > pivot)

up--;

if(down < up ) interchange x[down] with x[up] }


Mahindra Satyam 2010 92

Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7

11

14

12

13

11
pivot

down

up

while(down < up) { while(x[down] <= pivot) down++;

while(x[up] > pivot)

up--;

if(down < up ) interchange x[down] with x[up] }


Mahindra Satyam 2010 93

Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7

11

14

12

13

11
pivot

down up

while(down < up) { while(x[down] <= pivot) down++;

while(x[up] > pivot)

up--;

if(down < up ) interchange x[down] with x[up] }


Mahindra Satyam 2010 94

Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7

11

14

12

13

11
pivot

up

down

while(down < up) { while(x[down] <= pivot) down++;

while(x[up] > pivot)

up--;

if(down < up ) interchange x[down] with x[up] }


Mahindra Satyam 2010 95

Partition Algorithm lb 0 1 2 3 4 5 6 ub 7

11

14

12

13

11
pivot

up

down

while(down < up) { while(x[down] <= pivot) down++;

while(x[up] > pivot)

up--;

if(down < up ) interchange x[down] with x[up] }


Mahindra Satyam 2010

interchange x[up] with x[lb]


96

Partition Algorithm lb 0 1 2 3 4 5 6 ub 7

11

14

12

13

11
pivot

up

down

while(down < up) { while(x[down] <= pivot) down++;

while(x[up] > pivot)

up--;

if(down < up ) interchange x[down] with x[up] }


Mahindra Satyam 2010

interchange x[up] with x[lb]


97

Quick Sort

7 13

pivot

11

12

14

11

11

14

12

13

Mahindra Satyam 2010

Quick Sort

11

12

14

13

11

14

12

13

Mahindra Satyam 2010 99

Quick Sort
0 1 2 3 4 5 6 7 13

11

12

14

11

14

12

13

Mahindra Satyam 2010 100

Quick Sort
0 1 2 3 4 5 6 7 13

11

12

14

11

14

12

13

1
Mahindra Satyam 2010 101

Datastructures

Mahindra Satyam 2010

Data structure

A data structure is an aggregation of atomic and composite data into a set with defined relationships. Structure means a set of rules that holds data together.

Mahindra Satyam 2010 103

Data structure

A combination of elements in which each is either a data type or another data structure. A set of associations or relationships (structure) involving the combined elements.

Mahindra Satyam 2010 104

Data Structure

Mahindra Satyam 2010 105

Mahindra Satyam 2010 106

Abstraction

The concept of abstraction means We know what a data type can do How it is done is hidden

Mahindra Satyam 2010 107

Abstract Data Type (ADT)

Declaration of data Declaration of operations Encapsulation of data and operations

Mahindra Satyam 2010 108

operations

Data structure

Mahindra Satyam 2010 109

Basic operations of linear lists

1. 2. 3. 4.

Insertion Deletion Retrieval Traversal

Mahindra Satyam 2010 110

Example: Banking Application


Operations are (typically): Open accounts (far less often than access) Close accounts (far less often than access) Access account to Add money Access account to Withdraw money

Mahindra Satyam 2010 111

Example: Banking Application


Teller and ATM transactions are expected to take little time. Opening or closing an account can take much longer (perhaps up to an hour).

Mahindra Satyam 2010 112

Example: Banking Application


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

Mahindra Satyam 2010 113

Example: Banking Application

1. One data structure that meets these requirements is the hash table (chapter 9). 2. Records are accessible by account number (called an exact-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 performance requiring the hash table to be reorganized).

Mahindra Satyam 2010 114

Example: City Database

1. Database system for cities and towns. 2. Users find information about a particular place by name (exact-match query) 3. Users also find all places that match a particular value (or range of values), such as location or population size (called a range query).

Mahindra Satyam 2010 115

Example: City Database

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.

Mahindra Satyam 2010 116

Example: City Database

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 never changed, a simple linear index would be more appropriate.

Mahindra Satyam 2010 117

Selecting a Data Structure

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 be supported. Quantify the resource constraints for each operation. 3. Select the data structure that best meets these requirements.

Mahindra Satyam 2010 118

Some Questions to Ask

1. Are all data inserted into the data structure at the beginning, or are insertions intersparsed with other operations? 2. Can data be deleted? 3. Are all data processed in some well-defined order, or is random access allowed?

Mahindra Satyam 2010 119

Data Structure Philosophy

Each data structure has costs and benefits. Rarely is one data structure better than another in all situations. A data structure requires: space for each data item it stores, time to perform each basic operation, programming effort.

Mahindra Satyam 2010 120

Data Structure Philosophy

Each problem has constraints on available space and time. Only after a careful analysis of problem characteristics can we know the best data structure for the task.

Bank example: Start account: a few minutes Transactions: a few seconds Close account: overnight

Mahindra Satyam 2010 121

Linked List

Mahindra Satyam 2010

Objectives

1. 2.

Understanding efficient memory utilization with Linked representation Time complexities with respect to insertion, deletion and search

Mahindra Satyam 2010 123

Linked List - Insertion at the front


first

10

200

20

300

30

NUL L

100

200

300

Consider a linked list of 3 nodes. Let first be the pointer to first node. Let us consider inserting a new node ( with data as 5 ) at the front.

Mahindra Satyam 2010 124

Linked List - Insertion at the front


first

curr

10

200

20

300

30

NULL

100
5
NU LL

200

300

150

insertAtFirst(*first,val) 1. curr = getNode(val) 2. curr->next=first

Mahindra Satyam 2010

3. first = curr
125

Linked List - Insertion at the rear

10

200

20

300

30

NUL L

100

200

300

Mahindra Satyam 2010 126

Linked List - Insertion at the rear


p

10

200

20

300

30

NUL L

100

200

300

Consider a linked list of 3 nodes. Let p be the pointer to first node.

Mahindra Satyam 2010 127

Linked List - Insertion at the rear


p t

10

200

20

300

30

NUL L

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.

Mahindra Satyam 2010 128

Linked List - Insertion at the rear


p t

10

200

20

300

30

NUL L

100

200

300

While(t->next != NULL) t = t->next;

Mahindra Satyam 2010 129

Linked List - Insertion at the rear


p t

10

200

20

300

30

NUL L

100

200

300

While(t->next != NULL) t = t->next;

Mahindra Satyam 2010 130

Linked List - Insertion at the rear


p t

10

200

20

300

30

NUL L

100

200

300

While(t->next != NULL) t = t->next; Now t points to last node in the list.


Mahindra Satyam 2010 131

Linked List - Insertion at the rear


p t

10

200

20

300

30

NULL

100

200

300
40
NULL

r <= Allocate memory for new node.

400

r->data = 40
r->next = NULL
Mahindra Satyam 2010 132

Linked List - Insertion at the rear


p t

10

200

20

300

30

NULL

100

200

300
40
NUL L

400

Make r as next node of t.


t->next = r;

Mahindra Satyam 2010 133

Linked List - Insertion in-between


first Insertion of a new node with data=25.

10

200

20

300

30

NUL L

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)

Mahindra Satyam 2010 134

Linked List - Insertion in-between


first o t

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 points to the correct position (sorted position ) for the new element and old points to its previous node.

Mahindra Satyam 2010 135

Linked List - Insertion in-between


first o t
val

10

200

20

300

30

NULL

25

100

200

300

while(temp!=NULL && temp->data <= val) old = temp; temp=temp->next;

Mahindra Satyam 2010 136

Linked List - Insertion in-between


first o t
val

10

200

20

300

30

NULL

25

100

200

300

while(temp!=NULL && temp->data <= val) old = temp; temp=temp->next;

Mahindra Satyam 2010 137

Linked List - Insertion in-between


first o t
val

10

200

20

300

30

NULL

25

100

200

300

while(temp!=NULL && temp->data <= val) old = temp; temp=temp->next;

Mahindra Satyam 2010 138

Linked List - Insertion in-between


first o t

10

200

20

300

X
curr 25

30

NULL

100

200

300
NULL

400
curr = getNode(val); curr -> next = t;
Mahindra Satyam 2010

o->next = curr;
139

STACKS

Mahindra Satyam 2010

Stacks

Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); void *pop( Stack s ); Like 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 */
Mahindra Satyam 2010 141

- add item to the top of the stack


- remove an item from the top of the stack

Stacks - Implementation

Arrays Provide a stack capacity to the constructor Flexibility limited but matches many real uses Capacity limited by some constraint Memory in your computer Size of the plate stacker, etc push, pop methods Linked list also possible Stack: basically a Collection with special semantics!

Variants of AddToC, DeleteFromC

Mahindra Satyam 2010 142

Stacks - Relevance

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

Mahindra Satyam 2010 143

Stack Frames - Functions


Program

in HLL

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); }
Context for execution of f
Mahindra Satyam 2010 144

Recursion

Very useful technique Definition of mathematical functions Definition of data structures Recursive structures are naturally processed by recursive functions!

Mahindra Satyam 2010 145

Recursion

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

Mahindra Satyam 2010 146

Recursion - Example
Fibonacci Numbers

Pseudo-code

fib( n ) = if ( n = 0 ) then 1 else if ( n = 1 ) then 1 else fib(n-1) + fib(n-2)

int fib( n ) { if ( n < 2 ) return 1; else return fib(n-1) + fib(n-2); }

Simple, elegant solution!

Mahindra Satyam 2010 147

Recursion - Example
Fibonacci Numbers

int fib( n ) { if ( n < 2 ) return 1; else return fib(n-1) + fib(n-2); }

However, many recursive functions, eg binary search, are simple, elegant and efficient!

Mahindra Satyam 2010 148

QUEUES

Mahindra Satyam 2010

Queue Overview

Queue ADT Basic operations of queue Enqueuing, dequeuing etc. Implementation of queue Array Linked list

Mahindra Satyam 2010 150

Queue ADT

Like a stack, a queue is also a list. However, with a queue, insertion is done at one end, while deletion 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 a store, the first customer in is the first customer served.

Mahindra Satyam 2010 151

The Queue ADT

Another form of restricted list Insertion is done at one end, whereas deletion is performed at 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

Mahindra Satyam 2010 152

Enqueue and Dequeue

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 rear of the queue Dequeue Remove an element from the front of the queue

Remove (Dequeue)
Mahindra Satyam 2010

front

rear

Insert (Enqueue)
153

Implementation of Queue

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 over static stacks

Mahindra Satyam 2010 154

Queue Implementation of Array

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.

rear

rear

rear

front Enqueue(3)
Mahindra Satyam 2010

front Enqueue(6)

front Enqueue(9)
155

Queue Implementation of Array

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!!!)

rear

rear

rear = -1

front Dequeue()
Mahindra Satyam 2010

front

front Dequeue()
156

Dequeue()

TREES

Mahindra Satyam 2010

The British Constitution


Crown

Church of
England Cabinet

House of Commons

House of Lords

Supreme Court

Ministers County Council Metropolitan police Rural District Council County Borough Council

Mahindra Satyam 2010 158

More Trees Examples


Unix / Windows file structure

Mahindra Satyam 2010 159

Definition of Tree
A tree is a finite set of one or more nodes such that: There is a specially designated node called the root. The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree. We call T1, ..., Tn the subtrees of the root.

Mahindra Satyam 2010 160

Level and Depth


Level

node (13) degree of a node 1 leaf (terminal) A 1 3 nonterminal 2 parent B 2 1 C 2 3 D 2 children 3 2 sibling degree of a tree (3) 4 2 E 3 0 F 3 0 G 31 H 3 0 I 3 0 J 3 ancestor level of a node height of a tree (4) 0 K4 0 L 4 0 M 4

Mahindra Satyam 2010 161

Terminology
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 terminal node. A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the children of the node. Children of the same parent are siblings. The ancestors of a node are all the nodes along the path from the root to the node.

Mahindra Satyam 2010 162

Tree Properties
Property Number of nodes Height Root Node Leaves Interior nodes Number of levels Ancestors of H Descendants of B Siblings of E Right subtree Value

Mahindra Satyam 2010 163

Representation of Trees

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 are needed in such a representation?

Mahindra Satyam 2010 164

A Tree Node
Every tree node: object useful information children pointers to its children nodes

O
Mahindra Satyam 2010 165

Left Child - Right Sibling

A left child B

data

right sibling

Mahindra Satyam 2010 166

Tree ADT

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)

Mahindra Satyam 2010 167

Tree Implementation
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)

Mahindra Satyam 2010 168

Tree Traversal

Two main methods: Preorder Postorder Recursive definition PREorder: visit the root traverse in preorder the children (subtrees) POSTorder traverse in postorder the children (subtrees) visit the root

Mahindra Satyam 2010 169

Thank you

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

Mahindra Satyam 2010 170

Potrebbero piacerti anche