COP3502_3_SortedListMatch

COP3502_3_SortedListMatch - Sorted List Matching Problem...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science Department University of Central Florida Sorted List Matching Problem COP 3502 – Computer Science I
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sorted List Matching Problem page 2 Sorted List Matching Problem Sorted List Matching Problem You are given two lists of Last Names Within each list, all names are distinct Also, each list is already sorted Problem: Output the names common to both lists
Background image of page 2
Sorted List Matching Problem page 3 Sorted List Matching Problem Sorted List Matching Problem Perhaps a standard way to attack this problem: For each name on list #1, do the following: a) Search for the current name in list #2 b) If the name is found, output it. If the list is unsorted, steps a and b above may take n steps, where n is the size of the second list. Who can tell us why? Steps a and b are run for each of the n names in List #1, resulting in an n 2 running time.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sorted List Matching Problem page 4 Sorted List Matching Problem Sorted List Matching Problem If we don’t take advantage of the fact that the lists are sorted, we can do a brute force algorithm as follows: void printMatches(char list1[][SIZE], char list2[][SIZE], int len1, int len2) { int i,j; for (i=0; i<len1; i++) { for (j=0; j<len2; j++) { if (strcmp(list1[i],list2[j]) == 0) { printf(“%s\n”, list1[i]); break; } } } }
Background image of page 4
Sorted List Matching Problem page 5 Sorted List Matching Problem Sorted List Matching Problem The previous solution did NOT use the fact that the lists are already sorted. We can exploit this fact by using a Binary Search in step (a) So what is a Binary Search…
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sorted List Matching Problem page 6 Sorted List Matching Problem Binary Search a binary search is an algorithm for locating the position of an
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/21/2011 for the course COP 3330 taught by Professor Staff during the Spring '08 term at University of Central Florida.

Page1 / 18

COP3502_3_SortedListMatch - Sorted List Matching Problem...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online