Download Sorting and Searching

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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:
X= 40
Number
Return: 3
18
20
17
40
22
27
0
1
2
3
4
5
30
6
Search

Example:
X= 32
Number
18
20
17
40
22
27
0
1
2
3
4
5
Return: NOT_FOUND (-1)
30
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 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 found
return 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.
2.
3.
4.
Download the partially implemented Search.java
class from the class web site
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.
Find the comment “// Please allocate the
memory for the array number here” and insert
the code as required there.
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 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);
}
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/2006
Format:
Multiple choices
Matching
Identify syntactic errors
Identify 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
order
a[i] >= a[j] (i>= j)
OR
a[i] <= a[j] (i>= j)

Example

Unsorted array
Number

18
20
17
40
22
27
0
1
2
3
4
5
30
6
Sorted array
Number
17
18
20
22
27
30
0
1
2
3
4
5
40
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
#1
low
high
mid
0
8
4
search( 44 )
 low  high 
mid  

2

0
1
2
3
4
5
6
7
8
5
12
17 23
38
44
77
84
90
low
mid
38 < 44
high
low = mid+1 = 5
Sequence of Successful Search 2
low
high
mid
search( 44 )
#1
#2
0
5
4
6
8
8
 low  high 
mid  

2

0
1
2
3
4
5
6
7
8
5
12
17 23
38
44
77
84
90
low
high = mid-1=5
mid
44 < 77
high
Sequence of Successful Search 3
low
high
mid
search( 44 )
#1
#2
#3
0
5
5
4
6
5
8
8
5
 low  high 
mid  

2

0
1
2
3
4
5
6
7
8
5
12
17 23
38
44
77
84
90
Successful Search!!
44 == 44
low high
mid
Sequence of Unsuccessful Search
-1
low
high
mid
search( 45 )
#1
0
4
8
 low  high 
mid  

2

0
1
2
3
4
5
6
7
8
5
12
17 23
38
44
77
84
90
low
mid
38 < 45
high
low = mid+1 = 5
Sequence of Unsuccessful Search
-2
low
high
mid
search( 45 )
#1
#2
0
5
4
6
8
8
 low  high 
mid  

2

0
1
2
3
4
5
6
7
8
5
12
17 23
38
44
77
84
90
low
high = mid-1=5
mid
45 < 77
high
Sequence of Unsuccessful Search
-3
low
high
mid
search( 45 )
#1
#2
#3
0
5
5
4
6
5
8
8
5
 low  high 
mid  

2

0
1
2
3
4
5
6
7
8
5
12
17 23
38
44
77
84
90
low high
mid
44 < 45
low = mid+1 = 6
Sequence of Unsuccessful Search
-4
low
high
mid
search( 45 )
#1
#2
#3
#4
0
5
5
6
4
6
5
8
8
5
5
 low  high 
mid  

2

0
1
2
3
4
5
6
7
8
5
12
17 23
38
44
77
84
90
high
low
Unsuccessful Search
no more elements to search
low > high
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;
}
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
10
50
100
500
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
Linear – N
10
50
100
500
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
Binary – log2N
4
6
7
9
10
11
12
12
13
13
13
13
14
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
1
1
1
and
and
and
and
10
9
8
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 10
a. 6
b. 7
c. 8
d. 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 10
a. 1
b. 2
c. 3
d. 4
“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)
Example
X = 40?
=?
Number
17
18
20
22
27
30
0
1
2
3
4
5
40
6
Middle position = (6+0)/2 = 3
Comparing: 40 and number[3](22) => Not match
Example
X = 40?
=?
Number
17
18
20
22
27
30
0
1
2
3
4
5
40
6
Middle position = (6+4)/2 = 5
Comparing: 40 and number[5](30) => Not match
Example
X = 40?
Number
17
18
20
22
27
30
0
1
2
3
4
5
40
6
Middle position = (6+6)/2 = 6
Comparing: 40 and number[6](40) => match
Return 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
Search Key
66
59
56
19
212
5
17
36
89
184
Array 1
Found/Not
Found?
Found
Found
FOUND
FOUND
FOUND
NOT
FOUND
FOUND
FOUND
NOT
FOUND
FOUND
Numbers of
Comparisons
made?
8
17
82
100
133
148
Search Key
75
34
148
17
36
89
14
184
66
59
56
19
212
5
Array 2
Found/Not
Found
Found
Found
FOUND
FOUND
FOUND
NOT
FOUND
FOUND
FOUND
NOT
FOUND
FOUND
Numbers of
Comparisons
made?
6
4
6
6
5
7
7
6
8
6
Lab 6
160
140
120
100
Array 1
80
Array 2
60
40
20
0
0
5
10
15
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:
Number

Output
18
20
17
40
22
27
0
1
2
3
4
5
17
18
20
22
27
30
0
1
2
3
4
5
30
6
Number
40
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):
number
18
20
17
40
22
27
0
1
2
3
4
5
30
6
Input
Number
0
1
2
3
4
5
6
Example
First pass
Number
18
20
17
40
22
27
30
0
1
2
3
4
5
6
17
20
18
40
22
27
30
0
1
2
3
4
5
6
Number
Example
Second pass
Number
17
20
18
40
22
27
30
0
1
2
3
4
5
6
17
18
20
40
22
27
30
0
1
2
3
4
5
6
Number
Example
Third pass
Number
17
18
20
40
22
27
30
0
1
2
3
4
5
6
Example
Fourth pass
Number
17
18
20
40
22
27
30
0
1
2
3
4
5
6
17
18
20
22
40
27
30
0
1
2
3
4
5
6
Number
Example
Fifth pass
Number
17
18
20
22
40
27
30
0
1
2
3
4
5
6
17
18
20
22
27
40
30
0
1
2
3
4
5
6
Number
Example
Sixth pass
Number
17
18
20
22
27
40
30
0
1
2
3
4
5
6
17
18
20
22
27
30
40
0
1
2
3
4
5
6
Number
Example
Result
Number
17
18
20
22
27
30
40
0
1
2
3
4
5
6
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)
Related documents