1 / 12

CHAPTER 2 : SORTING & SEARCHING

CHAPTER 2 : SORTING & SEARCHING. Overview. SORTING Bubble Sort SEARCHING Linear Search Binary Search. SORTING : Bubble Sort.

Download Presentation

CHAPTER 2 : SORTING & SEARCHING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 2 : SORTING & SEARCHING

  2. Overview SORTING • Bubble Sort SEARCHING • Linear Search • Binary Search

  3. SORTING :Bubble Sort • Just like a bubble comes up from water, in bubble sort smallest or largest number, depending upon sorting array on ascending or descending order, bubbles up towards start or end of the array. • Therefore need at least N pass to sort the array completely and at the end of each pass one elements are sorted in its proper position. • Take first element from array, and start comparing it with other element, swapping where it's lesser than the number you are comparing. • Start this comparison from start or from end. CSC248 DATA STRUCTURE

  4. SORTING : Bubble Sort CSC258 DATA STRUCTURE

  5. SORTING : Bubble Sort • In this array, start from index 0, which is 5 and starts comparing elements from start to end. So first element we compare 5 is 1, and since 5 is greater than 1 we swap them ( because ascending order sorted array will have larger number towards end). • Next compare 5 to 6, here  no swapping because 6 is greater than 5 and it's on higher index than 5 • Now compare 6 to 2, again we need swapping to move 6 towards end. • At the end of this pass 6 reaches (bubbles up) at the top of the array. • In next iteration 5 will be sorted on its position and after n iteration all elements will be sorted. Since compare each element with another, need two for loops and that result in complexity of O(n^2). CSC258 DATA STRUCTURE

  6. SEARCHING • Searching is an operation or a technique that helps finds the place of a given element or value in the list. • Any search is said to be successful or unsuccessful depending upon whether the element that is being searched is found or not. • Some of the standard searching technique that is being followed in the data structure is listed below: • 1. Linear Search or Sequential Search • 2. Binary Search CSC258 DATA STRUCTURE

  7. SEARCHING : Linear Search • This is the simplest method for searching. • In this technique of searching, the element to be found in searching the elements to be found is searched sequentially in the list. • This method can be performed on a sorted or an unsorted list (usually arrays). • In case of a sorted list searching starts from 0th element and continues until the element is found from the list or the element whose value is greater than (assuming the list is sorted in ascending order), the value being searched is reached. • As against this, searching in case of unsorted list also begins from the 0th element and continues until the element or the end of the list is reached. CSC258 DATA STRUCTURE

  8. SEARCHING : Linear Search • A linear search scans one item at a time, without jumping to any item . • The worst case complexity is  O(n), sometimes known an O(n) search • Time taken to search elements keep increasing as the number of elements are increased. • Linear Search to find the element “J” in a given sorted list from A-X CSC258 DATA STRUCTURE

  9. SEARCHING : Binary Search • Binary search is a very fast and efficient searching technique. It requires the list to be in sorted order. • In this method, to search an element you can compare it with the present element at the center of the list. • If it matches, then the search is successful otherwise the list is divided into two halves: one from the 0th element to the middle element which is the center element (first half) another from the center element to the last element (which is the 2nd half) where all values are greater than the center element. • The searching mechanism proceeds from either of the two halves depending upon whether the target element is greater or smaller than the central element. • If the element is smaller than the central element, then searching is done in the first half, otherwise searching is done in the second half. CSC258 DATA STRUCTURE

  10. SEARCHING : Binary Search • A binary search however, cut down your search to half as soon as you find middle of a sorted list. • The middle element is looked to check if it is greater than or less than the value to be searched. • Accordingly, search is done to either half of the given list • Binary Search to find the element “J” in a given sorted list from A-X CSC258 DATA STRUCTURE

  11. Linear Search vs Binary Search • Input data needs to be sorted in Binary Search and not in Linear Search • Linear search does the sequential access whereas Binary search access data randomly. • Time complexity of linear search -O(n) , Binary search has time complexity O(log n). •  Linear search performs equality comparisons and Binary search performs ordering comparisons CSC258 DATA STRUCTURE

More Related