sorting and searching. searching problem definition: given a value x, return the index of x in the...
DESCRIPTION
Search Example: Number X= Return: 3TRANSCRIPT
Sorting and Searching
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)
Search
Example:
Number402018 17 22 27 30
X= 40
0 1 2 3 4 5 6
Return: 3
Search
Example:
Number402018 17 22 27 30
X= 32
0 1 2 3 4 5 6
Return: NOT_FOUND (-1)
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.
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)
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 foundreturn NOT_FOUND;
} else { return pos; //Found, return the position}
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
Lab
1. Download the partially implemented Search.java class from the class 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
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
int size;int[] number;String inputStr;
// Please insert the code to ask a user to type in the size of the arrayinputStr = JOptionPane.showInputDialog(null,"Please enter the size of
array: ");size = Integer.parseInt (inputStr);
// Please allocate the memory for the array number herenumber = new int[size];
// Please insert the code to ask a user to type in the actual // value for each element in the arrayfor (int i=0; i< size; i++) {
inputStr = JOptionPane.showInputDialog(null,"Please enter the number["+i+"]");number[i] = Integer.parseInt (inputStr);
}
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);
Second midterm exam
Date: Monday, 04/17/2006Format:
Multiple choicesMatchingIdentify syntactic errorsIdentify results of code
Content: refer to guidelines
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 ordera[i] >= a[j] (i>= j)
ORa[i] <= a[j] (i>= j)
Example
Unsorted array
Sorted array
Number402018 17 22 27 30
0 1 2 3 4 5 6
Number221817 20 27 30 40
0 1 2 3 4 5 6
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.
Sequence of Successful Search - 1
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 44 )low high mid0 8#1
highlow
2
highlowmid
38 < 44 low = mid+1 = 5
mid
4
Sequence of Successful Search - 2
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 44 )low high mid0 8#1
2
highlowmid
44 < 77high = mid-1=5
4
mid
6
highlow
5 8#2
Sequence of Successful Search - 3
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 44 )low high mid0 8#1
2
highlowmid
44 == 44
45 8#2 6
highlow
5 5#3
mid
5
Successful Search!!
Sequence of Unsuccessful Search - 1
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 45 )low high mid0 8#1
highlow
2
highlowmid
38 < 45 low = mid+1 = 5
mid
4
Sequence of Unsuccessful Search - 2
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 45 )low high mid0 8#1
2
highlowmid
45 < 77high = mid-1=5
4
mid
6
highlow
5 8#2
Sequence of Unsuccessful Search - 3
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 45 )low high mid0 8#1
2
highlowmid
44 < 45
45 8#2 6
highlow
5 5#3
mid
5
low = mid+1 = 6
Sequence of Unsuccessful Search - 4
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 45 )low high mid0 8#1
2
highlowmid
45 8#2 65 5#3 5
high low
6 5#4
Unsuccessful Searchlow > highno more elements to search
Binary Search Routinepublic 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;
}
Binary Search Performance
Successful Search Best Case: 1 comparison Worst Case: log2N comparisons
Unsuccessful Search Best Case =
Worst Case: log2N comparisons
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/2K elements in the list. We solve for K when N/2K = 1, deriving K = log2N.
Comparing N and log2N Performance
Array Size Linear – N Binary – log2N10 10 4
50 50 6100 100 7500 500 9
1000 1000 102000 2000 113000 3000 124000 4000 125000 5000 136000 6000 137000 7000 138000 8000 139000 9000 14
10000 10000 14
“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
“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 10a. 6b. 7c. 8d. 9
“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 10a. 1b. 2c. 3d. 4
“Just in time review”
4. Which of the following is NOT true?a. Linear search requires no special
constraint on the arrayb. Binary search requires the array to be
sortedc. 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)
Example
X = 40?
Number221817 20 27 30 40
0 1 2 3 4 5 6
Middle position = (6+0)/2 = 3Comparing: 40 and number[3](22) => Not match
=?
Example
X = 40?
Number221817 20 27 30 40
0 1 2 3 4 5 6
Middle position = (6+4)/2 = 5Comparing: 40 and number[5](30) => Not match
=?
Example
X = 40?
Number221817 20 27 30 40
0 1 2 3 4 5 6
Middle position = (6+6)/2 = 6Comparing: 40 and number[6](40) => matchReturn 6
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.
Lab 6
Array 1 Array 2 Search Key Found/Not
Found? Numbers of Comparisons made?
Search Key Found/Not Found
Numbers of Comparisons made?
66 Found 8 66 Found 6 59 Found 17 59 Found 4 56 FOUND 82 56 FOUND 6 19 FOUND 100 19 FOUND 6 212 FOUND 133 212 FOUND 5 5 NOT
FOUND 148 5 NOT
FOUND 7
17 FOUND 75 17 FOUND 7 36 FOUND 34 36 FOUND 6 89 NOT
FOUND 148 89 NOT
FOUND 8
184 FOUND 14 184 FOUND 6
Lab 6
0
20
40
60
80
100
120
140
160
0 5 10 15
Array 1
Array 2
Lab 6
Array 1: we did not sort it and we use linear search
Array 2: sort it and use binary search
Sorting
Sorting
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.
Example
Input:
Output
Number402018 17 22 27 30
0 1 2 3 4 5 6
Number221817 20 27 30 40
0 1 2 3 4 5 6
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
Example
Input (unsorted):
Input
number402018 17 22 27 30
0 1 2 3 4 5 6
Number
0 1 2 3 4 5 6
Example
First passNumber
402018 17 22 27 30
0 1 2 3 4 5 6
Number17
0 1 2 3 4 5 6
1820 40 22 27 30
Example
Second passNumber
17
0 1 2 3 4 5 6
1820 40 22 27 30
Number17
0 1 2 3 4 5 6
2018 40 22 27 30
Example
Third pass
Number17
0 1 2 3 4 5 6
2018 40 22 27 30
Example
Fourth pass
Number17
0 1 2 3 4 5 6
2018 40 22 27 30
Number17
0 1 2 3 4 5 6
2018 22 40 27 30
Example
Fifth passNumber
17
0 1 2 3 4 5 6
2018 22 40 27 30
Number17
0 1 2 3 4 5 6
2018 22 27 40 30
Example
Sixth passNumber
17
0 1 2 3 4 5 6
2018 22 27 40 30
Number17
0 1 2 3 4 5 6
2018 22 27 30 40
Example
Result
Number17
0 1 2 3 4 5 6
2018 22 27 30 40
Code
public void selectionSort(int[] number) {int minIndex, size, temp;size = number.length;
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;
}}
Complexity analysis
Given an array with n elements, the total number of comparisons is approximately the square of the size of an array (N2)