Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching Sequential Search Binary Search. Sequential Search Sequential search is to look into the key one after another until the target is found If.

Similar presentations


Presentation on theme: "Searching Sequential Search Binary Search. Sequential Search Sequential search is to look into the key one after another until the target is found If."— Presentation transcript:

1 Searching Sequential Search Binary Search

2 Sequential Search Sequential search is to look into the key one after another until the target is found If the list of data does not contain the target, the search performs until the last item has been reached. Although, sequential search, by its nature, is easy to program, it is the slowest technique Sequential search takes at most n comparisons and n/2 comparisons on average.

3 Search By Guessing: Binary Search The key idea for Binary Search is that elements in the list need to be stored before the search begins The target is compared with the middle element If lucky, the target is located in the middle –Therefore, the search finished with successful result If not, result indicates which half of the list contains target (The other half will be discarded.) Process goes until the target is found or the number of potential elements becomes zero.

4 Binary Search vs Sequential Search A binary search of a file with n records takes at most: And on average approximately: Sequential search takes at most n comparisons and n/2 comparisons on average. With 1,000 records, it takes at most: –10 comparisons for Binary Search –1,000 comparisons for Sequential Search. Note: lg is logarithm function to the base 2.

5 Binary Search vs Sequential Search Doubling number of records will cost: –One more comparison for Binary Search –But double number of comparisons for Sequential search Binary search is said to be O(lg n) While sequential search is O(n). on average and could be 2,000 comparisons.

6 Finding Things Sequential search implies looking at every record Binary search approximately takes What if there is no record containing the requested file? What if we think there might be more than one record containing the key? However: This lecture assume searching for primary. (no duplication involved)

7 2104 22152941 33 5248568188947098 Finding a Record by Its Key 021 4357 6 98101213141115 22884 81155641 94 981023329487052 021 4357 6 98101213141115 List sorted by its key (ascending) Unsorted List Sequential Search Binary Search

8 Psuedo: Sequential Search int sequential_search(Data[], n, Target) { For i = 0 to n-1 if(Target == Data[i]) return i //Found Target Next i return -1; //Target is not found }

9 Psuedo: Binary Search int binary_search(Data[], n, Target) { start=0; end=n-1; while(TRUE){ mid = start + ((end-start)/2); if(mid==start) { if (Data[mid]==Target) return mid; //FOUND if (Data[end]==Target) return end; //FOUND return -1; //Target Not Found } if (Data[mid] == Target) return mid; //FOUND else if(Data[mid] < Target) start = mid+1; //Discard Left else end = mid - 1; //Discard Right }

10 Programming Time Given a record of Suppose, we have 20 students, given Can you perform both searches by student’s name? typedef struct { char name[50]; int score; } STD_SCORE; STD_SCORE std_score[20];

11 String Compare Functions Try these two functions for string comparision The upper compares two strings, ignoring case The lower compare first n characters, ignoring case int strcasecmp(const char *s1, const char *s2); int strncasecmp(const char *s1, const char *s2, size_t n);


Download ppt "Searching Sequential Search Binary Search. Sequential Search Sequential search is to look into the key one after another until the target is found If."

Similar presentations


Ads by Google