Weitere ähnliche Inhalte
Kürzlich hochgeladen (20)
Week09
- 2. Copyright Warning
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
This material has been copied and communicated to you by or
on behalf of Bond University pursuant to Part VB of the
Copyright Act 1968 (the Act).
The material in this communication may be subject to copyright
under the Act. Any further copying or communication of this
material by you may be the subject of copyright protection
under the Act.
Do not remove this notice.
© 2004 Pearson Addison-Wesley. All rights reserved 4-2
- 3. Sorting
Frequently, we want our collections of data sorted (in ascending
order, alphabetically, etc.)
There are many algorithms for sorting data
Considerations:
• How to compare elements?
– numerically, in ascending or descending order
– alphabetically
– highest card, etc.
• How are elements stored?
– e.g., if an array, need to move elements around until they are
in the right order
• What is the best method?
– need a way to characterise different sorting methods to decide
which is best for the problem at hand
© 2004 Pearson Addison-Wesley. All rights reserved 4-3
- 4. Selection Sort
Selection sort pseudo-code algorithm:
problem: sort the array A into ascending order
1. Find the position of the smallest element of A
2. Swap the smallest with the element at position A[0]
3. Find the position of the smallest element of A from
position 1 to the end of A
4. Swap this smallest element with the element at
position A[1]
5. Find the position of the smallest element of A from
position 2 to the end of A
6. Swap with A[2]
7. Repeat this process until the whole array has been
sorted
© 2004 Pearson Addison-Wesley. All rights reserved 4-4
- 5. Selection Sort Example
(from Phil Stocks’ slides)
original array: A: 11 2 3 7 5
0 1 2 3 4
A: 11 2 3 7 5 A: 2 11 3 7 5
from 1
0 2 3 4 0 1 2 3 4
A: 2 11 3 7 5 A: 2 3 11 7 5
0 1 2 3 4 0 1 2 3 4
A: 2 3 11 7 5 A: 2 3 5 7 11
0 1 2 3 4 0 1 2 3 4
A: 2 3 5 7 11 A: 2 3 5 7 11
0 1 2 3 4 0 1 2 3 4
A: 2 3 5 7 11 A: 2 3 5 7 11
0 1 2 3 4 done 0 1 2 3 4
© 2004 Pearson Addison-Wesley. All rights reserved 4-5
- 6. Selection Sort in Java:
Stepwise Refinement
// Perform a selection sort of the array A
public void selSort ( int [ ] A ) {
// iterate over indices up to second from the end
(A.length – 2)
for ( int i = 0; i < A.length – 1; i++) {
// find position, k, of smallest element of remaining
array:
// i.e., position, k, of smallest in A[i .. A.length – 1]
<need code here>
// swap elements at positions i and k
<need code here>
}
}
© 2004 Pearson Addison-Wesley. All rights reserved 4-6
- 7. Selection Sort in Java:
Stepwise Refinement
// Perform a selection sort of the array A
public void selSort ( int [ ] A ) {
// iterate over indices up to second from the end (A.length – 2)
for ( int i = 0; i < A.length – 1; i++) {
// find position, k, of smallest element of remaining array:
// i.e., position, k, of smallest in A[i .. A.length – 1]
int k;
<need code here>
// swap elements at positions i and k
int temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
© 2004 Pearson Addison-Wesley. All rights reserved 4-7
- 8. Selection Sort in Java:
Stepwise Refinement
// Perform a selection sort of the array A
public void selSort ( int [ ] A ) {
// iterate over indices up to second from the end (A.length – 2)
for ( int i = 0; i < A.length – 1; i++) {
// find position, k, of smallest element of remaining array:
// i.e., position, k, of smallest in A[i .. A.length – 1]
int k = i; Note the initialisation
for (int j = i+1; j < A.length; j++) to i
if ( A[j] < A[k] )
k = j;
// swap elements at positions i and k
int temp = A[i];
A[i] = A[k];
A[k] = temp;
}
© 2004 Pearson Addison-Wesley. All rights reserved 4-8
- 9. Selection Sort Analysis
How good is selection sort? How fast is it?
Most often executed statement:
– statement body of innermost for-loop: if ( A[j] < A[k] ) k = j;
How many executions of this statement for an array of length n?
– first iteration of outer loop: n-1 times
– second iteration of outer loop: n-2 times
– …
– last iteration of outer loop: 1 time
⇒ (n-1) + (n-2) + … + 2 + 1
= n (n-1) / 2
this is proportional to n2
We call selection sort an order n2 algorithm ( written: O(n2) )
© 2004 Pearson Addison-Wesley. All rights reserved 4-9
- 10. How slow is O(n2)?
Example, sort population of England, approximately
50 million
n2 = 5 x 107 x 5 x 107
= 2.5 x 1015
I.e., on the order of 2.5 x 1015 comparisons executed!
(At 100 million comparisons per second (optimistic)
this would take nearly a year to run.)
© 2004 Pearson Addison-Wesley. All rights reserved 4-10
- 11. Searching
Very common to search through collections of
data looking for a particular element
Again, efficiency is a realistic concern
Consider Linear Search from before:How fast is it?
How many
boolean found = false; comparisons?
for (int index=0; index<SIZE && !found; index+ For an array of length n,
+) on the order of n
if (A[index] == X) comparisons
found = true;
Linear search is order
n ( O(n) )
© 2004 Pearson Addison-Wesley. All rights reserved 4-11
- 12. Searching Ordered Data
Common to keep collections in sorted order. We
can use that knowledge to speed up searching
Linear Search II:
boolean found = false;
(of sorted array)
for (int index=0; index<SIZE && !found;
• proceed from front to index++) {
back
if ( A[index] > X)
• if value at position
equals X then found break;
• if value of position is
greater than X, X if (A[index] == X)
cannot be in the array, found = true;
so break }
Linear Search II is still order n
© 2004 Pearson Addison-Wesley. All rights reserved 4-12
- 13. Twenty Questions
I’m thinking of a number between 1 and 1,000,000
Can you guess it in 20 questions?
“Is it smaller than 500,000?” “yes”
“Is it smaller than 250,000?” “no”
“Is it smaller than 375,000?” “no”
etc.
Each question halves the remaining viable numbers
How many questions to search 1,000,000? I.e., how many times can
1,000,000 be divided by 2?
– 220 = 1, 048, 576
– log2(1,048,576) = 20
– log2(1,000,000) < 20 therefore, 20 questions is enough!
© 2004 Pearson Addison-Wesley. All rights reserved 4-13
- 14. Twenty Questions
Find the value X in a sorted array A in the range A[0] to A[size]:
• Start with the middle element. If X > A[middle], search the top half. If X <
A[middle], search the bottom half. In either case, now searching a subarray
half the original size.
• The search continues by checking the middle element of the subarray and
further halving the search space.
• End when find a location P in 0..Size such that A[P-1] < X <= A[P]. If the
current position is at one end then X < A[0] or X > A[Size]
A
Find 27: -5 2 5 9 13 14 27 31 44 51
-5 2 5 9 13 14 27 31 44 51
-5 2 5 9 13 14 27 31 44 51
© 2004 Pearson Addison-Wesley. All rights reserved 4-14
- 15. Binary Search Procedure
int binSearch ( int [ ] A, int x) // return index of x in A
{
int left = 0; right = A.length-1; mid;
if (A.length == 0 || x < A[0]) return –1;
if ( x > A[right] ) return A.length;
while (left < right) {
// assertion: A[left]< x < A[right]
mid = (left + right) / 2 ;
if ( x == A[mid] )
return (x);
if (x < A[mid] )
right = mid - 1;
else
left = mid + 1;
}
if (x == A[left]) return left;
else return –1;
© 2004 Pearson Addison-Wesley. All rights reserved 4-15
}