search problem 2 search for a value in a sorted list and
play

Search Problem 2: search for a value in a sorted list and return - PDF document

CISC4080 Review of sorting and searching algorithms, recursive algorithms Spring 2019 1. Algorithm and pseudocode A. Problem: input/output B. instance (a particular input) C. Pseudocode: language-neutral, specific data type-neural (sort an


  1. CISC4080 Review of sorting and searching algorithms, recursive algorithms Spring 2019 1. Algorithm and pseudocode A. Problem: input/output B. instance (a particular input) C. Pseudocode: language-neutral, specific data type-neural (sort an array of int, double…), an array/vector of ints, … A few terms: - a list: a[1…n] refers to a data structure (ADT) that stores a collection of elements (of some type), in which accessing a[i] (i-th element) takes constant amount of time (i.e., accessing a[1], a[2], …a[1000] takes same amount of time) - can be a C++ array, C++ STL vector - in Pseudocode, index to list usually starts from 1. (careful when translating into C or java, or python). - in general a sublist a[i…j] where i>=1, j<=n, is a contiguous part of a list a[1…n] - e.g., a[1…8] is a sublist of a[1…9] - a[1…1] is a sublist of a[1…9] of length 1 - a[3…2] is a null list (length is 0) - for i=1 to n // repeat do something for i=1, i=2, … i=n - do something - repeat until some_condition //as long as some_condition is false, do something - do something 2. Searching Problem sorting a list (arrange list elements in ascending or descending order) | ^ | helps | helps (in insertion sort) v | searching a list for a value: linear search and binary search Search Problem 1: searching for a value in a list input: a[1…n], value output: index/location in a if value is found; -1 if not found LinearSearch1 (a[1…n], v) // for Search Problem 1 for i = 1 to n if a[i]==v return i //indent body of loop return -1

  2. Search Problem 2: search for a value in a sorted list and return would-be location of not found input: a[1…n], value output: index/location where value is found; negation of would-be location of value if it’s not found linearSearch2 (a[1…n], v) // alg for search problem 2 for i=1 to n if a[i]==v return i if a[i] > v return -i return -(n+1) // v is larger than all list elements ——————————————————- binarySearch (a[1…n], v) // alg for search problem 2 compare v with elements in the middle of a[1…n] if v is larger than middle element of a[1…n], perform binary search on second half … 1) to make it recursive, the function needs to take the left and right index as parameter, to specify the sublist to search 2) complete the recursive function 3. Three sorting algorithms bubble sort: * based upon the process of bubbling (up the largest number to the right by comparing adjacent elements and swap them if the former is larger than the latter) * Repeat the same bubbling process for the sublist, a[1…n-1]; and then a[1… n-2],.. * until the sublist is of length 1, i.e., a[1…1] bubble_sort (a[1…n]) end_index = n repeat until end_index==1 or there is no swap in last bubbling // bubbling up a[1…end_index] for i=1 to end_index-1 if (a[i] > a[i+1]) swap a[i], a[i+1] // now: a[end_index] is greater than a[1], a[2], … a[end_index-1] end_index = end_index -1

  3. bubble_sort (a[1…n]) implemted as recursive function if (n==1) return for i=1 to n-1 if (a[i] > a[i+1]) swap (a[i], a[i+1]) // now a[n] is the largest value among a[1…n], no need to touch it bubble_sort (a[1…n-1]) Check: Do you know how to trace the execution of a recursive function? a[1…6] = {10, 4, 5, 7, 3, 9} 2) How to convince/check the correctness of recursive function? three questions: A. Are the base cases solved correctly? B. Does the general case reduce the size of the problem and eventually to a base case? C. Does the general cases use the smaller case solution to solve the larger one correctly? Math. induction Insertion Sort Idea: * Gradually build up a sorted sublist. Initially a[1…1] is sorted, insert a[2] into the sorted sublist a[1…1] to get sorted sublist a[1…2] insert a[3] into a[1…2] => a[1…3] is sorted … * Key operation: insert a value into a sorted sublist by shifting all values that are larger then v to the right (down) or use binary search to find where to insert the new value Insertion_Sort (a[1…n]) // a[1…1] is sorted for i=2 to n // insert a[i] into the sorted sublist a[1…i-1] value = a[i] // use j to scan a[1…i-1] from right to left, until find first element that’s smaller // than or equal to value, shift all larger values to the right

  4. for j=i-1 to 1 if value < a[j] a[j+1] = a[j] else break //either j==0 or a[j] <= value (and a[j+1]>value) a[j+1] = value //add testing if j+1!=i to avoid this copying // a[1…i] is now sorted Selection Sort: fewer swapping of the data (than bubble sort and insertion sort) Find out the location of the largest element of list a[1…n], swap it with a[n] Find the index of the largest element of list a[1…n-1], swap it with a[n-1] … Find the index of the largest element of sublist a[1…2], and swap it with a[2] ——————————————————- SelectionSort (a[1…n]) for endIndex=n to 2 //find maxIndex (index of largest element in sublist a[1…endIndex]) maxIndex = 1 //maxIndex is the largest so far, i.e., in a[1…1] for i=2 to endIndex if a[i]>a[maxIndex] maxIndex = i //a[maxIndex] is largest among a[1…i] //swap a[maxIndex] with a[endIndex] if endIndex!=maxIndex swap a[endIndex], a[maxIndex] ——————————————————- //Recursive implementation: pass the left and right index of the list to be sorted SelectionSort_recursive (a[], left, right ) if (left==right) return //find maxIndex (index of largest element in sublist a[left…right]) maxIndex = left //maxIndex is the largest so far, i.e., in a[left…left]

  5. for i=left+1 to right if a[i]>a[maxIndex] maxIndex = i //a[maxIndex] is largest among a[left…right] //swap a[maxIndex] with a[right] if right!=maxIndex swap a[right], a[maxIndex] SelectionSort_recursive (a[left…right-1] ——————————————————- 4. Implementation in lab1, as function template (a template that can be used to generate automatically functions that has the same logic, but work on different data type). You don’t write a function to sort an array of ints, and then a different one to sort an array of doubles, or an array of chars…. Link to code example: http://storm.cis.fordham.edu/zhang/cs4080/Demo/bubblesort.cpp

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend