ITEC2620 Introduction to Data Structures Searching and Sorting It is faster to search a sorted array What happens if our data set changes? We have to keep the array in sorted order Lecture 3b Linked-Lists Inserting into Sorted Arrays I Inserting into Sorted Arrays II 2 9 14 16 1 18 21 23-2 9 14 16 1 18 21 23 - What happens if we want to insert 8? What happens if we want to insert 8? Shift all larger elements back one 2 8 9 14 16 1 18 21 23 1
Inserting into Sorted Arrays III What is the algorithm for this? Start from the back of the array loop larger values get moved back one Inserting into Sorted Arrays IV What is the complexity of this algorithm? Is there a best, worst, and average case? What type of loop was used? Does the insert always happen in the same location? No best, worst, and average cases Inserting into Sorted Arrays V How many times? What does it cost? Best 0 moves O(1) Worst n * moves/swaps O(n) Avg n/2 * moves/swaps O(n) Inserting into Full Arrays I 2 8 9 14 16 1 18 21 23 What happens if we want to insert another value into this array? 2
Inserting into Full Arrays II Option 1 Copy entire array into a larger array Option 2 Keep an array with extra space Time-space trade-off What saves time can often waste space What saves space can often waste time Dynamic Data Structures I An array creates an entire block of data at once int data = new int[n]; int data = new int[n+1]; Whole new block of data created not just one more element Dynamic Data Structures II Would like to create extra memory slots one at a time How can we allocate memory in Java? new Make each data element an instance of a class Dynamic Data Structures III public class IntLink public int key; public IntLink next; } key next 3
Creating Links I public IntLink (int key, IntLink next) this.key = key; this.next = next; } key IntLink Creating Links II IntLink list = new IntLink (, new IntLink (, new IntLink (1, null) ) ); list 1 next Inserting Links I public void insertafter (IntLink target) target.next = next; next = target; } Inserting Links II list.insertafter(new IntLink(x, null) ); list 1 x x list 1 4
Arrays vs. Linked Lists Arrays fixed length contiguous memory locations direct access Linked-Lists dynamic length arbitrary memory locations access by following links list Lists I 2 8 9 14 16 1 18 21 23 1 Find and delete a value in a sorted array and a linked-list Lists II 2 8 9 14 16 1 18 21 23 Arrays Find value with binary search (assume value is in array) Shuffle all higher values down a slot Lists III public static void delete (int[] ar, int target) int slot = binarysearch(target, ar); for (int i = slot; i < ar.length-1; i++) ar[i] = ar[i+1]; }
Lists IV What is the complexity for find? Best middle element O(1) Worst end element O(logn) Avg in between Lists V What is the complexity for update? Best last element O(1) Worst first element O(n) Avg middle element O(n) Lists VI What is the overall complexity? Best last element O(logn) + O(1) O(logn) Worst first element O(logn) + O(n) O(n) Avg mid-area element O(logn) + O(n) O(n) head Lists VII Linked-Lists Find value by following links Link past target link (garbage collecting will remove it) 1 6
head Lists VIII What happens if target is first link? Have to change our reference to the start of the list 1 Lists IX public static IntLink deletelink (IntLink head, int searchkey) // Target is first link if (head.key == searchkey) return head.next; Lists X // Initialization IntLink prev = head; IntLink todelete = head.next; Lists XI // Traverse while (todelete!= null && todelete.key!= searchkey) prev = todelete; todelete = todelete.next; }
Lists XII Lists XIII // Update list if (todelete!= null) prev.next = todelete.next; 1 return head; } list = deletelink (list, ); Lists XIV Lists XV list list 1 1 prev todelete prev todelete // Initialization // Traverse 8
Lists XVI Lists XVII list 1 What is the complexity for find? Best first element O(1) Worst last element O(n) Avg middle element O(n) prev todelete // Update Lists XVIII What is the complexity for update? Always the same O(1) Lists XIX What is the overall complexity? Best first element O(1) + O(1) O(1) Worst last element O(n) + O(1) O(n) Avg middle element O(n) + O(1) O(n) 9
Summary I Linked-List does not allow direct access to an element Binary search on an array exploits direct access Summary II Array has O(n) update which makes overall O(n) Linked-List has O(n) find which makes overall O(n) Need to allow O(logn) search on linked structures Readings and Assignments Suggested Readings from Shaffer (third edition) 4.1-4.1.4