Presentation is loading. Please wait.

Presentation is loading. Please wait.

Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.

Similar presentations


Presentation on theme: "Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques."— Presentation transcript:

1 Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques

2 2 Contents Introduction to searching techniques Types of Searching Introduction to sorting techniques Types of Sorting Introducing the concept of analysis of algorithms

3 3 Objectives At the end of this section, you should be able to: Different Types of search methods –Linear search –Binary search Different Types of Sort Methods –Selection sort –Bubble sort Analysis of algorithms

4 4 Search Concept Searching is Locating a particular data item in a list of data elements is known as searching. Search takes a problem as input and returns a solution to the problem It is the process of locating data like a word, a file etc There are different Types of search- Few of them are –Linear Search –Binary Search

5 5 Linear Search One of the Famous Searching Techniques Linear search also as sequential search In Linear search each item is verified one at a time, in sequence, until a matching result is found. It also means check item starting at the beginning of the data and checking each item in turn until either the desired item is found or the end of the data is reached.

6 6 Linear Search Algorithm 1.Initialize the index to start of array. 2.Repeat through step 4 n times (n - number of elements in the array) 3.Compare search element with current index element. 4.If equal return the index of current element. 5.Return -1 (indicates the element was not found)

7 7 Binary Search Binary Search - Searches a sorted array by repeatedly dividing the search interval in half. Binary search finds a particular value in a sorted array, by "ruling out" half of the data at each step. Important point to be noted Before Using Binary Search –Requires the input array to be sorted. –Should not be used when array involves lots of insertions and deletions. It is faster than Linear search – since it follows divide and conquer rule

8 8 Binary Search Algorithm 1.Initialize Low to the index of the first element. 2.Initialize High to the index of the last element. 3.Repeat through step 6 while Low <= High 4.Middle=(Low+High)/2 5.If search element is equal to middle element return Index of middle element 6. If search element is less than middle element High=Middle-1 else Low=Middle+1 7.return -1 (indicates element not found)

9 9 Sort Concept Sorting is the process of arranging data items in a particular order (Ascending or descending). Sorting is a feature in which the elements of a list are put in certain order There are different Types of search- Few of them are –Selection sort –Bubble sort

10 10 Selection Sort In Selection sort it selects the smallest unsorted item remaining in the list, and then swapping it with the item in the next position to be filled. It Beginning with the first element in the array, a search is performed to locate the element which has the smallest key. When the element is found it is interchanged with the first element. This process is continued for all the elements in the array. It is mainly used with small list of elements

11 11 Selection Sort Algorithm 1. Repeat thru steps 5 a total of n-1 times. 2. Record the number portion of the vector already used. 3. Repeat step 4 for the elements in the unsorted portion of the vector. 4. Record location of smallest elements in unsorted vector 5. Exchange first element in unsorted vector with smallest element.

12 12 Example - Selection sort Original Array Pass Number 123456 4211 23 74 36 1142 58 94 74 36 74 94

13 13 Bubble Sort In bubble sort instead of finding the smallest element and then exchanging, it compares and exchanges with the adjacent element. It starts by comparing the first item to the second, the second to the third and so on until it finds one item out of order. It then swaps the two items and starts over. Is used for Sequencing small list.

14 14 Bubble Sort Algorithm 1. Repeat through step 4 a total of n-1 times. 2. Repeat step 3 for elements in unsorted portion of the vector. 3. If the current element in the vector > next element in the vector then exchange the elements. 4. If no exchanges were made then return else reduce the size of the unsorted vector by one.

15 15 Example - Bubble sort Original Array Pass Number 12345 746558422311 655842231123 5842231142 231158 231165 1174

16 16 Space Complexity –Indicates how much extra memory the algorithm needs Time Complexity –Indicates how fast the algorithm runs Analysis of algorithms

17 17 Key Points Searching is Locating a particular data item in a list of data elements is known as searching. Few of the Searching types are Linear search & Binary search Sorting is the process of arranging data items in a particular order (Ascending or descending). Few of the types of Sorting Techniques are Selection sort & Bubble sort Analysis of algorithms

18 18 Questions and Comments


Download ppt "Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques."

Similar presentations


Ads by Google