Stack. S Array or Vector representing stack containing N elements

Similar documents
The time and space are the two measure for efficiency of an algorithm.

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

Insert. SEMCOM Page 1 of 11

1 P age DS & OOPS / UNIT II

Introduction to Data Structure

Fundamentals of Data Structure

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Sample Question Paper

Department of Computer Science and Technology

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

DATA STRUCTURES/UNIT 3

Answers. 1. (A) Attempt any five : 20 Marks

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

DATA STRUCTURES AND ALGORITHMS

O(n): printing a list of n items to the screen, looking at each item once.

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

3. Fundamental Data Structures

CSCE 2014 Final Exam Spring Version A

Final Exam Data Structure course. No. of Branches (5)

CS301 - Data Structures Glossary By

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

a) State the need of data structure. Write the operations performed using data structures.

INSTITUTE OF AERONAUTICAL ENGINEERING

CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar

CS 8391 DATA STRUCTURES

Basic Data Structures (Version 7) Name:

Ashish Gupta, Data JUET, Guna

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

18.3 Deleting a key from a B-tree

17CS33:Data Structures Using C QUESTION BANK

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Binary Trees. Height 1

Delhi Noida Bhopal Hyderabad Jaipur Lucknow Indore Pune Bhubaneswar Kolkata Patna Web: Ph:

Trees. (Trees) Data Structures and Programming Spring / 28

CS DATA STRUCTURES AND ALGORITHMS

DDS Dynamic Search Trees

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Mid-Term Exam. CSE2100 DS & Algorithms 1

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

1. Two main measures for the efficiency of an algorithm are a. Processor and memory b. Complexity and capacity c. Time and space d.

CS8391-DATA STRUCTURES

Cpt S 122 Data Structures. Sorting

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

Data Structure. IBPS SO (IT- Officer) Exam 2017

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

Sorting and Searching

V Advanced Data Structures

MAHARASHTRASTATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

Summer Final Exam Review Session August 5, 2009

Data Structures and Algorithms Notes

We have the pointers reference the next node in an inorder traversal; called threads

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Abstract Data Types CHAPTER 12. Review Questions

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

Sorting and Searching

V Advanced Data Structures

( D. Θ n. ( ) f n ( ) D. Ο%

MLR Institute of Technology

An Introduction to Trees

Dynamic Data Structures

B-Trees. Disk Storage. What is a multiway tree? What is a B-tree? Why B-trees? Insertion in a B-tree. Deletion in a B-tree

(2,4) Trees. 2/22/2006 (2,4) Trees 1

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Data Structures and Algorithms

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

12 Abstract Data Types

UNIT IV 4 LINKED LIST

DATA STRUCTURE UNIT I

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

Linked List. April 2, 2007 Programming and Data Structure 1

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

Search Trees - 1 Venkatanatha Sarma Y

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

Priority Queues 1 / 15

DC54 DATA STRUCTURES DEC 2014

CS24 Week 8 Lecture 1

Chapter 17 Indexing Structures for Files and Physical Database Design

Lecture 6 Sorting and Searching

Draw a diagram of an empty circular queue and describe it to the reader.

Module 04 Trees Contents

Data Structure Advanced

Lecture 6: Analysis of Algorithms (CS )

[ DATA STRUCTURES ] Fig. (1) : A Tree

DEEPIKA KAMBOJ UNIT 2. What is Stack?

CSE 230 Intermediate Programming in C and C++

2 Marks Questions & Answers

CMSC351 - Fall 2014, Homework #2

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

MODULE 3: LINKED LIST

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Transcription:

Stack Algorithm to Insert an element in a St ack: Procedure PUSH(S, TOP, X) X Value of element to be inserted in stack TOP A pointer ( or index) pointing to the top element of stack S Array or Vector representing stack containing N elements 1) [ Check for stack overflow] If TOP N then Write ( Stack Overflow ) 2) [ Increment TOP ] TOP TOP + 1 3) [ Insert element ] S[TOP] X 4) [ Finished ] Algorithm to Delete an element from a St ack: Function POP(S, TOP) TOP A pointer ( or index) pointing to the top element of stack S Array or Vector representing stack containing N elements 1) [ Check for underflow on stack ] If TOP = 0 then Write ( Stack Underflow on POP ) 2) [ Decrement Pointer TOP ] TOP TOP - 1 3) [ former top eleme nt of stack ] ( S[ TOP + 1]) Algorithm to obtain value of ith element from the top of the St ack: Function PEEP(S, TOP, I) TOP A pointer ( or index) pointing to the top element of stack S Array or Vector representing stack containing N elements I Index of element from T OP of the stack 1) [ Check for underflow on stack ] If TOP I + 1 0 then Write ( Stack Underflow on PEEP ) 2) [ Ith element from top of stack ] ( S[ TOP I + 1]) 1 / 4

Algorithm to change the content of ith element from the top of the Stack: Function CHANGE(S, TOP, X, I) TOP A pointer ( or index) pointing to the top element of stack S Array or Vector representing stack containing N elements I Index of element from T OP of the stack X Value of element to be change in the stack 1) [ Check for underflow on stack ] If TOP I + 1 0 then Write ( Stack Underflow on PEEP ) 2) [ Change Ith element from top of stack ] S[TOP I + 1] X 3) [ Finished ] Queue Algorithm to Insert an element in a Queue: Procedure QINSERT(Q, F, R, N, Y) Y Value of element to be inserted in queue F A pointer ( or index) pointing to the front element of queue R A pointer ( or index) pointing to the rear element of queue Q Array or Vector representing queue containing N elements This procedure insert Y at the rear of the queue. Prior to the first invocation of the procedure QINSERT, F and R have been set to zero. 1) [ Overflow? ] If R N then Write ( OVERFLOW ) 2) [ Increment rear pointer ] R R + 1 3) [ Insert element ] Q[R] Y 4) [ Is front pointer properly set? ] If F = 0 then F 1 Algorithm to Delete an element from a Queue: Function QDELETE(Q, F, R) F A pointer ( or index) pointing to the front element of queue R A pointer ( or index) pointing to the rear element of queue Q Array or Vector representing queue containing N elements 2 / 4

Y Temporary variable to store value of deleted element of queue This function deletes and return the front element of the queue 1) [ Underflow? ] If F = 0 then Write ( UNDERFLOW ) (0) 2) [ Delete element] Y Q[F] 3) [ Queue empty? ] If F = R then F R 0 else F F + 1 4) [ element ] (Y) Algorithm to Insert an element in a Ci rcular Queue: Procedure CQINSERT(F, R, Q, N, Y) Y Value of element to be inserted in queue F A pointer ( or index) pointing to the front element of queue R A pointer ( or index) pointing to the rear element of queue Q Array or Vector representing queue containing N elements This procedure insert Y at the rear of the que ue. Prior to the first invocation of the procedure CQINSERT, F and R have been set to zero. 1) [ Reset rear pointer? ] If R = N then R 1 else R R + 1 2) [ Overflow? ] If F = R then Write ( OVERFLOW ) 3) [ Insert element ] Q[R] Y 4) [ Is front pointer properly set? ] If F = 0 then F 1 Algorithm to Delete an element from a Circular Queue: Function CQDELETE(Q, F, R, N) F A pointer ( or index) pointing to the front element of queue R A pointer ( or index) pointing to the rear element of queue Q Array or Vector representing queue containing N elements 3 / 4

Y Temporary variable to store value of deleted element of queue This function deletes and return the front element of the queue 1) [ Underflow? ] If F = 0 then Write ( UNDERFLOW ) (0) 2) [ Delete element] Y Q[F] 3) [ Queue empty? ] If F = R then F R 0 (Y) 4) [ Increment front pointer ] If F = N then F 1 else F F +1 (Y) 4 / 4

Singly Linked List Algorithm to Insert New Node at the Beginning of the Linked List: Function INSERT(X, FIRST ): X Value of new element stored in I NFO part of newly created Node FIRST A pointer to the First Node of a Linked Linear List AVAIL A pointer to the top Node of the Free Node List NEW Temporary pointer pointing to newly created node Node contains INFO and LINK Fields 1) [ Underflow? Or Not Enough Memory] If AVAIL = NULL then Write ( Availability Stack Underflow ) ( FIRST) 2) [ Obtain address of next Free Node ] NEW AVAIL 3) [ Remove Free Node from availability stack ] AVAIL LINK (AVAIL) 4) [ Initialize fields of new Node and its link to the List ] INFO (NEW) X LINK (NEW) FIRST 5) [ address of new Node ] ( NEW) (Or FIRST NEW if we are not returning address from Function) When function INSERT is invoked, it returns a pointer value to the variable FIRST. i.e. FIRST INSERT(X, FIRST) Algorithm to Insert New Node at the End of the Linked List: Function INSEND(X, FIRST): X Value of new element stored in I NFO part of newly created Node FIRST A pointer to the First Node of a Linked Linear List AVAIL A pointer to the top Node of the Free Node List NEW Temporary pointer pointing to newly created node SAVE - Temporary pointer required for Traversing the List to reach the last Node Node contains INFO and LINK Fields 1) [ Underflow? Or Not Enough Memory] If AVAIL = NULL then Write ( Availability Stack Underflow ) ( FIRST) 2) [ Obtain address of next Free Node ] NEW AVAIL 3) [ Remove Free Node from availability stack ] AVAIL LINK (AVAIL) 4) [ Initialize fields of new Node and its link to the List ] INFO (NEW) X 1 / 4

LINK (NEW) NULL 5) [ Is the List empty ] If FIRST = NULL then ( NEW) 6) [ Initialize search for the Last Node ] SAVE FIRST 7) [ Search for End of the List ] Repeat while LINK ( SAVE) NULL SAVE LINK (SAVE) 8) [ Set LINK field of last Node to NEW ] LINK (SAVE) NEW 9) [ first Node pointer ] ( FIRST) When function INSE ND is invoked, it returns a pointer value to the variable FIRST. i.e. FIRST INSEND(X, FIRST) Algorithm to Insert New Node into an Ordered Linked List: Function INSORD(X, FIRST): X Value of new element stored in I NFO part of newly created Node FIRST A pointer to the First Node of a Linked Linear List AVAIL A pointer to the top Node of the Free Node List NEW Temporary pointer pointing to newly created node SAVE - Temporary pointer required for Traversing the List to reach the last Node Node contains INFO and LINK Fields 1) [ Underflow? Or Not Enough Memory] If AVAIL = NULL then Write ( Availability Stack Underflow ) ( FIRST) 2) [ Obtain address of next Free Node ] NEW AVAIL 3) [ Remove Free Node from availability stack ] AVAIL LINK (AVAIL) 4) [ Copy information contents into new Node ] INFO (NEW) X 5) [ Is the List empty ] If FIRST = NULL then LINK (NEW) NULL ( NEW) 6) [ Does the new Node precede all others in the List? ] If INFO (NEW) INFO (FIRST) then LINK (NEW) FIRST ( NEW) 7) [ Initialize temporary pointer ] 2 / 4

SAVE FIRST 8) [ Search for Predecessor of new Node ] Repeat while LINK (SAVE) NULL and INFO (LINK (SAVE)) INFO (NEW) SAVE LINK (SAVE) 9) [ Set LINK field of new Node and its Predecessor ] LINK ( NEW) LINK (SAVE) LINK (S AVE) NEW 10) [ first Node pointer ] ( FIRST) When function INS ORD is invoked, it returns a pointer value to the variable FIRST. i.e. FIRST INSORD(X, FIRST) Algorithm to Delete Node from Linked List: Procedure DELETE(X, FIRST): X A pointer to the Node in the Linked List, which we want to delete FIRST A pointer to the First Node of a Linked Linear List AVAIL A pointer to the top Node of the Free Node List TEMP Temporary pointer used to find the desired Node PRED - Pointer that keeps track of the predecessor of T EMP Node contains INFO and LINK Fields 1) [ Empty list?] If FIRST = NULL then Write ( Underflow ) 2) [ Initialize search for X ] TEMP FIRST 3) [ Find X ] Repeat thru step 5 while TEMP X and LINK ( TEMP) NULL 4) [ Update predecessor marker] PRED TEMP 5) [ Move to next Node ] TEMP LINK (TEMP) 6) [ End of the list ] If TEMP X then Write ( Node NOT Found ) 7) [ Delete X ] If X = FIRST ( Is X the first Node?) then FIRST LINK (FIRST) else LINK (PRED) LINK (X) 8) [ Node to availability stack ] LINK (X) AVAIL AVAIL X 3 / 4

FIRST is changed only when X is pointing to the first Node of the list. Procedure DELETE assumes that the address of the Node to be deleted is known initially. Algorithm to Copy Linked List: Function COPY(FIRST): FIRST A pointer to the First Node of a Linked Linear List, whose copy is made AVAIL A pointer to the top Node of the Free Node List BEGIN A pointer to the First Node of newly created Linked List NEW, PRED, SAVE Temporary pointers Existing Linked List Node contains INFO and LINK Fields New Linked List Node contains FIELD and PTR Fields 1) [ Empty list?] If FIRST = NULL then ( NULL) 2) [ Copy first node ] If AVAIL = NULL then Write ( Availa bility Stack Underflow ) ( 0) else NEW AVAIL AVAIL LINK (AVAIL) FIELD (NEW) INFO (FIRST) BEGIN NEW 3) [ Initialize traversal ] SAVE FIRST 4) [ Move to next node if not at end of list ] Repeat thru step 6 while LINK ( SAVE) NULL 5) [ Update predecessor and save pointers ] PRED NEW SAVE LINK (SAVE) 6) [ Copy node ] If AVAIL = NULL then Write ( Availability Stack Underflow ) (0) Else NEW AVAIL AVAIL LINK (AVAIL) FIELD (NEW) INFO (SAVE) PTR (PRED) NEW 7) [ Set link of last node and return ] PTR (NEW) NULL ( BEGIN) 4 / 4

Doubly Linked List Algorithm to Insert New Node to the left of a given Node in Doubly Linked Linear List: Procedure DOUBINS( L, R, M, X): X Value of new element stored in I NFO part of newly created Node L A pointer to the Left- most Node of a Linked Linear List R A pointer to the Right- most Node of a Linked Linear List M A pointer pointing to the Node, to the left new Node is inserted NEW Temporary pointer pointing to newly created node Node contains INFO, LPTR ( Left Link) and RPTR ( Right Link) Fields 1) [ Obtain New Node from availability Stack ] NEW <= NODE 2) [ Copy I nformation field ] INFO (NEW) X 3) [ Insertion into an empty List? ] If R = NULL then LPTR (NEW) RPTR (NEW) NULL L R NEW 4) [ Left- most I nsertion ] If M = L then LPTR (NEW) NULL RPTR (NEW) M LPTR (M) NEW L NEW 5) [ Insert in M iddle ] LPTR (NEW) LPTR (M) RPTR (NEW) M LPTR (M) NEW RPTR (LPTR (NEW)) NEW Algorithm to Delete a Node from Doubly Linked Linear List: Procedure DOUBDEL(L, R, OLD) L A pointer to the Left- most Node of a Linked Linear List R A pointer to the Right- most Node of a Linked Linear List OLD A pointer pointing to the Node to be deleted Node contains INFO, LPTR( Left Link) and RPTR( Right Link) Fields 1) [ Underflow? ] If R = NULL then Write( Underflow ) 1 / 2

2) [ Delete node ] If L = R ( Single Node in List) then L R NULL else If OLD = L ( Left- most Node being Deleted) then L RPTR (L) LPTR (L) NULL else If OLD = R ( Right- most Node being Deleted) then R LPTR (R) RPTR (R) NULL else RPTR (LPTR (OLD)) RPTR (OLD) LPTR (RPTR (OLD)) LPTR (OLD) 3) [ deleted Node ] Restore( OLD) 2 / 2

Tree Algorithm for a pre-order traversal of a binary tree (Iterative) Procedure PREORDER(T) : T A pointer to the Root Node of Binary Tree P A pointer pointing to the current node in the Tree S An auxiliary Stack used to store Node addresses TOP Top index of Stack Node contains DATA and two pointers LPTR and RPTR DATA Information associated with a Node LPTR A pointer to Left sub tree RPTR A pointer to Right sub tree 1) [ Initialize ] If T = NULL then Write( EMPTY TREE ) else TOP 0 Call PUSH( S, TOP, T) 2) [ Process each stacked branch address ] Repeat step 3 while TOP > 0 3) [ Get stored address and branch left ] P POP(S, TOP) Repeat while P NULL 4) [ Finished ] Write( DATA( P)) If RPTR( P) NULL then Call PUSH( S, TOP, RPTR( P)) [ P LPTR(P) [ branch left ] store address of nonempty right sub tree ] Algorithm for a post-order traversal of a binary tree (Iterative) Procedure POSTORDER(T): T A pointer to the Root Node of Binary Tree P A pointer pointing to the current node in the Tree S An auxiliary Stack used to store Node addresses TOP Top index of Stack Node contains DATA and two pointers LPTR and RPTR DATA Information associated with a Node LPTR A pointer to Left sub tree RPTR A pointer to Right sub tree 1) [ Initialize ] 1 / 5

If T = NULL then Write( EMPTY TREE ) else P T TOP 0 2) [ Traverse in post- order ] Repeat thru step 5 while true 3) [ Descend left ] Repeat while P NULL Call PUSH( S, TOP, P) P LPTR(P) 4) [ Process a node whose left and right sub trees have been traversed ] Repeat while S [ TOP] < 0 P POP(S, TOP) Write(DATA (P)) If TOP = 0 ( Have all nodes been processed? ) then 5) [ Branch right and then mark node from which we branched ] P RPTR(S[TOP]) S [TOP] -S[TOP] Algorithm for a pre-order traversal of a binary tree (Recursive) Procedure RPREORDER(T): T A pointer to the Root Node of Binary Tree Node contains DATA and two pointers LPTR and RPTR DATA Information associated with a Node LPTR A pointer to Left sub tree RPTR A pointer to Right sub tree 1) [ Process the root node ] If T NULL then Write( DATA( T)) else Write( EMPTY TREE ) 2) [ Process the left sub tree ] If LPTR( T) NULL then Call RPREORDER( LPTR( T)) 3) [ Process the right sub tree ] If RPTR( T) NULL then Call RPREORDER( RPTR( T)) 4) [ Finished ] 2 / 5

Algorithm for an in-order traversal of a binary tree (Recursive) Procedure RINORDER(T): T A pointer to the Root Node of Binary Tree Node contains DATA and two pointers LPTR and RPTR DATA Information associated with a Node LPTR A pointer to Left sub tree RPTR A pointer to Right sub tree 1) [ Check for empty tree ] If T = NULL then Write( EMPTY TREE ) 2) [ Process the left sub tree ] If LPTR( T) NULL then Call RINORDER( LPTR( T)) 3) [ Process the root node ] Write( DATA( T)) 4) [ Process the right sub tree ] If RPTR( T) NULL then Call RINORDER( RPTR( T)) 5) [ Finished ] Algorithm for a post-order traversal of a binary tree (Recursive) Procedure RPOSTORDER(T): T A pointer to the Root Node of Binary Tree Node contains DATA and two pointers LPTR and RPTR DATA Information associated with a Node LPTR A pointer to Left sub tree RPTR A pointer to Right sub tree 1) [ Check for empty tree ] If T = NULL then Write( EMPTY TREE ) 2) [ Process the left sub tree ] If LPTR( T) NULL then Call RPOSTORDER( LPTR( T)) 3) [ Process the right sub tree ] If RPTR( T) NULL then Call RPOSTORDER( RPTR( T)) 3 / 5

4) [ Process the root node ] Write( DATA( T)) 5) [ Finished ] Algorithm for deletion of a node from a lexically ordered binary tree Procedure TREE_DELETE(HEAD, X) : HEAD Address of tree X Information value ( DATA) of the node marked for deletion PARENT Pointer to the parent of the node marked for deletion CUR Address of the node to be deleted. PRED & SUC Pointers to find the inorder successor of CUR. Q Address of the node to which either the left or right link of the parent of X must be assigned in order to complete the deletion. D Direction from the parent node to the node marked for deletion. FOUND Boolean variable indicates whether the node marked for deletion has been found or not Node contains DATA and two pointers LPTR and RPTR DATA Information associated with a Node LPTR A pointer to Left sub tree RPTR A pointer to Right sub tree 1) [ Initialize ] If LPTR( HEAD) HEAD then CUR LPTR(HEAD) PARENT HEAD D L else Write( NODE NOT FOUND ) 2) [ Search for the node marked for deletion ] FOUND false Repeat while not FOUND AND CUR NULL If DATA( CUR) = X then FOUND true else If X < DATA( CUR) then ( branch left) PARENT CUR CUR LPTR(CUR) D L else ( branch right) PARENT CUR CUR RPTR(CUR) 4 / 5

D R If FOUND = false then Write( NODE NOT FOUND ) 3) [ Perform the indicated deletion and restructure the tree ] If LPTR( CUR) = NULL then ( empty left sub tree) Q RPTR(CUR) else If RPTR( CUR) = NULL then ( empty right sub tree) Q LPTR(CUR) else ( check right child for successor) SUC RPTR(CUR) If LPTR( SUC) = NULL then LPTR(SUC) LPTR(CUR) Q SUC else ( search for successor of CUR) PRED RPTR(CUR) SUC LPTR(PRED) Repeat while LPTR( SUC) NULL PRED SUC SUC LPTR(PRED) ( connect successor) LPTR(PRED) RPTR(SUC) LPTR(SUC) LPTR(CUR) RPTR(SUC) RPTR(CUR) Q SUC ( Connect parent of X to its replacement) If D = L then LPTR(PARENT) Q else RPTR(PARENT) Q 5 / 5

Sorting Algorithm for a Selection Sort Method Procedure SELECTION_SORT(K,N) : K Vector or Array to store elements N No of elements in Vector( or Array) K PASS Pass index or Pass counter MIN_INDEX Position of the smallest element in a particular pass I Index of Vector K 1) [ Loop on pass index ] Repeat thru step 4 for PASS = 1, 2,., N-1 2) [ Initialize minimum index ] MIN_INDEX PASS 3) [ Make a pass and obtain element with smallest value ] Repeat for I = PASS+1, PASS+2,.., N If K[ I] < K[ MIN_INDEX] then MIN_INDEX I 4) [ Exchange elements ] If MIN_INDEX PASS then K[PASS] K[MIN_INDEX] 5) [ Finished ] Algorithm for a Bubble Sort Method Procedure BUBBLE_SORT(K,N) : K Vector or Array to store elements N No of elements in Vector( or Array) K PASS Pass index or Pass counter LAST Indicate the position of the last unsorted element EXCHS Count the number of exchanges made on any pass I Index of Vector K 1) [ Initialize ] LAST N (entire list assumed unsorted at this position) 2) [ Loop on p ass index ] Repeat thru step 5 for PASS = 1, 2,.., N-1 3) [ Initialize exchanges counter for this pass ] EXCHS 0 4) [ Perform pair wise comparisons on unsorted elements ] Repeat for I = 1, 2,.., LAST-1 If K[I] > K[I+1] then K[I] K[I+1] EXCHS EXCHS + 1 1 / 5

5) [ Were any exchanges made on this pass? ] If EXCHS = 0 then ( sorting completed, return early) else LAST LAST - 1 (reduce size of unsorted list) 6) [ Finished ] ( maximum number of passes required) Algorithm for a Simple Merge Sort Method Procedure SIMPLE_MERGE(K, FIRST, SECOND, THIRD) : K Two ordered sub tables stored in Vector k( or Array) FIRST Start index of Table 1 ( Table 1 end at SECOND - 1) SECOND Start index of Table 2 THIRD End index of Table 2 TEMP Temporary Ve ctor use in merging process I Index of Table 1 J Index of Table 2 L Index of Vector TEMP 1) [ Initialize ] I FIRST J SECOND L 0 2) [ Compare corresponding elements and output the smallest ] Repeat while I < SECOND and J T HIRD If K[I] K[J] then L L + 1 TEMP[L] K[I] I I + 1 else L L + 1 TEMP[L] K[J] J J + 1 3) [ Copy the remaining unprocessed elements in output area ] If I SECOND then Repeat while J THIRD L L + 1 TEMP[L] K[J] J J + 1 else Repeat while I < SECOND L L + 1 TEMP[L] K[I] I I + 1 2 / 5

4) [ Copy elements in temporary vector into original area ] Repeat for I = 1, 2,.., L K[FIRST 1 + I] TEMP[I] 5) [ Finished ] Algorithm for a Quick Sort Method Procedure Quick_SORT(K, LB, UB) : K Vector or Array to store elements N No of elements in Vector( or Array) K LB Lower bound of the current sub table being processed. UB Upper bound of the current sub table being processed. I & J Indices used to select certain keys during the process ing of each sub table. KEY Key value which is being placed in its final position within the sorted subtable. FLAG Logical variable which indicates the end of the process that places a record in its final position. 1) [ Initialize ] FLAG true 2) [ Perform sort ] If LB < UB then I LB J UB + 1 KEY K[LB] Repeat while FLAG I I + 1 Repeat while K[ I] < KEY ( scan the keys from left to right) I I + 1 J J 1 Repeat while K[ J] > KEY ( scan the keys from right to left) J J 1 If I < J then K[I] K[J] (interchange records) else FLAG false K[LB] K[J] (interchange records) Call QUICK_SORT( K, LB, J 1) ( sort first sub table) Call QUICK_SORT( K, J+1, UB) ( sort second sub table) 3) [ Finished ] 3 / 5

Searching Algorithm for a Linear(or Sequential) Search Method Function LINEAR_SEARCH(K, N, X) : K Unordered Vector consisting of N+1 element N Number of elements in Vector K X Value of element to be searched K[ N+1] Vector element serves as a sentinel element and receives the value of X prior to the search This function returns the index of the vector element if the search is successful, and returns 0 otherwise 1) [ Initialize search ] I 1 K[N+1] X 2) [ Search the vector ] Repeat while K[ I] X I I + 1 3) [ Successful search? ] If I = N+1 then Write( UNSUCCESSFUL SEARCH ) ( 0) else Write( SUCCESSFUL SEARCH ) ( I) Algorithm for a Binary Search Method(Iterative) Function BINARY_SEARCH(K, N, X) : K Vector K, consisting of N element s in ascending order N Number of elements in Vector K X Value of element to be searched LOW Lower index of the search interval MIDDLE M iddle index of the search interval HIGH Upper inde x of the search interval This function returns the index of the vector element if the search is successful, and returns 0 otherwise 1) [ Initialize search ] LOW 1 HIGH N 2) [ Perform search ] Repeat thru step 4 while LOW HIGH 3) [ Obtain index of midpoint of interval ] 4 / 5

MIDDLE (LOW + HIGH) / 2 4) [ Compare ] If X < K[ MIDDLE] then HIGH MIDDLE 1 else If X > K[ MIDDLE] then LOW MIDDLE + 1 else Write( SUCCESSFUL SEARCH ) ( MIDDLE) 5) [ Unsuccessful search ] Write( SUCCESSFUL SEARCH ) ( 0) Algorithm for a Binary Search Method(Recursive) Function BINARY_SEARCH_R(P, Q, K, X) : K Vector K, consisting of N elements in ascending order N Number of elements in Vector K X Value of element to be searched LOC Stores the index of the search element MIDDLE M iddle index of the search interval P & Q Bounds of a sub table This function returns the index of the vector element if the search is successful, and returns 0 otherwise 1) [ Search vector K between P and Q for value X ] If P > Q then LOC 0 else MIDDLE (P + Q) / 2 If X < K[ MIDDLE] then LOC BINARY_SEARCH_R(P, MIDDLE -1, K, X) else If X > K[ MIDDLE] then LOC BINARY_SEARCH_R(MIDDLE+1, Q, K, X) else LOC MIDDLE 2) [ Finished ] ( LOC) With initial call POSITION BINARY_SEARCH_R( 1, N, K, X) 5 / 5