This preview shows page 1. Sign up to view the full content.
Unformatted text preview: 1.00 Lecture 34
Sorting 2 Reading for next time: Big Java 21.121.3 Counting sort
• O(n) sort when keys are small integers
• Procedure:
– Count the number of records with a given key
– Calculate the number of records with keys that
are less than given key by adding up their
counts
– This tells you where the final position of your
keys will be
– Move keys to final positions and you’re done • You never compare any value pairs!
– This is special case of radix sorting 1 Counting sort example
Input
ia
03
15
22
33
size Count:
0
1
2
3
4
5 #
0
0
1
2
0
1 Cum
0
0
1 1 offset
3
3
4 Output
b
i
0
2
1
3
2
3
3
5 range
Move values to output vector or array Steps in the CountingSort Algorithm
1. 2. 3. 4. Copy the numbers to be sorted to a temporary array.
Initialize an array indexed by key values (a count of keys)
to 0 (Java initializes arrays to 0 automatically)
Iterate over the array to be sorted counting the frequency
of each key.
Calculate the cumulative count for each key value, k. The
first element, k=0, is the same as the frequency of key k.
The second, k=1, is the sum of the frequency for k=0 and
k=1. The third is the sum of the second plus the
frequency for k=2. And so on. 2 Steps in the CountingSort Algorithm, 2
5. 6. 7. The first element of the cumulative count contains the
number of elements of the original array with values <= 0.
the second, those <= 1. They lay out blocks of values in
the sorted array.
Starting with the last element in the original array and
working back to the first, look up its key in the cumulative
count to find its destination in the sorted array. Its
count value
destination = count value – 1.
Decrement the entry in the cumulative count so the next
element is not stored on top of the first, if there are more
than one at the same key value (e.g., two 3s) CountingSort
Range
(maximum element value)
Range = 8 (maximum element value)
4 3 7 6 4 8 3 5 2 Frequencies (no of keys w/each value)
Frequencies (no of keys w/each value) There are no keys
0, 1
Key 2 occurs 1 time
and should occupy
position 0. 0 1 2 3 4 5 6 7 8 0 0 1 2 2 1 1 1 1 Key 3 occurs 2 times
and should occupy
positions 1 and 2. Cumulative count
Cumulative count 0 1 2 3 4 5 6 7 8 0 0 1 3 5 6 7 8 9 Key 4 occurs 2 times
and should occupy
positions 3 and 4.
Etc.
(Our code actually
writes values from
last to first) Sorted output
Sorted output 0 1 2 3 4 5 6 7 8 2 3 3 4 4 5 6 7 8 3 CountingSort
Range
(maximum element value)
Range = 8 (maximum element value)
4 3 7 6 4 8 3 5 2 Count (no of keys w/each value)
Count (no of keys w/each value) There are no keys
0, 1
Key 2 occurs 1 time
and should occupy
position 0. 0 1 2 3 4 5 6 7 8 0 0 1 2 2 1 1 1 1 Key 3 occurs 2 times
and should occupy
positions 1 and 2. Cumulative count
Cumulative count 0 1 2 3 4 5 6 7 8 0 0 1 3 5 6 7 8 9 Key 4 occurs 2 times
and should occupy
positions 3 and 4.
Etc.
(Our code actually
writes values from
last to first) Sorted output
Sorted output 0 1 2 3 4 5 6 7 8 2 3 3 4 4 5 6 7 8 CountingSort Questions
• Download and double click the
CountingSort2004.jar
CountingSort2004.jar file to run the simulation.
– It works the same way as the others. • Experiment with the simulation.
– Type the numbers to be sorted first.
• Hit carriage return after entering each number
• Use single digit numbers to keep the range small – Then enter the range (the largest number you entered) in the
range field and press the start button.
start
– Use stepinto to trace the code.
stepinto • Does counting sort have a best case or worst case?
• Does it sort in O(1), O(n), O(n log n) or O(n2) time?
Why? 4 Counting Sort
import javax.swing.*;
import javax.swing.*;
public class CountingSort { // A few changes from the demo
class
few changes from the demo
sortRange;
private int sortRange;
range;}
public CountingSort(int range) { sortRange = range;}
public void sort(int d, int v) {
void sort(int d, int v)
int value
new int[d.length];
int value = new int[d.length]; int extraStuff= new int[d.length];
‘Value’ field
int extraStuff= new int[d.length];
// ‘Value’ field int count
new int[sortRange];
int count = new int[sortRange]; size
d.length;
int i, size = d.length; System.arraycopy(d, 0, value, 0, size);
Copy arrays
System.arraycopy(d, 0, value, 0, size); // Copy arrays System.arraycopy(v, 0, extraStuff, 0, size);
System.arraycopy(v, 0, extraStuff, 0, size); (i
0;
size; i++)
for (i = 0; i < size; i++) count[value[i]]++; (i
1;
sortRange; i++)
for (i = 1; i < sortRange; i++) += count[i
count[i] += count[i 1]; (i
size
>= 0; i
)
for (i = size 1; i >= 0; i) {
extraStuff[i];
chg
v[count[value[i]] 1] = extraStuff[i]; //Only chg
value[i];
d[count[value[i]] 1] = value[i];
count[value[i]]
;
count[value[i]];
} } Counting Sort, p.2
public static void main(String args)
public static void main(String args) { input
String input = elements:");
JOptionPane.showInputDialog("Enter no. elements:");
Integer.parseInt(input);
int size = Integer.parseInt(input);
int
new int[size];
What will be sorted
int keyData = new int[size];
// What will be sorted
int valueData = new int[size];
int
new int[size];
// Associated data
Associated data
1000;
int range = 1000;
new CountingSort(range);
CountingSort s = new CountingSort(range);
for (int i = 0; i < size; i++) {
(int
0;
size; i++)
// Random data
Random data
keyData[i]
(int (range
int)
Math.random());
keyData[i] = (int) (range * Math.random());
valueData[i]
keyData[i]
7;
valueData[i] = keyData[i] + 7;
}
s.sort(keyData, valueData);
Call with “parallel arrays”
s.sort(keyData, valueData);
// Call with “parallel arrays”
System.out.println("Done");
System.out.println("Done");
(size <= 1000)
if (size <= 1000)
(int
0;
size; i++)
for (int i = 0; i < size; i++)
System.out.println(keyData[i]
valueData[i]);
System.out.println(keyData[i] + " " + valueData[i]);
System.exit(0);
}
} 5 Applications of sorting
• Business applications are obvious:
– Reports sorted by customer, department, etc. • Many technical applications use sorting:
– Satellite payload
– Optimal maintenance policy (e.g., pavement)
– Processor scheduling
– Generating graphs (networks) • One estimate is that 25% of all CPU time is
spent on sorting
– And the other 75% is spent on IM and games? Satellite payload Experiment
A
B
C
D Benefit
4
6
5
5 Weight
2
4
2
3 Ben/Wgt
2.0
1.5
2.5
1.7 Assume satellite can carry max weight= 7 6 Satellite, p.2 Max=7 20
15
D
Benefit 10
5
0 A
C 0 x B
x x x 2 4 6
8
Weight 10 12 Sort in Benefit/Weight ratio. Maximum derivative gives optimum value. Gives solution for all maximum weights M. (‘Greedy algorithm’) Last item in solution may be fractional. Often this is acceptable. If not, we use more sophisticated optimization methods as in PS9 Exercise: Sorting, p. 1
• • Finish class Satellite, which selects experiments to
be taken aloft:
We create an Experiment static nested class, like
insertion sort’s Item, to store the benefit/weight
ratio (key) and weight (value)
– – – – Experiment data members: ratio, weight
Write a constructor with two arguments
Experiment, to be sortable, must implement Comparable:
Implement public int compareTo(Object o) { … }
•
•
•
• Cast o to an Experiment other
Return –1 if other.ratio < ratio
Return 0 if other.ratio == ratio
Return 1 if other.ratio > ratio 7 Exercise, p. 1
public class Satellite
public class Satellite {
static class Experiment implements Comparable
private static class Experiment implements Comparable {
double ratio;
public double ratio;
public int weight;
Write constructor for Experiment
// Write constructor for Experiment
public int compareTo(Object o) {
Complete this method:
// Complete this method:
Cast
to an Experiment other
// Cast o to an Experiment other
Return
if
ratio
// Return –1 if other.ratio < ratio
Return
if
ratio
// Return 0 if other.ratio == ratio
Return
if
ratio
// Return 1 if other.ratio > ratio
} } Exercise: Sorting, p. 2
• In Satellite’s main():
– We created the 4 Experiments from the earlier slide
– We put the experiments in an array of Experiments
– You should call a sort method to put the experiments in
benefit/weight order
•
• Invoke InsertionSortTest.sort() or QuicksortTest.sort() or …
Copy the class from last lecture’s project to this lecture – Set the weight limit= 7 (or ask the user to input it)
– Loop through the sorted experiments, and select the best
ones to go on the satellite that fit within the weight limit
• Keep track of cumulative weight, cumulative benefit – Print out your solution:
•
•
• Experiments to go on the satellite
Total weight
Total benefit 8 Exercise, p.2
public static void main(String args)
public static void main(String args) {
Experiment a= new Experiment(2.0, 2);
a= new Experiment(2.0, 2);
b= new Experiment(1.5, 4);
Experiment b= new Experiment(1.5, 4);
c= new Experiment(2.5, 2);
Experiment c= new Experiment(2.5, 2);
Experiment d= new Experiment(1.66667, 3);
d= new Experiment(1.66667, 3);
e= {a, b, c, d};
Experiment e= {a, b, c, d};
sort method
// Invoke a sort method
Set maximum weight=
(or get it from user via JOptionPane)
// Set maximum weight= 7 (or get it from user via JOptionPane)
Initialize cumulative weight, cumulative benefit=
// Initialize cumulative weight, cumulative benefit= 0
Loop thru the sorted experiments
// Loop thru the sorted experiments Accumulate the weight until the max weight is reached
//
Accumulate the weight until the max weight is reached Accumulate the benefit as each experiment is added
//
Accumulate the benefit as each experiment is added Compute benefit as ratio*weight
//
Compute benefit as ratio*weight Print out each experiment added to the payload
//
Print out each experiment added to the payload Break out of the loop when max weight is reached
//
Break out of the loop when max weight is reached the total benefit
// Print the total benefit Processor scheduling
• You have a set of n jobs to run on a
processor (CPU) or machine
• Each job i has a deadline di and profit pi
• There is one processor or machine
• Each job takes 1 unit of time (simplification)
• You earn the profit if and only if the job is
completed by its deadline
– “Profit” can be the priority of the computer job in a
real time system that discards tasks that cannot
be completed by their deadline 9 Processor scheduling example Number of jobs n=5.
Profit
Job (i)
A
100
B
19
C
27
D
25
E
15 Time slots 1, 2, 3.
Deadline Profit/Time
2
100
1
19
2
27
1
25
3
15 Solution is {C, A, E} for profit of 142 Processor scheduling algorithm
• Sort jobs by profit/time ratio (slope or derivative): A, C, D, B, E • Place each job at latest time that meets its
deadline
– Nothing is gained by scheduling it earlier, and
scheduling it earlier could prevent another
more profitable job from being done C (27) A (100)
0 2 1
Time E (15) 3
D, B infeasible 10 ...
View Full
Document
 Spring '05
 GeorgeKocur

Click to edit the document details