Data Structures and Algorithms Lecture (10) PDF
Document Details
Uploaded by StylishSpessartine
University of Science and Technology
Noureldien A. Noureldien
Tags
Related
- Data Structures and Algorithms Lecture Notes PDF
- 22317 Computer Science Past Paper PDF
- Data Structures and Algorithms MCQ's PDF
- A Practical Introduction to Data Structures and Algorithms (Java) PDF
- Data Structures And Algorithms Past Paper PDF BT-3/D-22 PC-CS-201A
- Data Structures and Algorithms Lecture (1) - University of Science and Technology - PDF
Summary
This lecture covers searching algorithms, focusing on linear and binary search. It explains the concepts, algorithms, and time complexity analysis of these methods, providing illustrative examples. The content is oriented toward computer science students at the undergraduate level.
Full Transcript
**University of Science and Technology** **Faculty of Computer Science and Information Technology** **Data Structures and Algorithms** **Lecture (10): Searching** **Prof. Noureldien A. Noureldien** **10.1 Introduction** Searching for items and sorting through items are tasks that we do every d...
**University of Science and Technology** **Faculty of Computer Science and Information Technology** **Data Structures and Algorithms** **Lecture (10): Searching** **Prof. Noureldien A. Noureldien** **10.1 Introduction** Searching for items and sorting through items are tasks that we do every day. We search for all occurrences of a word in a file in order to replace it with another word. We sort the items on a list into alphabetical or numerical order. Searching and sorting are also most common operations in computer programs. **10.2 Searching** Searching refers to the operation of finding the location of the desired key element within some data structures. Data structures can include linked lists, arrays, search trees or hash tables. The search is successful if the item is found, and then returns its location; otherwise, the search is unsuccessful. The appropriate search algorithms often depends on the data structure being searched. There are many searching algorithms, the most common two are: Linear Search Binary Search **10.3 Linear Search** Linear search, or sequential search is a method for finding the position of a particular key value in a list, where the search starts from beginning of the list and checking every element, one at a time in sequence until the desired key is found or the end of the list is reached. **10.3.1 Algorithm of Linear Search** Linear search is the simplest searching technique. Suppose A is an array with N elements from A \[0\] to A\[N - 1\]. The linear search algorithm for finding the KEY item performs as below. At first, the algorithm compares KEY with the first element A\[0\] of the list. If KEY = A\[0\] then the search is successful and return the location. Otherwise, compares KEY with the second element A\[1\] , third element A\[2\], so on until the KEY is found or the end of the list is reached. Finally, after successfully searching return the location of the KEY by reference, otherwise in the case of unsuccessful search return -1. **10.3.2 Time complexity of Linear Search** The linear search runs at a worst time as it makes at most and comparisons, where n is the length of the list. In the average case, the number of comparisons calculated by the average of all possible instances: ![](media/image2.png) The best case occurs when the key is found at first position, so only one comparison is required and the worst case occurs when the key is found at the last position, hence n+1 comparisons are required. **Programming Example : Implementing Linear Search** This is a C Program to implement Linear Search Algorithm to find the minimum element in an array. 1\. We first have to create an array of numbers by taking input from user. We have to input an array of numbers and then apply the linear search algorithm to find the minimum value element in an array.\ 2. In order to find out the minimum element from the array of numbers we assume the first element of an array to be the minimum.\ 3. After that we traverse the whole array from the beginning and if we encounter any element whose value is less than the value of the first element we'll put that element as minimum element.\ 4. This process will continue till we have compared all the elements with each other. As a last step we return the smallest element from the array. 1. */\* C program to find the minimum element in an array using Linear Search \*\** 3. \* \* C program accept an array of N elements and a key to search.* 4. \* \* If the search is successful, it displays \"SUCCESSFUL SEARCH\".* 5. \* \* Otherwise, a message \"UNSUCCESSFUL SEARCH\" is displayed.* 6. \* \*\* search begins \*/* 28. 29. low = 0; 30. high = (size - 1); 31. 32. while (low \