Sie sind auf Seite 1von 47

Data Structures for Java

Chapter 4
Introduction to Algorithms
Selection Sort
 Proceed through an array position by
position, starting with index 0. At the
current position, select the element from
the remaining elements that is next in
order and copy it into the current position.
Selection Sort Example Step 1

 The ordering begins by locating the


smallest animal, the fish, in the first
position. The operation occurs by having
the owl that currently occupies the first
position exchange places with the fish.
The effect is to place the smallest animal
at the front of the list.
Selection Sort Example Step 2

O rdered U n o rdered

 The tail of the list, starting at the second


position is unordered. The process
continues by identifying the smallest
animal among the owl, dragon, and dog
and arranging it in the second position.
The selection is the owl that is already in
its proper position and so we can move to
the next step.
Selection Sort Example Step 3

O rdered U n o rdered
 With the fish and owl in position, only the
last two animals must be ordered. We
identify the dog as the next-smallest
animal and have it exchange positions
with the dragon. After this step, the tail of
the list has only one item left, which must
be the largest animal. The entire list is
ordered.
Selection Sort in Action
selectionSort()
public static void selectionSort(int[] arr)
{
// index of smallest element in the sublist
int smallIndex;
int pass, j, n = arr.length;
int temp;

// pass has the range 0 to n-2


for (pass = 0; pass < n-1; pass++)
{
// scan the sublist starting at index pass
smallIndex = pass;
selectionSort() (concluded)
// j traverses the sublist
// arr[pass+1] to arr[n-1]
for (j = pass+1; j < n; j++)
// if smaller element found, assign
// smallIndex to that position
if (arr[j] < arr[smallIndex])
smallIndex = j;

// swap the next smallest


// element into arr[pass]
temp = arr[pass];
arr[pass] = arr[smallIndex];
arr[smallIndex] = temp;
}
}
Selection Sort Example
// declare an integer array
int[] arr = {66, 20, 33, 55, 53, 57, 69, 11, 67, 70};
// call selectionSort() to order the array
Arrays.selectionSort(arr);
System.out.print("Sorted: ");
for (int i=0; i < arr.length; i++)
System.out.print(arr[i] + " ");

Output:
Sorted: 11 20 33 53 55 57 66 67 69 70
Sublists in an Array
 A sublist of an array is a sequence of
elements whose range of indices begin at
index first and continue up to, but not
including last. Indicate a sublist using
the notation [first, last).
Sequential Search
 Begin with a target value and an index
range [first, last). Scan the sublist
item by item, looking for the first
occurrence of a match with an item
named target. Return the index of the
match or -1 if target is not in the sublist.
The seqSearch() Method
seqSearch() Implementation
public static int seqSearch(int[] arr,
int first, int last, int target)
{
// scan indices in the range first
// <= i < last; return the index
// indicating the position if a match occurs;
// otherwise return -1
for (int i = first; i < last; i++)
if (arr[i] == target)
return i;

// no return yet if match is


// not found; return -1
return -1;
}
Binary Search
 The binary search exploits the fact that a list is
ordered. This allows large sections of the list to be
removed from the search.
 Select the midpoint of the current sublist
[first,last).
 If target matches midpoint, the search is successful.
 If the target is less than the current midpoint value, look
in the lower sublist by selecting its midpoint; otherwise,
look in the upper sublist by selecting its midpoint.
 Continue until locating the target or the sublist reduces
to size 0.
Binary Search Case 1
 A match occurs. The search is complete,
and mid is the index that locates target.
Binary Search Case 2
 The value target is less than midValue,
and the search must continue in the lower
sublist. The index range for this sublist is
[first, mid). Reposition the index last
to the end of the sublist (last = mid).
Binary Search Case 3
 The value target is greater than
midValue, and the search must continue
in the upper sublist . The index range for
this sublist is [mid+1,last), because the
sublist begins immediately to the right of
mid . Reposition the index first to the front
of the sublist (first = mid+1).
Binary Search Termination
 The binary search terminates when a
match is found or when the sublist to be
searched is empty. An empty sublist
occurs when first >= last.
Binary Search Success
Example Step 1

target = 23
Binary Search Success
Example Step 2
target = 23
Binary Search Success
Example Step 3
target = 23
Binary Search Failure
Example Step 1
target = 4
Binary Search Failure
Example Step 2
target = 4
Binary Search Failure
Example Step 3
target = 4
Binary Search Failure
Example Step 4
Index range [2,2). first ≥ last, so the search terminates
unsuccessfully. The return value is -1.
binSearch() Method
public static int binSearch(int arr[], int first,
int last, int target)
{
// index of the midpoint
int mid;
// value that is assigned arr[mid]
int midValue;

// test for nonempty sublist


while (first < last)
{
mid = (first+last)/2;
midValue = arr[mid];
binSearch() Method
(concluded)
if (target == midValue)
// have a match
return mid;
// determine which sublist to search
else if (target < midValue)
// search lower sublist; reset last
last = mid;
else
// search upper sublist; reset first
first = mid+1;
}
// target not found
return -1;
}
System/Memory Efficiency
 System efficiency measures how well an
algorithm runs on a particular machine.
 Memory efficiency is a measure of the
amount of memory an algorithm uses. If
an algorithm uses too much memory, it
can be too slow or may not execute at all
on a particular system.
Running Time Analysis
 Machine independent measures of
efficiency involve counting the number of
comparisons, assignment statements, etc.
 To analyze the complexity of an algorithm,
identify the dominant operation and
measure the number of times it occurs.
Running Time: min() Method
 Count the number of comparisons, T(n),
