17-Searching-Sorting.pptx - CMPSC 201 Lecture 17 Searching...

Info icon This preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPSC 201 Lecture 17 Searching & Sorting Arrays Based on Book 8.3 (some slides adapted from Jenn Plasterr)
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Objectives Be able to search for a value in an array using linear search Be able to search for a value in an array using binary search Be able to sort arrays using different methods Selection sort Bubble sort Insertion sort Martin K.-C. Yeh 2
Image of page 2
Searching Arrays There are times you may want to look at the elements of an array to determine if a specific value or key is present in the array . The search is often written as a function so it could be called at various times in the program, using various arrays. Two primary methods: Linear (sequential) search Binary search Martin K.-C. Yeh 3
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Algorithm for a Linear Search Linear Search is also sometimes called a Sequential Search. Suppose you want to find an exam of a specific score in a stack of exams that have not been sorted. How would you do it? What if you wanted to find all the entries that match your value? Martin K.-C. Yeh 4
Image of page 4
Write the Code Let’s begin by using a loop to examine all the elements of the array, first to last, to find the index of the array element(s) that has/have the value (key) we are looking for. for( int j = 0; j < size; j++) { if (data[j] == key) { cout << key << “ found at ” << j; } } Martin K.-C. Yeh 5
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Modify Code We should output a message that states that the key was not found if our search was unsuccessful. bool found = false; for( int j = 0; j < size; j++) { if (data[j] == key) { found = true; cout<< key <<“ found at ” <<j; } } if (!found) { cout<< key<< “ was not found in the array” << endl; } Martin K.-C. Yeh 6
Image of page 6
Modify Code ? If we want to find the only first instance of the key, we could stop the search when found = true .
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern