java中的数组搜寻和排序

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

high
low = mid+1 = 5
2012-4-20
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Sequence of Unsuccessful Search - 2
low #1 #2 0 5 high 8 8 mid 4 6
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Polymorphism in Searching
• Recall that an class that implements the Comparable interface defines a compareTo method to determine the relative order of its objects • We can use polymorphism to develop a generic search for any set of Comparable objects • The searching method accepts as a parameter an array of Comparable objects • That way, one method can be used to search target from a group of People, or Books, or whatever
Chapter 9 Searching & Sorting
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Searching
• Searching is the process of finding a target element within a group of items called the search pool
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Sequence of Successful Search - 1
low #1 0 high 8 mid 4
low + high mid = 2
2012-4-20
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Binary Search
• Another Polymorphism Implementation of Binary Search • See PhoneList2.java • See Searching.java, specifically the binarySearch method
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Linear Search
• A linear search begins at one end of a list and examines each element in turn • Eventually, either the item is found or the end of the list is encountered • See PhoneList2.java • See Searching.java, specifically the linearSearch method
search( 45 )
low + high mid = 2
0 5
1 12
2
3
4 38
5 44
low
6 77
7 84
8 90
high
17 23
mid 45 < 77
high = mid-1=5
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2012-4-20
Sequence of Unsuccessful Search - 1
low #1 0 high 8 mid 4
low + high mid = 2
search( 45 )
0 5
1 12
2
3
4 38
5 44
6 77
7 84
8 90
17 23
low
mid 38 < 45
search( 45 )
low + high mid = 2
5 44
high
6 77
low
7 84
8 90
17 23
Unsuccessful Search
no more elements to search
low > high
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Sequence of Successful Search - 2
low #1 #2 0 5 high 8 8 mid 4 6
search( 44 )
low + high mid = 2
0 5
1 12
2
3
4 38
5 44
6 77
7 84
8 90
17 23
low
mid 44 < 77
2012-4-20
Sequence of Unsuccessful Search - 3
low #1 #2 #3 0 5 5 high 8 8 5 mid 4 6 5 3 4 38 5 44
low high mid
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
10 50 100 500 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
10 50 100 500 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
4 6 7 9 10 11 12 12 13 13 13 13 14 14
• Unsuccessful Search
– Best Case =Worst Case – log2N comparisons
• 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.
search( 45 )
low + high mid = 2
0 5
1 12
2
6 77
7 84
8 90
17 23
44 < 45
low = mid+1 = 6
2012-4-20
Sequence of Unsuccessful Search - 4
low #1 #2 #3 #4 0 5 5 6 0 5 1 12 2 high 8 8 5 5 3 4 38 mid 4 6 5
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2012-4-20
Binary Search Performance
• Successful Search
– Best Case – 1 comparison – Worst Case – log2N comparisons
2012-4-20
Binary Search Routine
public int binarySearch ( int[] number, int searchValue ) { int low = 0, high = number.length - 1, mid = (low + high) / 2; while ( low <= high && number[mid] != searchValue ) { if (number[mid] < searchValue) { low = mid + 1; } else { //number[mid] > searchValue high = mid - 1; } mid = (low + high) / 2; //integer division will truncate } if ( low > high) { mid = NOT_FOUND; } return mid; }
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2012-4-20
Comparing N and log2N Performance
Array Size Linear – N Binary – log2N
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Binary Search
• A binary search assumes the list of items in the search pool is sorted • It eliminates a large part of the search pool with a single comparison • A binary search first examines the middle element of the list -- if it matches the target, the search is over • If it doesn't, only one half of the remaining elements need be searched • Since they are sorted, the target can only be in one half of the other number 0 1 2 3 4 5 6 7 8 5 12 17 23 38 44 77 84 90
high
high = mid-1=5
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2012-4-20
Sequence of Successful Search - 3
low #1 #2 #3 0 5 5 high 8 8 5 mid 4 6 5
search( 44 )
0 5
low
1 12
2
Fra Baidu bibliotek
3
4 38
mid 38 < 44
5 44
6 77
7 84
8 90
high
17 23
low = mid+1 = 5
2012-4-20
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
• The target may or may not be in the search pool
• We want to perform the search efficiently, minimizing the number of comparisons
• Let's look at two classic searching approaches: linear search and binary search
search( 44 )
low + high mid = 2
0 5
1 12
2
3
4 38
5 44
low high mid
6 77
7 84
8 90
17 23
Successful Search!!
44 == 44
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
相关文档
最新文档