required to locate the minimum of the
elements in an n-element array.

T(n) = n-1
Running Time: Selection Sort
 Count the number of comparisons in the n-1
passes of the algorithm.

T(n) = (n-1) + (n-2) + ... + 2 + 1

T(n) = n(n-1)/2 = n2/2 - n/2


Running Time:
Sequential Search
 Best case: Locating the target at index 0.
T(n) = 1
 Worst caset: Locating the target at index n-1 or
finding no match.
T(n) = n
 Average case: Average of the number of
comparisons to locate a target at each position.
T(n)=(1+2+3...+n)/n=n(n+1)/2 (1/n)
= (n+1)/2
Running Time: Binary Search
 Best case: Target found at first midpoint.
T(n) = 1
 Worst case: Length of sublists decrease by a factor of two at
each iteration.
T(n) = (int)log2n + 1
 Average case: A sophisticated analysis shows that the average
number of iterations is one less than the number in the worst
case.
Big-O Notation
 To get an approximate measure for T(n),
define a notation that involves the dominant
term of T(n). O(<term>) is called the Big-O
measure for the algorithm.
 Selection sort is O(n2).
 The average case for sequential search is O(n).
 The worst case for binary search is O(log2n).
 If T(n) = 8n3+5n2-11n+1, then T(n) is O(n3).
Common Orders of Magnitude
 Constant time: Algorithm is O(1) when its
running time is independent of the
number of items.
 Examples: Find the minimum of an ordered
n-element array.
arr
1 3 4 6 8 10 15 20 35 55
m in im u m = a r r [0 ]
Common Orders of Magnitude
(continued)
 Linear: Algorithm is O(n) when its running
time is proportional to n, the size of the
list. If n doubles, number of operations
doubles.
 Example: Find the minimum of an unordered
n-element array.
Common Orders of Magnitude
(continued)
 Quadratic: An algorithm is quadratic if its
running time is O(n2). If n doubles, the number
of operations increases by a factor of 4.
 Example: Selection sort.
 Cubic: An algorithm is cubic if its running time
is O(n3). Doubling n increases the number of
operations by a factor of 8.
 Example: The number of products in matrix
multiplication is cubic.
Common Orders of Magnitude
(continued)
 Logarithmic: An algorithm is logarithmic if
its running time is O(log2) or O(n log2).
Occurs when the algorithm repeatedly
subdivides the data into sublists whose
sizes are 1/2, 1/4, 1/8, ... of the original
size n.
 Example: Binary search is O(log2), and
quicksort is O(n log2).
Common Orders of Magnitude
(concluded)
 Exponential: An algorithm is exponential if
its running time is O(an). These algorithms
deal with problems that require searching
through a large number of potential
solutions before finding an answer.
 Example: The traveling salesperson problem.
Comparison of Graphs
Orders of Magnitude for
Selected Values of n
n log2n n log2n n2 n3 2n
2 1 2 4 8 4
4 2 8 16 64 16
8 3 24 64 512 256
16 4 64 256 4096 65536
32 5 160 1024 32768 4294967296
128 7 896 16384 2097152 3.4 x 1038
1024 10 10240 1048576 1073741824 1.8 x 10308
65536 16 1048576 4294967296 2.8 x 1014 Forget it!
The Timing Class
class TIMING ds.time

Constructor
Timing()
Create an instance of the class with all timing
variables set to 0.
Methods
void start()
Start the timing
double stop()
Stop the timing and return elapsed time in
seconds.
Timing Class Example
Timing sortTimer = new Timing();
double timeInSec;

// flank the process with calls to start() and stop()


sortTimer.start(); // start timing
Sort.selectionSort(arr); // the sorting process
timeInSec = sortTimer.stop(); // get time in seconds
Program 4.1
import java.util.Random;
import java.text.DecimalFormat;
import ds.util.Arrays;
import ds.time.Timing;

public class Program4_1


{
public static void main(String[] args)
{
final int ARRAY_SIZE = 100000,
TARGET_SIZE = 50000;

// arrays for the search


int[] listSeq = new int[ARRAY_SIZE],
listBin = new int[ARRAY_SIZE],
targetList = new int[TARGET_SIZE];
int i;
Program 4.1 (continued)
// use Timing object t to compute
// times for each process
Timing t = new Timing();
double seqTime, binTime;

// random number object


Random rnd = new Random();

// format real numbers with


// three decimal places
DecimalFormat fmt =
new DecimalFormat("#.000");
Program 4.1 (continued)
// initialize the arrays with
// random numbers in the
// range 0 to 999,999
for (i = 0; i < ARRAY_SIZE; i++)
listSeq[i] =
listBin[i] =
rnd.nextInt(1000000);

// initialize targetList with


// random numbers in the
// same range 0 to 999,999
for (i=0;i < TARGET_SIZE; i++)
targetList[i] = rnd.nextInt(1000000);

// time the sequential search


// with elements from listSeq
t.start();
for (i = 0; i < TARGET_SIZE; i++)
Arrays.seqSearch(listSeq,0,ARRAY_SIZE,
targetList[i]);
Program 4.1 (concluded)
binTime = t.stop();
System.out.println(
"Binary Search takes " +
fmt.format(binTime) + " seconds.");

System.out.println(
"Ratio of sequential to" +
"binary search time is " +
fmt.format(seqTime/binTime));
}
}
/*
Run:

Sequential Search takes 16.094 seconds.


Binary Search takes .016 seconds.
Ratio of sequential to binary search time is
1005.875
*/

Das könnte Ihnen auch gefallen