Download presentation
Presentation is loading. Please wait.
1
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin
2
Computing is Fun! Work at google… Don’t want to walk to your friend’s office… No Problem!
3
Computing is Fun! Want to sleep late? Want to sleep DURING work? No problem!
4
Computing is Fun! Need to work out? No problem!
5
Stressed? Take a break!
6
Computing is Fun! Hungry? Free food!
7
Sorting and Searching: Overview What we will cover today: Algorithms and the importance of designing effective ones Searching Different methods and concerns: Linear Search Binary Search Sorting Different algorithms and their implementations: Selection Sort Bubble Sort
8
Algorithms “A step-by-step problem-solving procedure” -- Answers.com How we solve a problem Demo: How would you design an algorithm for going to the store?
9
Going to the Store What do we do first? Walk me through the steps of going to the store with a grocery list and buying everything on it. MORAL: Algorithms are necessary to solve a problem, although they are more useful for more difficult problems.
10
Efficiency Searching can take a long time Imagine having to search through all 6,000,000 people on facebook individually we want a faster method Computer Scientists have ways of determining the time that different algorithms take Big O notation n = size of arbitrarily large input We don’t expect you to know/understand the exact number of steps an algorithm takes You should be able to intuitively tell if an algorithm is efficient/why or why not
11
Searching Searching is looking for what you want Examples Google Other search engines Practical applications Facebook Image Processing Natural Language Processing
12
Linear Search Algorithm 1. Traverse through the array/list 2. Look at each element 3. Is this the element you need? 4. Continue until you find the element or you have gotten through the array/list
13
Linear Search Running Time What is the minimum number of steps this could take? When does this happen? 1 The item you are looking for is in the first slot n The item is not in the array/list or it is in the last slot What is the maximum number of steps this could take? When does this happen?
14
Linear Search Code int best = 0; for(int index = 1; index < array.length; index++) { if(array[index] is better than array[best]) { best = index; } GIVEN: an array and a value you are looking for
15
Linear Search Problems Linear Search not always effective When we go to the store we don’t need to look at every product to find what we are looking for
16
Binary Search Lets say I wanted to find a name in a phonebook… Demo: Binary Searching through a phonebook Tell me each step to find a name We are developing an __________
17
Binary Search 1716253037424652596369707478 Is 65 in the array? Start with an ordered array Look at the middle element Is this > or < what we are searching for? 01234567891011121314 0123456(7)891011121314 1716253037424652596369707478
18
Start at previous middle, find new middle location Look at the middle element Is this > or < what we are searching for? Binary Search 1716253037424652596369707478 012345678910(11)121314 012345678910(11)121314 1716253037424652596369707478
19
Start at previous middle, find new middle location Look at the middle element Is this > or < what we are searching for? Binary Search 1716253037424652596369707478 012345678(9)1011121314 012345678(9)1011121314 1716253037424652596369707478
20
Start at previous middle, find new middle location Look at the middle element Is this the last element? Is this what we were looking for? Binary Search 1716253037424652596369707478 0123456789(10)11121314 0123456789(10)11121314 1716253037424652596369707478
21
Binary Search: Summary GIVEN: an array and a value you are looking for 1. Look at the middle value 2. Is this middle value > or < the value you are looking for 3. Adjust your array so that the middle value becomes the highest or lowest value in the array 4. Repeat steps 1-3 until you find your value or until the array has size = 1
22
Binary Search: Code int low = 0; //original lowest value to start the search from int high = array.length - 1; //original highest value to start the search from int middle; while(low <=high) { mid = (low/2) + (high/2); //find middle value of the given array or subarray(as //specified by location) if(array[middle] > value) //if middle value is higher than what you are looking for low = mid - 1; //set the highest value for the subarray to be one less else if(array[middle[] < value) //if middle value is lower than what you are looking for high = mid + 1; //set the lowest value for the subarray to be one more else return mid; //only returns a number if it is neither > or < value } return -1; //not found GIVEN: an array and a value you are looking for
23
Binary Search Efficiency What is the best possible case? What is the worst possible case? Find it on the first try The element you are looking for is not in the array, so you have to do every step and it is not there The element you are looking for is in the smallest possible subarray.
24
Sorting Arrange the elements of a list or an array in a specified order Some algorithms need to be sorted in order to work Binary Search A dictionary
25
Ordering a list of integers from smallest to largest 1. Find the smallest integer in list 2. Swap it with the integer in position 0 Selection Sort Algorithm 73985 73985 0 1 2 3 4 37985
26
Selection Sort Algorithm 3. Find the smallest integer in the list starting with position 1 4. Swap the smallest integer with the item in position 1 37985 0 1 2 3 4 35987
27
Selection Sort Algorithm 5. Find the smallest integer in the list starting with position 2 6. Swap it with the item in position 2 35987 0 1 2 3 4 35789
28
Selection Sort Algorithm 7. Find the smallest integer in the list starting with position 2 8. Swap it with the item in position 3 35789 0 1 2 3 4 35789
29
Selection Sort Algorithm 9. Your list is sorted! 35789 0 1 2 3 4 Now…how do we code this?
30
Demo: Coding Selection Sort Pseudocode int first = 0; //the first element to look at in the given step int smallest; //the smallest element in the sub array int last = length - 1; //the last element to look at in the given step while(first < last) { //control how long you evaluate the array smallest = smallest element from first to last; //find smallest element swap first with smallest; //swap the first element with the smallest first = first + 1; //do the above again, but starting in the next position } Can also be done with a for loop GIVEN: an array
31
Demo: Coding Selection Sort int first = 0; int last = array.length - 1; int smallest; int temp; int next =0; while(first < last) { smallest = first; int i = 0; next = first+1; while(next <= last) { //linear search to find the smallest element if(arraySort[smallest] > arraySort[next] ) { smallest = next; } next++; } temp = array[first]; //swap the elements array[first] = array[smallest]; array[smallest] = temp; first ++; //increment first } GIVEN: an array
32
Bubble Sort Algorithm Ordering an array from smallest to largest Start with an unordered array/list 62935 01234
33
Bubble Sort Algorithm 1. Look at the first two items, are they in order? 2. If not, swap them 3. Move up one element 63935 (0)(1)234 36925 (0)(1)234 36925 0 (2)34
34
Bubble Sort Algorithm 4. Are these two items in order? 5. If yes, do nothing 6. Move up one element 36925 0(1)(2)34 36925 0(1)(2)34 36925 01 (3)4
35
Bubble Sort Algorithm 7. Are these two items in order? 36925 01(2)(3)4 8. Swap these items 36295 01(2)(3)4 9. Move up one element 36295 012(3)(4)
36
Bubble Sort Algorithm 10. Are these items in order? 36295 012(3)(4) 11. Swap items 36259 012(3)(4)
37
Bubble Sort Algorithm 36259 012(3)(4) Now what? Is the array ordered? Are any objects in the right spot? We moved the largest element to the end of the array/list. By constantly moving the largest element while looking at pairs of elements, we moved the largest element Continue process to move second largest, etc.
38
Bubble Sort Algorithm Second Pass 1. Look at the first two items, are they in order? 2. If not, swap them 3. Move up one element 36259 (0)(1)234 36259 0 (2)34 36259 01234
39
Bubble Sort Algorithm 4. Are these two items in order? 36259 0(1)(2)34 32659 0(1)(2)34 32659 01 (3)4 6. Move up one element 5. Swap them
40
Bubble Sort Algorithm 7. Are these two items in order? 32659 01(2)(3)4 8. Swap these items 32569 01(2)(3)4 Don’t need to move on to the last slot, because we have already ensured that the biggest element is there
41
Bubble Sort Algorithm Third Pass 1. Look at the first two items, are they in order? 23569 (0)(1)234 23569 0 (2)34 32569 01234 2. If not, swap them 3. Move up one element and repeat(if necessary)
42
2. If not, swap them (not necessary here, but could be in other cases) Bubble Sort Algorithm Fourth Pass 1. Look at the first two items, are they in order? 23569 (0)(1)234 23569 01234
43
Bubble Sort Algorithm Summary 1. Look at pairs of elements 2. Order the pair 3. Move to the next pair and order 4. Continue until at the end of array/list 5. Repeat steps 1-4, each time going one step less 6. Continue until there is only one pair to order
44
Bubble Sort Code Pseudocode int last; //the last element to position, for the given pass int next ; //the index of the pairs you are checking last = array.length; //first time through you start with the last position for(int pass = 1; pass < last; pass++) { //keep checking as long as there are two spots for(int next = 1; next < last - pass; next++){ //this loop gets shorter through each iteration if(array[next] better than array[next+ 1]) { //check to see if ordered swap array[next] with array[next+ 1]; //if not, swap them } Can also be done with while loops GIVEN: an array
45
Bubble Sort Code int last = array.length; //the last element to position, used to determine how far //to go in a given pass int next; //the index of the pairs you are checking int temp; for(int pass = 1; pass < last; pass++) { //keep checking as long as there are two spots for(int next = 1; next < last - pass; next++){ //this loop gets shorter through each iteration if(array[next] > array[next+ 1]) { //check to see if ordered temp = array[next]; //if not, swap them array[next] = array[next + 1]; array[next + 1] = temp; } Can also be done with while loops GIVEN: an array
46
Demo: Insertion Sort I have a row of different kinds of candy that need to be alphabetized Divide the candy into two groups with the first candy in one group and every other candy in another(make sure they are in a line) Take the first item in the unsorted group(the larger group) out of that group and insert it into the proper position in the sorted group The sorted group should now be one larger than it was Repeat this process until all of the candy is alphabetized NOTICE: How do we add the cookie to the full row of cookies? What happens to the unsorted array each time we add a cookie to the sorted array? What happens to the sorted array each time we add a cookie to it?
47
Demo: Insertion Sort Graphical Representation of Insertion Sort http://maven.smith.edu/~thiebaut/java/sort/demo.h tml http://maven.smith.edu/~thiebaut/java/sort/demo.h tml
48
Lab You will be assigned to groups of three In your group, write a class to sort a given array using the algorithm we just used to sort the cookies.
49
Homework Due by April 23rd @ 1:30 pm 1. Edit the sortAndSearch class 2. EXTRA CREDIT: Which sorting algorithm that we used today do you think is most efficient? Why?
50
Quiz April 23rd @ 1:30pm You will be expected to understand HOW sorting and searching code works Understand the need for efficiency concerns and what makes an algorithm efficient or not
51
Sources Wikipedia Barron’s Guide to AP Computer Science An Introduction to Programming and Object- Oriented Design by Nino/Hosch CIS 110(Fall 2007) lecture slides CIS 120(Spring 2008) lecture slides http://www.leepoint.net/notes- java/data/arrays/32arraybubblesort.html http://www.leepoint.net/notes- java/data/arrays/31arrayselectionsort.html http://web.engr.oregonstate.edu/~minoura/cs162/jav aProgs/sort/InsertSort.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.