Data Structure and Algorithms Design BITS Pilani Campus
Data Structure and Algorithms Design BITS Pilani Campus Dr. Maheswari Karthikeyan Lecture 3
Selection Sort Alg. : SELECTION-SORT(A) 8 n ← length[A] for j ← 1 to n - 1 do smallest ← j for i ← j + 1 to n do if A[i] < A[smallest] then smallest ← i exchange A[j] ↔ A[smallest] 4 6 9 2 3 1 BITS Pilani, Pilani Campus
Analysis of Selection Sort Alg. : SELECTION-SORT(A) n ← length[A] for j ← 1 to n - 1 » n 2/2 do smallest ← j comparisons » n exchanges for i ← j + 1 to n do if A[i] < A[smallest] then smallest ← i exchange A[j] ↔ A[smallest] cost c 1 times 1 c 2 n c 3 n-1 c 4 c 5 c 6 c 7 n-1 BITS Pilani, Pilani Campus
Divide-and-Conquer Divide the problem into a number of sub-problems – Similar sub-problems of smaller size Conquer the sub-problems – Solve the sub-problems recursively – Sub-problem size small enough solve the problems in straightforward manner Combine the solutions to the sub-problems – Obtain the solution for the original problem BITS Pilani, Pilani Campus
Merge Sort Approach To sort an array A[p. . r]: Divide – Divide the n-element sequence to be sorted into two subsequences of n/2 elements each Conquer – Sort the subsequences recursively using merge sort – When the size of the sequences is 1 there is nothing more to do Combine – Merge the two sorted subsequences BITS Pilani, Pilani Campus
Merge Sort p Alg. : MERGE-SORT(A, p, r) if p < r then q ← (p + r)/2 1 r q 2 3 4 5 6 7 8 5 2 4 7 1 3 2 6 Check for base case Divide MERGE-SORT(A, p, q) Conquer MERGE-SORT(A, q + 1, r) Conquer MERGE(A, p, q, r) Combine Initial call: MERGE-SORT(A, 1, n) BITS Pilani, Pilani Campus
Example – n Power of 2 1 2 3 4 5 6 7 8 5 2 4 7 1 3 2 6 q=4 Example 1 1 2 3 4 5 6 7 8 5 2 4 7 1 3 2 6 BITS Pilani, Pilani Campus
Example – n Power of 2 1 1 1 2 3 4 5 6 7 8 1 2 2 3 4 5 6 7 8 2 4 5 7 1 2 3 6 2 3 4 5 6 7 8 2 5 4 7 1 3 2 6 1 2 3 4 5 6 7 8 5 2 4 7 1 3 2 6 BITS Pilani, Pilani Campus
Example – n Not a Power of 2 q=3 1 2 3 4 5 6 7 8 9 4 7 2 6 1 4 7 3 5 10 2 1 2 3 4 5 6 7 8 9 4 7 2 6 1 4 7 3 5 1 2 4 5 7 8 4 7 6 1 7 3 11 q=6 6 10 11 2 6 10 2 q=9 11 6 BITS Pilani, Pilani Campus
Example – n Not a Power of 2 1 2 3 4 5 6 7 8 9 1 2 2 3 4 4 5 6 6 10 7 1 2 3 4 5 6 7 8 9 1 2 4 4 6 7 2 3 5 1 2 3 4 5 6 7 8 9 2 4 7 1 4 6 3 5 7 1 2 3 4 5 6 7 8 9 4 7 2 1 6 4 3 7 5 1 2 4 5 7 8 4 7 6 1 7 3 11 7 10 11 6 7 10 2 11 6 BITS Pilani, Pilani Campus
Merging Input: Array A and indices p, q, r such that p≤q<r – Subarrays A[p. . q] and A[q + 1. . r] are sorted Output: One single sorted subarray A[p. . r] p 1 r q 2 3 4 5 6 7 8 2 4 5 7 1 2 3 6 BITS Pilani, Pilani Campus
Merging Idea for merging: – Two piles of sorted cards • Choose the smaller of the two top cards • Remove it and place it in the output pile – Repeat the process until one pile is empty – Take the remaining input pile and place it face-down onto the output pile BITS Pilani, Pilani Campus
MERGE(A, 9, 12, 16) p q r BITS Pilani, Pilani Campus
Example: MERGE(A, 9, 12, 16) BITS Pilani, Pilani Campus
Example (cont. ) BITS Pilani, Pilani Campus
Example (cont. ) BITS Pilani, Pilani Campus
Example (cont. ) Done! BITS Pilani, Pilani Campus
Merge - Pseudocode p 1 r q 2 3 4 5 6 7 8 Alg. : MERGE(A, p, q, r) 2 4 5 7 1 2 3 6 1. Compute n 1 and n 2 n 1 the next nn 2 2. Copy the first n 1 elements into L[1. . n 1 + 1] and 2 elements into R[1. . n 2 + 1] 3. L[n 1 + 1] ← ; R[n 2 + 1] ← p q 4. i ← 1; j ← 1 2 4 5 7 L 5. for k ← p to r q+1 r 6. do if L[ i ] ≤ R[ j ] R 1 2 3 6 7. then A[k] ← L[ i ] 8. i ←i + 1 9. else A[k] ← R[ j ] 10. j←j+1 BITS Pilani, Pilani Campus
Running Time of Merge Initialization (copying into temporary arrays): – (n 1 + n 2) = (n) Adding the elements to the final array (the last for loop): – n iterations, each taking constant time (n) Total time for Merge: – (n) BITS Pilani, Pilani Campus
Analyzing Divide-and Conquer Algorithms The recurrence is based on the three steps of the paradigm: – – T(n) – running time on a problem of size n Divide the problem into a subproblems, each of size n/b: takes D(n) Conquer (solve) the subproblems a. T(n/b) Combine the solutions C(n) T(n) = (1) a. T(n/b) + D(n) + C(n) if n ≤ c otherwise BITS Pilani, Pilani Campus
MERGE-SORT Running Time Divide: – compute q as the average of p and r: D(n) = (1) Conquer: – recursively solve 2 subproblems, each of size n/2 2 T (n/2) Combine: – MERGE on an n-element subarray takes (n) time T(n) = (1) 2 T(n/2) + (n) C(n) = (n) if n =1 if n > 1 BITS Pilani, Pilani Campus
Solve the Recurrence T(n) = c if n = 1 2 T(n/2) + cn if n > 1 Use Master’s Theorem: Compare n with f(n) = cn Case 2: T(n) = Θ(nlgn) BITS Pilani, Pilani Campus
Abstract Data Types (ADT) • A data type can be considered abstract when it is defined in terms of operations on it, and its implementation is hidden. • Examples of ADT List Stack Queue BITS Pilani, Pilani Campus
Array Data Structure • An array is a sequenced collection of elements, normally of the same data type. – Single-dimensional – Multi-dimensional BITS Pilani, Pilani Campus
Memory layout • The index in an one-dimensional array directly define the relative positions of the element in actual memory. • two-dimensional array is stored in memory using row -major or column-major storage BITS Pilani, Pilani Campus
We have stored the two-dimensional array students in memory. The array is 100 × 4 (100 rows and 4 columns). Show the address of the element students[5][3] assuming that the element student[1][1] is stored in the memory location with address 1000 and each element occupies only one memory location. The computer uses row-major storage. BITS Pilani, Pilani Campus
Operations on array • The common operations on arrays as structures are searching, insertion, deletion and traversal. • An array is more suitable when the number of deletions and insertions is small, but a lot of searching and retrieval activities are expected. BITS Pilani, Pilani Campus
Example We have stored the two-dimensional array students in memory. The array is 100 × 4 (100 rows and 4 columns). Show the address of the element students[5][3] assuming that the element student[1][1] is stored in the memory location with address 1000 and each element occupies only one memory location. The computer uses row-major storage. Solution We can use the following formula to find the location of an element, assuming each element occupies one memory location. If the first element occupies the location 1000, the target element occupies the location 1018. 11. 28 BITS Pilani, Pilani Campus
Linked Lists • A linked list is a collection of data in which each element contains the location of the next element. • Each element contains two parts: data and link. The name of the list is the same as the name of this pointer variable. BITS Pilani, Pilani Campus
Operations on linked lists • • Search Insertion Deletion Traversal BITS Pilani, Pilani Campus
Search operation Moving of pre and cur pointers in searching a linked list BITS Pilani, Pilani Campus
Search operation Values of pre and cur pointers in different cases BITS Pilani, Pilani Campus
Insertion Four cases can arise: • Inserting into an empty list. • Insertion at the beginning of the list. • Insertion at the end of the list. • Insertion in the middle of the list. BITS Pilani, Pilani Campus
Insertion Inserting a node at the beginning of a linked list BITS Pilani, Pilani Campus
Insertion Inserting a node at the end of the linked list BITS Pilani, Pilani Campus
Insertion Inserting a node in the middle of the linked list BITS Pilani, Pilani Campus
Deletion Two cases are: • deleting the first node • deleting any other node. BITS Pilani, Pilani Campus
Deletion Deleting the first node of a linked list BITS Pilani, Pilani Campus
Deletion Deleting a node at the middle or end of a linked list BITS Pilani, Pilani Campus
Traversal BITS Pilani, Pilani Campus
Linked List - Applications • It is a dynamic data structure in which the list can start with no nodes and then grow as new nodes are needed • It is a suitable structure if a large number of insertions and deletions are needed, but searching a linked list is slower that searching an array. • It is a very efficient data structure for sorted list that will go through many insertions and deletions BITS Pilani, Pilani Campus
Linked List - Operations BITS Pilani, Pilani Campus
Linked List - Operations BITS Pilani, Pilani Campus
Linked List - Operations BITS Pilani, Pilani Campus
Variations of the Linked List Singly linked list: It has only head part and corresponding references to the next nodes. Doubly linked list: A linked list which has both head and tail parts, thus allowing the traversal in bi-directional fashion. Except the first node, the head node refers to the previous node. Circular linked list: A linked list whose last node has reference to the first node. BITS Pilani, Pilani Campus
Variations of the Linked List The Doubly-Linked List The Circular Linked List BITS Pilani, Pilani Campus
Linear Lists Operations on Linear Lists • List - • Insert -- • Delete -- • Traverse -- • Empty ---- BITS Pilani, Pilani Campus
Linear Lists (Insert) BITS Pilani, Pilani Campus
General linear list ADT We define a general linear list as an ADT as shown below: BITS Pilani, Pilani Campus
General linear list implementation • A general list ADT can be implemented using either an array or a linked list. BITS Pilani, Pilani Campus
Stacks • A stack is a restricted linear list in which all additions and deletions are made at one end, the top. (LIFO) BITS Pilani, Pilani Campus
Operations on stacks • • Stack --Push --Pop ---Empty--- BITS Pilani, Pilani Campus
Operations on stacks BITS Pilani, Pilani Campus
Stack ADT BITS Pilani, Pilani Campus
Stack implementation • Stack ADTs can be implemented using either an array or a linked list. BITS Pilani, Pilani Campus
Queues • A queue is a linear list in which data can only be inserted at one end, called the rear, and deleted from the other end, called the front. (FIFO). BITS Pilani, Pilani Campus
Operations on queues Queue • Enqueue • Dequeue • Empty • BITS Pilani, Pilani Campus
Operations on queues BITS Pilani, Pilani Campus
Queue ADT BITS Pilani, Pilani Campus
Queue implementation • A queue ADT can be implemented using either an array or a linked list BITS Pilani, Pilani Campus
- Slides: 60