All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class rebelsky.util.IntegerArrayAlgorithms

java.lang.Object
   |
   +----rebelsky.util.IntegerArrayAlgorithms

public class IntegerArrayAlgorithms
extends Object
A collection of algorithms appropriate for arrays of integers. While this class was originally designed to support an exercise in algorithm measurement, it is hoped that it has other uses.

Version:
1.0 of February 1998
Author:
Samuel A. Rebelsky

Constructor Index

o IntegerArrayAlgorithms()

Method Index

o binarySearch(int[], int)
Search a sorted array of integers for a particular value.
o bubbleSort(int[])
Sort an array of integers by "bubbling" values up the array.
o indexOfSmallest(int[])
Find the position of the smallest element in an array of integers.
o indexOfSmallest(int[], int)
Find the position of the smallest element in a subrange of an array of integers.
o indexOfSmallest(int[], int, int)
Find the position of the smallest element in a subrange of an array of integers.
o random(int)
Create an array of integers of size n with the population of the array being "random".
o random(int, int)
Create an array of nonnegative integers of size n with the population of the array being "random" and the values between 0 ...
o selectionSort(int[])
Sort an array of integers using the selection sort algorithm, in which the smallest remaining element is repeatedly identified and moved to the front of the list.
o sequentialSearch(int[], int)
Search through an unsorted array of integers to determine whether a particular value (key) is in the array.
o swap(int[], int, int)
Swap the elements at positions a and b in an integer array.

Constructors

o IntegerArrayAlgorithms
 public IntegerArrayAlgorithms()

Methods

o binarySearch
 public static boolean binarySearch(int iarr[],
                                    int key)
Search a sorted array of integers for a particular value.
pre: The array is sorted in increasing order.
pre: The array is nonempty.
post: The array is not affected.

Returns:
true if the value is in the array; false otherwise.
o bubbleSort
 public static int[] bubbleSort(int iarr[])
Sort an array of integers by "bubbling" values up the array.
pre: (none)
post: the original array is unmodified.
post: for 0 <= i < length-1, sorted[i] <= sorted[i+1]
post: if element x appears n times in the original array, it also appears n times in the sorted array
post: the sorted array has the same length as the original array.

Returns:
a sorted version of the original array.
o indexOfSmallest
 public static int indexOfSmallest(int iarr[])
Find the position of the smallest element in an array of integers.
pre: The array is nonempty.
post: The array is unaffected.
post: The index smallest element is returned.

Returns:
x s.t. iarr[x] <= iarr[i] for all 0 <= i < iarr.length
o indexOfSmallest
 public static int indexOfSmallest(int iarr[],
                                   int lb)
Find the position of the smallest element in a subrange of an array of integers.
pre: The subrange is nonempty.
post: The array is unaffected.
post: The index smallest element is returned.

Returns:
x s.t. iarr[x] <= iarr[i] for all lb <= i < iarr.length
o indexOfSmallest
 public static int indexOfSmallest(int iarr[],
                                   int lb,
                                   int ub)
Find the position of the smallest element in a subrange of an array of integers.
pre: The subrange is nonempty.
post: The array is unaffected.
post: The index smallest element is returned.

Returns:
x s.t. iarr[x] <= iarr[i] for all lb <= i <= ub
o random
 public static int[] random(int size,
                            int bound) throws Exception
Create an array of nonnegative integers of size n with the population of the array being "random" and the values between 0 ... bound.
pre: n >= 0
pre: bound >= 0
post: a newly-allocated array of size n is returned

Example (10 integers between 0 and 100): int[] stuff = IntegerArrayAlgorithms.random(10,100);

Throws: Exception
When the preconditions are not met.
o random
 public static int[] random(int size) throws Exception
Create an array of integers of size n with the population of the array being "random".
pre: n >= 0
post: a newly-allocated array of size n is returned

Example:
int[] stuff = IntegerArrayAlgorithms.random(10);

Throws: Exception
when size < 0
o selectionSort
 public static int[] selectionSort(int iarr[])
Sort an array of integers using the selection sort algorithm, in which the smallest remaining element is repeatedly identified and moved to the front of the list.
pre: (none)
post: the original array is unmodified.
post: for 0 <= i < length-1, sorted[i] <= sorted[i+1]
post: if element x appears n times in the original array, it also appears n times in the sorted array array.

o sequentialSearch
 public static boolean sequentialSearch(int iarr[],
                                        int key)
Search through an unsorted array of integers to determine whether a particular value (key) is in the array.
pre: (none)
post: the array is unaffected

Returns:
true if the key is in the array; false otherwise.
o swap
 public static void swap(int iarr[],
                         int a,
                         int b)
Swap the elements at positions a and b in an integer array.
pre: 0 <= a,b < iarr.length
pre: the array is nonempty


All Packages  Class Hierarchy  This Package  Previous  Next  Index