17-Searching-Sorting.pptx

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

• 29

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

CMPSC 201 Lecture 17 Searching & Sorting Arrays Based on Book 8.3 (some slides adapted from Jenn Plasterr)

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

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
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

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

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
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

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

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
Modify Code ? If we want to find the only first instance of the key, we could stop the search when found = true .

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

This is the end of the preview. Sign up to access the rest of the document.
• Spring '08
• SUSANQUICK

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern