Chapter 14 – Sorting and Searching
Copyright © 2014 by John Wiley & Sons. All rights reserved.
1
Chapter Goals
 To study several sorting and searching algorithms
 To appreciate that algorithms for the same task can differ
widely in performance
 To understand the big-Oh notation
 To estimate and compare the performance of algorithms
 To write code to measure the running time of a program
Copyright © 2014 by John Wiley & Sons. All rights reserved.
2
Sorting
 Sorting places data in ascending or descending order,
based on one or more sort keys
 E.g.
• A list of names could be sorted alphabetically,
• bank accounts could be sorted by account number,
• employee payroll records could be sorted by social security
number
 Popular sorting algorithms:
•
•
•
•
•
Selection sort
Insertion sort
Merge sort
Bubble sort
Quick sort
Copyright © 2014 by John Wiley & Sons. All rights reserved.
3
 The end result (sorted array) is the same no matter
which algorithm you use to sort the array.
 The choice of algorithm affects only the run time and
memory use of the program.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
4
Selection Sort
 Simple sorting algorithm
 First iteration:
• selects the smallest element in the array and swaps it with the first
element.
 Second iteration:
•
selects the second-smallest item and swaps it with the second element.
 After the ith iteration:
• the smallest i items of the array will be sorted into increasing order in the
first i elements of the array.
 Selection sort sorts an array by repeatedly finding the smallest
element of the unsorted tail region and moving it to the front.
 Slow when run on large data sets.
 Example: sorting an array of integers
Copyright © 2014 by John Wiley & Sons. All rights reserved.
5
Sorting an Array of Integers
1. Find the smallest and swap it with the first element
2. Find the next smallest. It is already in the correct place
3. Find the next smallest and swap it with first element of
unsorted portion
4. Repeat
5. When the unsorted portion is of length 1, we are done
Copyright © 2014 by John Wiley & Sons. All rights reserved.
6
Programming Question
 Implement class SelectionSorter . Method sort should
accepts an integer array as parameter and perform
selection sort on it.
 Start by writing a pseudocode for selection sort
Copyright © 2014 by John Wiley & Sons. All rights reserved.
7
Answer
 Selection sort pseudocode:
For i = 0 to n-1 do:
iMin = i
For j = i + 1 to n-1 do:
If A(j) < A(iMin )
iMin = j
End-If
End-For
swap(A(i), A(iMin))
End-For
 swap(A(i), A(iMin)):
temp = A(i)
A(i) = A(i+1)
A(i+1) = temp
Copyright © 2014 by John Wiley & Sons. All rights reserved.
8
Answer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
SelectionSorter.java
/**
The sort method of this class sorts an array, using the selection
sort algorithm.
*/
public class SelectionSorter
{
/**
Sorts an array, using selection sort.
@param a the array to sort
*/
public static void sort(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
int minPos = minimumPosition(a, i);
ArrayUtil.swap(a, minPos, i);
}
}
Continued
Copyright © 2014 by John Wiley & Sons. All rights reserved.
9
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
Finds the smallest element in a tail range of the array.
@param a the array to sort
@param from the first position in a to compare
@return the position of the smallest element in the
range a[from] . . . a[a.length - 1]
*/
private static int minimumPosition(int[] a, int from)
{
int minPos = from;
for (int i = from + 1; i < a.length; i++)
{
if (a[i] < a[minPos]) { minPos = i; }
}
return minPos;
}
}
Copyright © 2014 by John Wiley & Sons. All rights reserved.
10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Arrays;
/**
This program demonstrates the selection sort algorithm by
sorting an array that is filled with random numbers.
*/
public class SelectionSortDemo
{
public static void main(String[] args)
{
int[] a = ArrayUtil.randomIntArray(20, 100);
System.out.println(Arrays.toString(a));
SelectionSorter.sort(a);
System.out.println(Arrays.toString(a));
}
}
Typical Program Run:
[65, 46, 14, 52, 38, 2, 96, 39, 14, 33, 13, 4, 24, 99, 89, 77, 73, 87, 36, 81]
[2, 4, 13, 14, 14, 24, 33, 36, 38, 39, 46, 52, 65, 73, 77, 81, 87, 89, 96, 99]
Copyright © 2014 by John Wiley & Sons. All rights reserved.
11
Profiling the Selection Sort Algorithm
 We want to measure the time the algorithm takes to
execute:
• Exclude the time the program takes to load
• Exclude output time
 To measure the running time of a method, get the
current time immediately before and after the method
call.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
12
Profiling the Selection Sort Algorithm
 We will create a StopWatch class to measure execution
time of an algorithm:
• It can start, stop and
• Use System.currentTimeMillis method
•
•
•
•
Create a StopWatch object:
Start the stopwatch just before the sort
Stop the stopwatch just after the sort
Read the elapsed time
Copyright © 2014 by John Wiley & Sons. All rights reserved.
13
section_2/StopWatch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
A stopwatch accumulates time when it is running. You can
repeatedly start and stop the stopwatch. You can use a
stopwatch to measure the running time of a program.
*/
public class StopWatch
{
private long elapsedTime;
private long startTime;
private boolean isRunning;
/**
Constructs a stopwatch that is in the stopped state
and has no time accumulated.
*/
public StopWatch()
{
reset();
}
Continued
Copyright © 2014 by John Wiley & Sons. All rights reserved.
14
section_2/StopWatch.java
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
Starts the stopwatch. Time starts accumulating now.
*/
public void start()
{
if (isRunning) { return; }
isRunning = true;
startTime = System.currentTimeMillis();
}
/**
Stops the stopwatch. Time stops accumulating and is
is added to the elapsed time.
*/
public void stop()
{
if (!isRunning) { return; }
isRunning = false;
long endTime = System.currentTimeMillis();
elapsedTime = elapsedTime + endTime - startTime;
}
Continued
Copyright © 2014 by John Wiley & Sons. All rights reserved.
15
section_2/StopWatch.java
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
Returns the total elapsed time.
@return the total elapsed time
*/
public long getElapsedTime()
{
if (isRunning)
{
long endTime = System.currentTimeMillis();
return elapsedTime + endTime - startTime;
}
else
{
return elapsedTime;
}
}
/**
Stops the watch and resets the elapsed time to 0.
*/
public void reset()
{
elapsedTime = 0;
isRunning = false;
}
}
Copyright © 2014 by John Wiley & Sons. All rights reserved.
16
section_2/SelectionSortTimer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Scanner;
/**
This program measures how long it takes to sort an
array of a user-specified size with the selection
sort algorithm.
*/
public class SelectionSortTimer
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter array size: ");
int n = in.nextInt();
// Construct random array
int[] a = ArrayUtil.randomIntArray(n, 100);
Continued
Copyright © 2014 by John Wiley & Sons. All rights reserved.
17
section_2/SelectionSortTimer.java
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Use stopwatch to time selection sort
StopWatch timer = new StopWatch();
timer.start();
SelectionSorter.sort(a);
timer.stop();
System.out.println("Elapsed time: "
+ timer.getElapsedTime() + " milliseconds");
}
}
Program Run:
Enter array size: 50000
Elapsed time: 13321 milliseconds
Copyright © 2014 by John Wiley & Sons. All rights reserved.
18
Selection Sort on Various Size Arrays
Doubling the size of the array more than doubles the time
needed to sort it.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
19
Question
Approximately how many seconds would it take to sort a
data set of 80,000 values?
Answer: Four times as long as 40,000 values, or about
37 seconds.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
20
Question
Look at the graph in Figure 1. What mathematical shape
does it resemble?
Answer: A parabola.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
21
Analyzing the Performance of the Selection Sort
Algorithm
 In an array of size n, count how many times an array
element is visited:
• To find the smallest, visit n elements + 2 visits for the swap
• To find the next smallest, visit (n - 1) elements + 2 visits for the
swap
• The last term is 2 elements visited to find the smallest + 2 visits
for the swap
For i = 0 to n-1 do:
iMin = i
For j = i + 1 to n-1 do:
If A(j) < A(iMin )
iMin = j
End-If
End-For
swap(A(i), A(iMin))
End-For
Copyright © 2014 by John Wiley & Sons. All rights reserved.
22
Analyzing the Performance of the Selection Sort
Algorithm
 The number of visits:
•
•
•
•
n + 2 + (n - 1) + 2 + (n - 2) + 2 + . . .+ 2 + 2
This can be simplified to n2 /2 + 5n/2 - 3
5n/2 - 3 is small compared to n2 /2 – so let's ignore it
Also ignore the 1/2 – it cancels out when comparing ratios
Copyright © 2014 by John Wiley & Sons. All rights reserved.
23
Analyzing the Performance of the Selection Sort
Algorithm
 The number of visits is of the order n2 .
 Computer scientists use the big-Oh notation to describe
the growth rate of a function.
• Using big-Oh notation: The number of visits is O(n2).
 To convert to big-Oh notation: locate fastest-growing
term, and ignore constant coefficient.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
24
Question
How large does n need to be so that n2/2 is bigger than
5n/2 – 3?
Copyright © 2014 by John Wiley & Sons. All rights reserved.
25
Answer
Plot both
Answer: If n is 4, then n2/2 is 8 and 5n/2 – 3 is 7.
60
n
n^2/2
1
2
3
4
5
6
7
8
9
10
5n/2 -3
0.5
-0.5
2
2
4.5
4.5
8
7
12.5
9.5
18
12
24.5
14.5
32
17
40.5
19.5
50
22
50
40
30
n^2/2
20
5n/2 -3
10
0
1
2
3
4
5
6
7
8
9
10
-10
Copyright © 2014 by John Wiley & Sons. All rights reserved.
26
Question
What is the big-Oh running time of checking whether an
array is already sorted?
Copyright © 2014 by John Wiley & Sons. All rights reserved.
27
Answer
Answer: We need to check that a[0] ≤ a[1], a[1] ≤
a[2], and so on, visiting 2n – 2 elements (n-1 pairs
compared). Therefore, the running time is O(n ).
Copyright © 2014 by John Wiley & Sons. All rights reserved.
28
Common Big-Oh Growth Rates
Copyright © 2014 by John Wiley & Sons. All rights reserved.
29
Question
 Plot the previous functions
n
log(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
0
0.30103
0.47712
0.60206
0.69897
0.77815
0.8451
0.90309
0.95424
1
1.04139
1.07918
1.11394
1.14613
1.17609
1.20412
1.23045
1.25527
1.27875
1.30103
nlog(n) n^2
0
2
4.7549
8
11.61
15.51
19.651
24
28.529
33.219
38.054
43.02
48.106
53.303
58.603
64
69.487
75.059
80.711
86.439
Copyright © 2014 by John Wiley & Sons. All rights reserved.
n^3
1
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
289
324
361
400
2^n
n!
1
2
1
8
4
2
27
8
6
64
16
24
125
32
120
216
64
720
343
128
5040
512
256
40320
729
512 362880
1000
1024 3628800
1331
2048 39916800
1728
4096 4.79E+08
2197
8192 6.23E+09
2744
16384 8.72E+10
3375
32768 1.31E+12
4096
65536 2.09E+13
4913 131072 3.56E+14
5832 262144 6.4E+15
6859 524288 1.22E+17
8000 1048576 2.43E+18
30
n
log(n)
nlog(n)
1
=LOG(E25)
2
n^2
n^3
2^n
n!
=E25*LOG(E25,2) =POWER(E25,2)
=POWER(E25,3)
=POWER(2,E25)
=FACT(E25)
=LOG(E26)
=E26*LOG(E26,2) =POWER(E26,2)
=POWER(E26,3)
=POWER(2,E26)
=FACT(E26)
3
=LOG(E27)
=E27*LOG(E27,2) =POWER(E27,2)
=POWER(E27,3)
=POWER(2,E27)
=FACT(E27)
4
=LOG(E28)
=E28*LOG(E28,2) =POWER(E28,2)
=POWER(E28,3)
=POWER(2,E28)
=FACT(E28)
5
=LOG(E29)
=E29*LOG(E29,2) =POWER(E29,2)
=POWER(E29,3)
=POWER(2,E29)
=FACT(E29)
6
=LOG(E30)
=E30*LOG(E30,2) =POWER(E30,2)
=POWER(E30,3)
=POWER(2,E30)
=FACT(E30)
7
=LOG(E31)
=E31*LOG(E31,2) =POWER(E31,2)
=POWER(E31,3)
=POWER(2,E31)
=FACT(E31)
8
=LOG(E32)
=E32*LOG(E32,2) =POWER(E32,2)
=POWER(E32,3)
=POWER(2,E32)
=FACT(E32)
9
=LOG(E33)
=E33*LOG(E33,2) =POWER(E33,2)
=POWER(E33,3)
=POWER(2,E33)
=FACT(E33)
10
=LOG(E34)
=E34*LOG(E34,2) =POWER(E34,2)
=POWER(E34,3)
=POWER(2,E34)
=FACT(E34)
11
=LOG(E35)
=E35*LOG(E35,2) =POWER(E35,2)
=POWER(E35,3)
=POWER(2,E35)
=FACT(E35)
12
=LOG(E36)
=E36*LOG(E36,2) =POWER(E36,2)
=POWER(E36,3)
=POWER(2,E36)
=FACT(E36)
13
=LOG(E37)
=E37*LOG(E37,2) =POWER(E37,2)
=POWER(E37,3)
=POWER(2,E37)
=FACT(E37)
14
=LOG(E38)
=E38*LOG(E38,2) =POWER(E38,2)
=POWER(E38,3)
=POWER(2,E38)
=FACT(E38)
15
=LOG(E39)
=E39*LOG(E39,2) =POWER(E39,2)
=POWER(E39,3)
=POWER(2,E39)
=FACT(E39)
16
=LOG(E40)
=E40*LOG(E40,2) =POWER(E40,2)
=POWER(E40,3)
=POWER(2,E40)
=FACT(E40)
17
=LOG(E41)
=E41*LOG(E41,2) =POWER(E41,2)
=POWER(E41,3)
=POWER(2,E41)
=FACT(E41)
18
=LOG(E42)
=E42*LOG(E42,2) =POWER(E42,2)
=POWER(E42,3)
=POWER(2,E42)
=FACT(E42)
19
=LOG(E43)
=E43*LOG(E43,2) =POWER(E43,2)
=POWER(E43,3)
=POWER(2,E43)
=FACT(E43)
20
=LOG(E44)
=E44*LOG(E44,2) =POWER(E44,2)
=POWER(E44,3)
=POWER(2,E44)
=FACT(E44)
Copyright © 2014 by John Wiley & Sons. All rights reserved.
31
3E+18
2.5E+18
n
n!
2E+18
log(n)
nlog(n)
1.5E+18
n^2
n^3
1E+18
2^n
5E+17
n!
0
1200000
1
2
3
4
5
6
7
n!8
9
10
11
12
13
14
15
16
17
18
19
20
n
1000000
log(n)
nlog(n)
800000
n^2
n^2
n^3
600000
2^n
400000
n
log(n)
200000
nlog(n)
n^2
0
1
2
3
4
5
6
7
8
Copyright © 2014 by John Wiley & Sons. All rights reserved.
9
10
11
12
13
14
15
16
17
18
19
20
32
9000
8000
7000
n^3
6000
n
5000
log(n)
4000
nlog(n)
3000
n^2
n^3
2000
1000
0
1
2
3
4
5
6
7
8
9
Copyright © 2014 by John Wiley & Sons. All rights reserved.
10
11
12
13
14
15
16
17
18
19
20
33
Insertion Sort
 Like selection sort, maintains a sorted portion(left) and
a unsorted portion (right)in array
 Fist iteration starts with second element in array and
insert it into correct position in sorted portion in array
 The second iteration looks at the third element and
inserts it into the correct position with respect to the
first two, so all three elements are in order.
 and so on….
Copyright © 2014 by John Wiley & Sons. All rights reserved.
34
Insertion Sort
 Assume initial sequence a[0] . . . a[k] is sorted (k
= 0):
 Add a[1]; element needs to be inserted before 11
 Add a[2]
 Add a[3]
 Finally, add a[4]
Copyright © 2014 by John Wiley & Sons. All rights reserved.
35
Programming Question
 Implement InsertionSorter.java. You can write your
sorting code in sort method that accepts an int array
and call it from main.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
36
Answer
import java.util.Arrays;
public class InsertionSorter
{
/**
Sorts an array, using insertion sort.
@param a the array to sort
*/
public static void sort(int[] a)
{
for (int i = 1; i < a.length; i++)
{
int next = a[i];
// Move all larger elements up
int j = i;
while (j > 0 && a[j - 1] > next)
{
a[j] = a[j - 1];
j--;
}
// Insert the element
a[j] = next;
}
}
public static void main(String[] args)
{
int[] a = ArrayUtil.randomIntArray(20, 100);
System.out.println(Arrays.toString(a));
InsertionSorter.sort(a);
System.out.println(Arrays.toString(a));
}
}
Copyright © 2014 by John Wiley & Sons. All rights reserved.
37
Insertion Sort
 Insertion sort is the method that many people use to sort
playing cards. Pick up one card at a time and insert it so
that the cards stay sorted.
 Insertion sort is an O(n2) algorithm.
for (int i = 1; i < a.length; i++)
{
int next = a[i];
// Move all larger elements up
int j = i;
while (j > 0 && a[j - 1] > next)
{
a[j] = a[j - 1];
j--;
}
// Insert the element
a[j] = next;
}
Copyright © 2014 by John Wiley & Sons. All rights reserved.
n visits to a[i]
O(n2) visits to a[j] and a[j-1]
n visits to a[j]
38
Merge Sort
 Sorts an array by
• Cutting the array in half
• Recursively sorting each half
• Merging the sorted halves
 Dramatically faster than the selection sort In merge sort,
one sorts each half, then merges the sorted halves.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
39
Merge Sort Example
 Divide an array in half and sort each half
 Merge the two sorted arrays into a single sorted array
Copyright © 2014 by John Wiley & Sons. All rights reserved.
40
Programming Question
 Implement MergeSorter.java. You can write your
sorting code in sort method that accepts an int array
and call it from main.
public static void sort(int[] a)
 Note that the base case is an array with only one
element and it simply return the same array as sorted
array.
 Steps:
•
•
•
•
Break array into halves/two sub arrays first, second
Sort first half
Sort second half
Merge first and second half
• Implement a method merge for this. Method should accept 3
parameters (first, second, and array into which you want to merge
first and second):
private static void merge(int[] first, int[] second, int[] a)
Copyright © 2014 by John Wiley & Sons. All rights reserved.
41
Merge Sort
public static void sort(int[] a)
{
if (a.length <= 1) { return; }
int[] first = new int[a.length / 2];
int[] second = new int[a.length - first.length];
// Copy the first half of a into first, the second half into second
. . .
sort(first);
sort(second);
merge(first, second, a);
}
Copyright © 2014 by John Wiley & Sons. All rights reserved.
42
section_4/MergeSorter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
The sort method of this class sorts an array, using the merge
sort algorithm.
*/
public class MergeSorter
{
/**
Sorts an array, using merge sort.
@param a the array to sort
*/
public static void sort(int[] a)
{
if (a.length <= 1) { return; }
int[] first = new int[a.length / 2];
int[] second = new int[a.length - first.length];
// Copy the first half of a into first, the second half into second
for (int i = 0; i < first.length; i++)
{
first[i] = a[i];
}
for (int i = 0; i < second.length; i++)
{
second[i] = a[first.length + i];
}
sort(first);
sort(second);
merge(first, second, a);
}
Copyright © 2014 by John Wiley & Sons. All rights reserved.
Continued
43
section_4/MergeSorter.java
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
Merges
@param
@param
@param
two sorted arrays into an array
first the first sorted array
second the second sorted array
a the array into which to merge first and second
*/
private static void merge(int[] first, int[] second, int[] a)
{
int iFirst = 0; // Next element to consider in the first array
int iSecond = 0; // Next element to consider in the second array
int j = 0; // Next open position in a
// As long as neither iFirst nor iSecond is past the end, move
// the smaller element into a
while (iFirst < first.length && iSecond < second.length)
{
if (first[iFirst] < second[iSecond])
{
a[j] = first[iFirst];
iFirst++;
}
else
{
a[j] = second[iSecond];
iSecond++;
}
j++;
}
Copyright © 2014 by John Wiley & Sons. All rights reserved.
Continued
44
section_4/MergeSorter.java
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Note that only one of the two loops below copies entries
// Copy any remaining entries of the first array
while (iFirst < first.length)
{
a[j] = first[iFirst];
iFirst++; j++;
}
// Copy any remaining entries of the second half
while (iSecond < second.length)
{
a[j] = second[iSecond];
iSecond++; j++;
}
}
}
Copyright © 2014 by John Wiley & Sons. All rights reserved.
45
section_4/MergeSortDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Arrays;
/**
This program demonstrates the merge sort algorithm by
sorting an array that is filled with random numbers.
*/
public class MergeSortDemo
{
public static void main(String[] args)
{
int[] a = ArrayUtil.randomIntArray(20, 100);
System.out.println(Arrays.toString(a));
MergeSorter.sort(a);
System.out.println(Arrays.toString(a));
}
}
Typical Program Run:
[8, 81, 48, 53, 46, 70, 98, 42, 27, 76, 33, 24, 2, 76, 62, 89, 90, 5, 13, 21]
[2, 5, 8, 13, 21, 24, 27, 33, 42, 46, 48, 53, 62, 70, 76, 76, 81, 89, 90, 98]
Copyright © 2014 by John Wiley & Sons. All rights reserved.
46
Question
Manually run the merge sort algorithm on the array 8 7 6
5 4 3 2 1.
Answer:
First sort 8 7 6 5.
Recursively, first sort 8 7.
Recursively, first sort 8. It's sorted.
Sort 7. It's sorted.
Merge them: 7 8.
Do the same with 6 5 to get 5 6.
Merge them to 5 6 7 8.
Do the same with 4 3 2 1: Sort 4 3 by sorting 4 and 3 and
merging them to 3 4.
Sort 2 1 by sorting 2 and 1 and merging them to 1 2.
Merge 3 4 and 1 2 to 1 2 3 4.
Finally, merge 5 6 7 8 and 1 2 3 4 to 1 2 3 4 5 6 7 8.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
47
Analyzing the Merge Sort Algorithm
 In an array of size n, count how many times an array
element is visited.
 Assume n is a power of 2: n = 2m.
 Calculate the number of visits to create the two subarrays and then merge the two sorted arrays:
• 3 visits to merge each element or 3n visits
• 2n visits to create the two sub-arrays
• total of 5n visits
Copyright © 2014 by John Wiley & Sons. All rights reserved.
48
Merge Sort Vs Selection Sort
 Selection sort is an O(n2) algorithm.
 Merge sort is an O(n log(n)) algorithm.
 The n log(n) function grows much more slowly than n2.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
49
Merge Sort Timing vs. Selection Sort
Copyright © 2014 by John Wiley & Sons. All rights reserved.
50
The Quicksort Algorithm
 No temporary arrays are required.
1. Divide and conquer Partition the range
2. Sort each partition
 In quicksort, one partitions the elements into two groups,
holding the smaller and larger elements. Then one sorts
each group.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
51
The Quicksort Algorithm
public void sort(int from, int to)
{
if (from >= to) return;
int p = partition(from, to);
sort(from, p);
sort(p + 1, to);
}
Copyright © 2014 by John Wiley & Sons. All rights reserved.
52
The Quicksort Algorithm
 Starting range
 A partition of the range so that no element in first
section is larger than element in second section
 Recursively apply the algorithm until array is sorted
Copyright © 2014 by John Wiley & Sons. All rights reserved.
53
The Quicksort Algorithm
private static int partition(int[] a, int from, int to)
{
int pivot = a[from];
int i = from - 1;
int j = to + 1;
while (i < j)
{
i++; while (a[i] < pivot) { i++; }
j--; while (a[j] > pivot) { j--; }
if (i < j) { ArrayUtil.swap(a, i, j); }
}
return j;
}
Copyright © 2014 by John Wiley & Sons. All rights reserved.
54
The Quicksort Algorithm - Partioning
Copyright © 2014 by John Wiley & Sons. All rights reserved.
55
The Quicksort Algorithm
 On average, the quicksort algorithm is an O(n log(n))
algorithm.
 Its worst-case run-time behavior is O(n²).
 If the pivot element is chosen as the first element of the
region,
• That worst-case behavior occurs when the input set is already
sorted
Copyright © 2014 by John Wiley & Sons. All rights reserved.
56
 Sort Animator:
• http://www.csbsju.edu/computer-science/useful-links/launchcs-applications
• Comparison Sorting
•
•
•
•
•
•
•
•
•
•
Bubble Sort
Selection Sort
Insertion Sort
Shell Sort
Merge Sort
Quick Sort
Bucket Sort
Counting Sort
Radix Sort
Heap Sort
From: http://www.cs.usfca.edu/~galles/visualization/
Copyright © 2014 by John Wiley & Sons. All rights reserved.
57
Searching
 Linear search:
• also called sequential search
• Examines all values in an array until it finds a match or
reaches the end
• Number of visits for a linear search of an array of n
elements:
• The average search visits n/2 elements
• The maximum visits is n
• A linear search locates a value in an array in O(n) steps
Copyright © 2014 by John Wiley & Sons. All rights reserved.
58
Programming Question
 Implement the class LinearSearch. Implement the method
search that performs linear search. The method should
accept as parameters an int array and search value. The
method should return the index of the value if found in
array or -1 otherwise.
 Then implement the tester class LinearSearchDemo that
test sort method of LinearSearch class.
 A sample output is shown:
Program Run:
[46, 99, 45, 57, 64, 95, 81, 69, 11, 97, 6, 85, 61, 88, 29, 65, 83, 88, 45, 88]
Enter number to search for, -1 to quit: 12
Found in position -1
Enter number to search for, -1 to quit: -1
Copyright © 2014 by John Wiley & Sons. All rights reserved.
59
Answer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
LinearSearch.java
/**
A class for executing linear searches in an array.
*/
public class LinearSearcher
{
/**
Finds a value in an array, using the linear search
algorithm.
@param a the array to search
@param value the value to find
@return the index at which the value occurs, or -1
if it does not occur in the array
*/
public static int search(int[] a, int value)
{
for (int i = 0; i < a.length; i++)
{
if (a[i] == value) { return i; }
}
return -1;
}
}
Copyright © 2014 by John Wiley & Sons. All rights reserved.
60
LinearSearchDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.Arrays;
import java.util.Scanner;
/**
This program demonstrates the linear search algorithm.
*/
public class LinearSearchDemo
{
public static void main(String[] args)
{
int[] a = ArrayUtil.randomIntArray(20, 100);
System.out.println(Arrays.toString(a));
Scanner in = new Scanner(System.in);
boolean done = false;
while (!done)
{
System.out.print("Enter number to search for, -1 to quit: ");
int n = in.nextInt();
if (n == -1)
{
done = true;
}
else
{
int pos = LinearSearcher.search(a, n);
System.out.println("Found in position " + pos);
}
}
}
}
Copyright © 2014 by John Wiley & Sons. All rights reserved.
61
Binary Search
 A binary search locates a value in a sorted array by:
• Get middle element in array
• If middle==searhckey
return success
• Else
• Divide array into two halves.
• Determining whether the value occurs in the first or second half
• Then repeating the search in one of the halves
 The size of the search is cut in half with each step.
 Animator:
http://www.cs.armstrong.edu/liang/animation/web/BinarySearch.html
Copyright © 2014 by John Wiley & Sons. All rights reserved.
62
Binary Search
 Searching for 15 in this array
 The last value in the first half is 9
• So look in the second (darker colored) half
 The last value of the first half of this sequence is 17
• Look in the darker colored sequence
Copyright © 2014 by John Wiley & Sons. All rights reserved.
63
Binary Search
 The last value of the first half of this very short
sequence is 12 (<15),
• This is smaller than the value that we are searching,
• so we must look in the second half
 15 ≠ 17: we don't have a match
Copyright © 2014 by John Wiley & Sons. All rights reserved.
64
Programming Question
 Implement BinarySearcher class. The search method
should implement binary search. The method should
return the index of the value if found in array or -1
otherwise.
public static int search(int[] a, int low, int high, int value)
Original array
Copyright © 2014 by John Wiley & Sons. All rights reserved.
Index range to search
Search key
65
Answer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
A class for executing binary searches in an array.
*/
public class BinarySearcher
{
/**
Finds a value in a range of a sorted array, using the binary
search algorithm.
@param a the array in which to search
@param low the low index of the range
@param high the high index of the range
@param value the value to find
@return the index at which the value occurs, or -1
if it does not occur in the array
*/
Continued
Copyright © 2014 by John Wiley & Sons. All rights reserved.
66
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public static int search(int[] a, int low, int high, int value)
{
if (low <= high)
{
int mid = (low + high) / 2;
if (a[mid] == value)
{
return mid;
}
else if (a[mid] < value )
{
return search(a, mid + 1, high, value);
}
else
{
return search(a, low, mid - 1, value);
}
}
else
{
return -1;
}
}
}
Copyright © 2014 by John Wiley & Sons. All rights reserved.
67
Binary Search
 Count the number of visits to search a sorted array of
size n
• We visit one element (the middle element) then search either the
left or right subarray
• Thus: T(n) = T(n/2) + 1
 If n is n / 2, then T(n / 2) = T(n / 4) + 1
 Substituting into the original equation: T(n) = T(n / 4) + 2
 This generalizes to: T(n) = T(n / 2k) + k
Copyright © 2014 by John Wiley & Sons. All rights reserved.
68
Binary Search
 Assume n is a power of 2, n = 2m
where m = log2(n)
 Then: T(n) = 1 + log2(n)
 A binary search locates a value in a sorted array in
O(log(n)) steps.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
69
Question
Suppose you need to look through 1,000,000 records to
find a telephone number. How many records do you
expect to search before finding the number?
Copyright © 2014 by John Wiley & Sons. All rights reserved.
70
Answer
Answer: On average, you’d make 500,000
comparisons.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
71
Question
Suppose you need to look through a sorted array with
1,000,000 elements to find a value. Using the binary
search algorithm, how many records do you expect to
search before finding the value?
Copyright © 2014 by John Wiley & Sons. All rights reserved.
72
Answer
Answer: You would search about 20. (The binary log
of 1,024 is 10.)
Copyright © 2014 by John Wiley & Sons. All rights reserved.
73
Question
Suppose you need to look through a sorted array with
1,000,000 elements to find a value. Using the binary
search algorithm, how many records do you expect to
search before finding the value?
Answer: You would search about 20. (The binary log
of 1,024 is 10.)
Copyright © 2014 by John Wiley & Sons. All rights reserved.
74
Problem Solving: Estimating the Running Time of an
Algorithm - Linear time
 Example: an algorithm that counts how many elements
have a particular value
int count = 0;
for (int i = 0; i < a.length; i++)
{
if (a[i] == value) { count++; }
}
Copyright © 2014 by John Wiley & Sons. All rights reserved.
75
Problem Solving: Estimating the Running Time of an
Algorithm – Linear Time
 Pattern of array element visits
int count = 0;
for (int i = 0; i < a.length; i++)
{
if (a[i] == value) { count++; }
}
 There are a fixed number of actions in each visit
independent of n.
 A loop with n iterations has O(n) running time if each
step consists of a fixed number of actions.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
76
Problem Solving: Estimating the Running Time of an
Algorithm – Linear Time
 Example: an algorithm to determine if a value occurs in
the array
boolean found = false;
for (int i = 0; !found && i < a.length; i++)
{
if (a[i] == value) { found = true; }
}
 Search may stop in the middle
 Still O(n) because we may have to traverse the whole
array.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
77
Problem Solving: Estimating the Running Time of an
Algorithm – Quadratic Time
 Problem: Find the most frequent element in an array.
 Try it with this array
 Count how often each element occurs.
• Put the counts in an array
• Find the maximum count
• It is 3 and the corresponding value in original array is 7
Copyright © 2014 by John Wiley & Sons. All rights reserved.
78
Problem Solving: Estimating the Running Time of an
Algorithm – Quadratic Time
 Estimate how long it takes to compute the counts
for (int i = 0; i < a.length; i++)
{
counts[i] = Count how often a[i] occurs in a
}
• We visit each array element once - O(n)
• Count the number of times that element occurs - O(n)
• Total running time - O(n²)
Copyright © 2014 by John Wiley & Sons. All rights reserved.
79
Problem Solving: Estimating the Running Time of an
Algorithm – Quadratic Time
 Three phases in the algorithm
• Compute all counts. O(n²) //dominating term
• Compute the maximum. O(n)
 A loop with n iterations has O(n²) running time if each
step takes O(n) time.
 The big-Oh running time for doing several steps in a row
is the largest of the big-Oh times for each step.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
80
Problem Solving: Estimating the Running Time of an
Algorithm – Logarithmic Time
 This takes the same amount of work per iteration:
• visits two elements
• 2n which is O(n)
 Running time of entire algorithm is O(n log(n)).
 An algorithm that cuts the size of work in half in each
step runs in O(log(n)) time.
Copyright © 2014 by John Wiley & Sons. All rights reserved.
81
Sorting and Searching in the Java Library - Sorting
 You do not need to write sorting and searching
algorithms
• Use methods in the Arrays and Collections classes
 The Arrays class contains static sort methods.
 To sort an array of integers:
int[] a = . . . ;
Arrays.sort(a);
• That sort method uses the Quicksort
 To sort an ArrayList, use Collections.sort
ArrayList<String> names = . . .;
Collections.sort(names);
• Uses merge sort algorithm
Copyright © 2014 by John Wiley & Sons. All rights reserved.
82
Sorting and Searching in the Java Library – Binary Search
 Arrays and Collections classes contain static
binarySearch methods.
• If the element is not found, returns -k - 1
• Where k is the position before which the element should be
inserted
 For example
int[] a = { 1, 4, 9 };
int v = 7;
K=2
int pos = Arrays.binarySearch(a, v);
// Returns –3; v should be inserted before position 2
Copyright © 2014 by John Wiley & Sons. All rights reserved.
83
Comparing Objects
 Arrays.sort sorts objects of classes that implement
Comparable interface:
public interface Comparable
{
int compareTo(Object otherObject);
}
 The call a.compareTo(b) returns
• A negative number if a should come before b
• 0 if a and b are the same
• A positive number otherwise
Copyright © 2014 by John Wiley & Sons. All rights reserved.
84
Comparing Objects
 Some java library classes implement Comparable.
• e.g. String and Date
 You can implement Comparable interface for your own
classes.
• The Country class could implement Comparable:
public class Country implements Comparable
{
public int compareTo(Object otherObject)
{
Country other = (Country) otherObject;
if (area < other.area) { return -1; }
else if (area == other.area) { return 0; }
else { return 1; }
}
}
Copyright © 2014 by John Wiley & Sons. All rights reserved.
85
Comparing Objects
 You could pass an array of countries to Arrays.sort
Country[] countries = new Country[n];
Arrays.sort(countries); // Sorts by increasing area
Copyright © 2014 by John Wiley & Sons. All rights reserved.
86