Download presentation
Presentation is loading. Please wait.
Published byBlanche Gilmore Modified over 9 years ago
1
Sorting and Searching
2
Searching Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1). (Assumptions: no duplicate entries in the array)
3
Search Example: Number 40201817222730 X= 40 0 1 2 3 4 5 6 Return: 3
4
Search Example: Number 40201817222730 X= 32 0 1 2 3 4 5 6 Return: NOT_FOUND (-1)
5
Searching We will count the number of comparisons the algorithms make to analyze their performance. The ideal searching algorithm will make the least possible number of comparisons to locate the desired data. Two separate performance analyses are normally done: one for successful search and another for unsuccessful search.
6
Searching We will count the number of comparisons the algorithms make to analyze their performance. The ideal searching algorithm will make the least possible number of comparisons to locate the desired data. Two separate performance analyses are normally done: one for successful search (x is found) and another for unsuccessful search (x is not found)
7
Linear Search Search the array from the first to the last position in linear progression. public int linearSearch ( int[] number, int searchValue ) { } int pos = 0; while (pos < number.length && number[pos] != searchValue) { pos++; } if (pos == number.length) { //Not found return NOT_FOUND; } else { return pos; //Found, return the position }
8
Linear Search Performance We analyze the successful and unsuccessful searches separately. Successful Search Best Case: 1 comparison Worst Case: N comparisons (N – array size) Unsuccessful Search Best Case = Worst Case: N comparisons
9
Lab 1. Download the partially implemented Search.java class from the class web siteclass web site 2. Find the comment “// Please insert the code to ask a user to type in the size of the array” and insert the code as required accordingly there. 3. Find the comment “// Please allocate the memory for the array number here” and insert the code as required there. 4. Find the comment “// Please insert the code to ask a user to type in the actual value for each element in the array” and insert the code as required there
10
Lab 5. Find the comment ”// Please insert the code to ask the user to type in the number being searched” and insert the code required there 6. Find the comment “Please insert the code is to call linearSearch method using searchObj object” and insert the code there 7. Compile and run your program
11
int size; int[] number; String inputStr; // Please insert the code to ask a user to type in the size of the array inputStr = JOptionPane.showInputDialog(null,"Please enter the size of array: "); size = Integer.parseInt (inputStr); // Please allocate the memory for the array number here number = new int[size]; // Please insert the code to ask a user to type in the actual // value for each element in the array for (int i=0; i< size; i++) { inputStr = JOptionPane.showInputDialog(null,"Please enter the number["+i+"]"); number[i] = Integer.parseInt (inputStr); }
12
int searchNumber; int index = -1; // Please insert the code to ask the user to type in // the number being searched inputStr = JOptionPane.showInputDialog(null,"Please enter the number being searched: "); searchNumber = Integer.parseInt (inputStr); Search searchObj = new Search(); // Please insert the code is to call linearSearch // method using searchObj object index = searchObj.linearSearch(number,searchNumber); System.out.println(" Index of the number being searched is "+ index); System.exit(0);
13
Second midterm exam Date: Monday, 04/17/2006 Format: Multiple choices Matching Identify syntactic errors Identify results of code Content: refer to guidelines
14
Binary Search If the array is sorted, then we can apply the binary search technique. Sorted array: all the values in the array are arranged in ascending or descending order a[i] >= a[j] (i>= j) OR a[i] = j)
15
Example Unsorted array Sorted array Number 40201817222730 0 1 2 3 4 5 6 Number 22181720273040 0 1 2 3 4 5 6
16
Binary Search The basic idea is straightforward: First search the value in the middle position. If X is less than this value, then search the middle of the left half next. If X is greater than this value, then search the middle of the right half next. Continue in this manner.
17
Sequence of Successful Search - 1 512172338 4477 012345678 8490 search( 44 ) lowhighmid 0808 #1 high low 38 < 44 low = mid+1 = 5 mid 4
18
Sequence of Successful Search - 2 512172338 4477 012345678 8490 search( 44 ) lowhighmid 0808 #1 44 < 77 high = mid-1=5 4 mid 6 highlow 5858 #2
19
Sequence of Successful Search - 3 512172338 4477 012345678 8490 search( 44 ) lowhighmid 0808 #1 44 == 44 4 5858 #2 6 highlow5 #3 mid 5 Successful Search!!
20
Sequence of Unsuccessful Search - 1 512172338 4477 012345678 8490 search( 45 ) lowhighmid 0808 #1 high low 38 < 45 low = mid+1 = 5 mid 4
21
Sequence of Unsuccessful Search - 2 512172338 4477 012345678 8490 search( 45 ) lowhighmid 0808 #1 45 < 77 high = mid-1=5 4 mid 6 highlow 5858 #2
22
Sequence of Unsuccessful Search - 3 512172338 4477 012345678 8490 search( 45 ) lowhighmid 0808 #1 44 < 45 4 5858 #2 6 highlow5 #3 mid 5 low = mid+1 = 6
23
Sequence of Unsuccessful Search - 4 512172338 4477 012345678 8490 search( 45 ) lowhighmid 0808 #1 4 5858 #2 65 #3 5 highlow 6565 #4 Unsuccessful Search low > high no more elements to search
24
Binary Search Routine public int binarySearch (int[] number, int searchValue) { int low = 0; int high= number.length – 1; int mid= (low + high) / 2; while (low <= high && number[mid] != searchValue) { if (number[mid] < searchValue) { low = mid + 1; } else { high = mid - 1; } mid = (low + high) / 2; } if (low > high) { mid = NOT_FOUND; } return mid; }
25
Binary Search Performance Successful Search Best Case: 1 comparison Worst Case: log 2 N comparisons Unsuccessful Search Best Case = Worst Case: log 2 N comparisons
26
Binary Search Performance Since the portion of an array to search is cut into half after every comparison, we compute how many times the array can be divided into halves. After K comparisons, there will be N/2 K elements in the list. We solve for K when N/2 K = 1, deriving K = log 2 N.
27
Comparing N and log 2 N Performance Array SizeLinear – NBinary – log 2 N
28
“Just in time” review 1. Suppose an array (SORTED) contains 1024 elements. What are the least and the greatest number of comparisons for a successful search using binary search? 1 and 10 1 and 9 1 and 8 1 and 1024
29
“Just in time review” 2. How many comparisons do we need to find x=6 using linear search on the following sorted array A (10 elements): -8 -6 -4 -2 0 2 4 6 8 10 a. 6 b. 7 c. 8 d. 9
30
“Just in time review” 3. How many comparisons do we need to find x=6 using binary search on the following sorted array A (10 elements): -8 -6 -4 -2 0 2 4 6 8 10 a. 1 b. 2 c. 3 d. 4
31
“Just in time review” 4. Which of the following is NOT true? a. Linear search requires no special constraint on the array b. Binary search requires the array to be sorted c. Binary search and linear search have the same best case performance (successful search) d. Binary search and linear search have the same worst case performance (successful search)
32
Example X = 40? Number 22181720273040 0 1 2 3 4 5 6 Middle position = (6+0)/2 = 3 Comparing: 40 and number[3](22) => Not match =?
33
Example X = 40? Number 22181720273040 0 1 2 3 4 5 6 Middle position = (6+4)/2 = 5 Comparing: 40 and number[5](30) => Not match =?
34
Example X = 40? Number 22181720273040 0 1 2 3 4 5 6 Middle position = (6+6)/2 = 6 Comparing: 40 and number[6](40) => match Return 6
35
Lab 6 Math.random method: Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
36
Lab 6
38
Array 1: we did not sort it and we use linear search Array 2: sort it and use binary search
39
Sorting
40
The problem statement: Given an array of N integer values, arrange the values into ascending order. We will count the number of comparisons the algorithms make to analyze their performance. The ideal sorting algorithm will make the least possible number of comparisons to arrange data in a designated order. We will compare different sorting algorithms by analyzing their worst-case performance.
41
Example Input: Output Number 40201817222730 0 1 2 3 4 5 6 Number 22181720273040 0 1 2 3 4 5 6
42
Selection Sort Step1 : Find the smallest integer in the array Step 2: Exchange the element in the first position and the smallest element. Now the smallest element is in the first position. Cross out the number found in step 1 from consideration. Step 3: Repeat steps 1 and 2 until all numbers in the array are sorted
43
Example Input (unsorted): Input number 40201817222730 0 1 2 3 4 5 6 Number 0 1 2 3 4 5 6
44
Example First pass Number 40201817222730 0 1 2 3 4 5 6 Number 17 0 1 2 3 4 5 6 182040222730
45
Example Second pass Number 17 0 1 2 3 4 5 6 182040222730 Number 17 0 1 2 3 4 5 6 201840222730
46
Example Third pass Number 17 0 1 2 3 4 5 6 201840222730
47
Example Fourth pass Number 17 0 1 2 3 4 5 6 201840222730 Number 17 0 1 2 3 4 5 6 201822402730
48
Example Fifth pass Number 17 0 1 2 3 4 5 6 201822402730 Number 17 0 1 2 3 4 5 6 201822274030
49
Example Sixth pass Number 17 0 1 2 3 4 5 6 201822274030 Number 17 0 1 2 3 4 5 6 201822273040
50
Example Result Number 17 0 1 2 3 4 5 6 201822273040
51
Code public void selectionSort(int[] number) { int minIndex, size, temp; size = number.length;
52
Code for (int i=0; i<size-2; i++) { minIndex = i; for (int j=i+1; j<size-1; j++) { if (number[j] < number[minIndex]) minIndex=j; } temp= number[i]; number[i]=number[minIndex]; number[minIndex] = temp; }
53
Complexity analysis Given an array with n elements, the total number of comparisons is approximately the square of the size of an array (N 2 )
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.