CSC 385 - Data Structures and Algorithms
University of Illinois Springfield
College of Health, Science, and Technology
<, <=, ==, >=, >
.==
for instance, does not compare the contents of objectstrue
equals
method of the Object class==
we need to call the method//Put method inside Rectangle Class
@Override
public boolean equals(Object other) {
if(other == null) {return false;}
if(!(other instanceof Rectangle)) {return false;}
if(other == this) {return true;}
Rectangle otherRect = (Rectangle) other;
return this.width == otherRect.width &&
this.height == otherRect.height;
}
...
//Change r1 == r2 to the following
r1.equals(r2);
equals
handles ==
for objects, what about <
or >
?compareTo
x
and y
are two objects and we do x.compareTo(y)
then the possible outputs becomex.compareTo(y)
Result | Meaning |
---|---|
negative integer | x < y |
zero | x = y |
positive integer | x > y |
implements Comparable<Rectangle>
to the end of the class headercompareTo
method making sure we follow the rules of the results from abovepublic class Rectangle implements Comaprable<Rectangle>{
//instance variables and constructor
@Override
public int compareTo(Rectangle other) {
int area = this.width * this.height;
int otherArea = other.width * other.height;
//if negative then this < other
//if positive then this > other
//if 0 then this = other
return area - otherArea;
}
}
indexOf
contains
//Linear Search Implementation
public int indexOf(Object array[], Object targetValue) {
for(int i = 0; i < array.length; i++) {
if(array[i].equals(targetValue)) {
return i;
}
}
//Element does not exist
return -1;
}
//Linear Search Implementation
public int indexOf(Object array[], Object targetElement) {
return indexOf(array, targetElement, 0);
}
public int indexOf(Object array[], Object targetElement, int index) {
if(index >= array.length) {
return -1;
}
if(array[index].equals(targetElement)) {
return index;
}
return indexOf(array, targetElement, index + 1);
}
if
is executed.//Binary Search Implementation
public int indexOf(Comparable array[], Comparable target) {
int low = 0;
int high = array.length - 1;
int mid;
while(low <= high) {
mid = low + (high - low) / 2;
int result = target.compareTo(array[mid]);
if(result < 0) {// target < array[mid]
high = mid - 1; //ignore upper half of subarray
} else if(result > 0) { //target > array[mid]
low = mid + 1; //ignore lower half of array
} else { //target == array[mid]
return mid;
}
}
//target does not exist in array
return -1;
}
low + (high - low) / 2
(low + high) / 2
because this simpler version can result in integer overflow for large enough arrays.compareTo
method is stored in an int
and this is what is compared to determine if target is less than, greater than, or equal to the element in the middle of the subarraymid - 1
and mid + 1
are used to ignore those values that were already checked//Binary Search Implementation
public int indexOf(Comparable array[], Comparable target) {
return binarySearch(array, target, 0, array.length - 1);
}
private int binarySearch(Comparable array[], Comparable target, int low, int high) {
if(low > high) {
return -1;
}
int mid = low + (high - low) / 2;
int result = target.compareTo(array[mid]);
if(result < 0) {
return binarySearch(array, target, low, mid - 1);
} else if(result > 0) {
return binarySearch(array, target, mid + 1, high);
} else {
return mid;
}
}
low
and high
we change them in the recursive calls\[ \frac{n}{2^i} \geq 1 \]
\[ \begin{align*} \frac{n}{2^i} &\geq 1 \\ n &\geq 2^i \\ log_2(n) &\geq i \end{align*} \]
\[ \begin{align*} T(1) &= 1 \\ T(n) &= T(\frac{n}{2}) + 1 \end{align*} \]
Suppose \(n = 16\)
\[ \begin{align*} T(16) &= T(\frac{n}{2}) + 1 = T(8) + 1 \\ T(8) &= T(\frac{n}{2}) + 1 = T(4) + 1 \\ T(4) &= T(\frac{n}{2}) + 1 = T(2) + 1 \\ T(2) &= T(\frac{n}{2}) + 1 = T(1) + 1 \\ T(1) &= 1 \end{align*} \]
\[ \begin{align*} T(1) &= 1 \\ T(2) &= T(1) + 1 = 1 + 1 = 2 \\ T(4) &= T(2) + 1 = 2 + 1 = 3 \\ T(8) &= T(4) + 1 = 3 + 1 = 4 \\ T(16) &= T(8) + 1 = 4 + 1 = 5 \end{align*} \]
We actually end up with \(T(n) = log_2(n) + 1\) which is still \(O(log n)\)
Guess: \(T(n) = log_2(n) + 1\)
Base Case \[ T(1) = log_2(1) + 1 = 0 + 1 = 1 ✓ \]
Inductive case
First, we will assume the input size is a power of 2
\[ log_2(2^k) + 1 = k + 1 \]
\[ \begin{align*} T(2^{k + 1}) &= T(\frac{2^{k + 1}}{2}) + 1 \\ &= T(2^k) + 1 \\ &= log_2(2^k) + 2 \\ &= k + 2 ✓ \end{align*} \]
equals
methodComparable
interface or have some method to indicate orderBest Case | Average Case | Worst Case | |
---|---|---|---|
Linear Search (unsorted data) | \(O(1)\) | \(O(n)\) | \(O(n)\) |
Linear Search (sorted data) | \(O(1)\) | \(O(n)\) | \(O(n)\) |
Binary Search (sorted data) | \(O(1)\) | \(O(log n)\) | \(O(log n)\) |
public static void sort(Comparable a[])
which is similar to the searchselection sort algorithm(collection) {
for i in 0 to length(collection) - 1 {
smallest = i
for j in (i + 1) to length(collection) - 1 {
if collection[j] < collection[smallest] {
smallest = j
}
}
swap(collection, i, smallest)
}
}
[14, 5, 4, 9, 7, 13, 2]
Find the first smallest element, 2, and swap it with the element at index 0:
[2, 5, 4, 9, 7, 13, 14]
Find the second smallest, 4, and swap it with the element at index 1:
[2, 4, 5, 9, 7, 13, 14]
Find the third smallest, 5, and swap it with the element at index 2:
[2, 4, 5, 9, 7, 13, 14]
Find the fourth smallest, 7, and swap it with the element at index 3:
[2, 4, 5, 7, 9, 13, 14]
Find the fifth smallest, 9, and swap it with the element at index 4:
[2, 4, 5, 7, 9, 13, 14]
Find the sixth smallest, 13, and swap it with the element at index 5:
[2, 4, 5, 7, 9, 13, 14]
\(\leftarrow\) done
Iteration | List |
---|---|
Initial List | \(7_1, 9, 7_2, 3_1, 3_2\) |
1 | \(3_1, 9, 7_2, 7_1, 3_2\) |
2 | \(3_1, 3_2, 7_2, 7_1, 9\) |
insertion sort algorithm(collection) {
//1 <= i < length
for i in 1 to length(collection) - 1 {
current = collection[i]
//0 <= j < i
for j in (i - 1) to 0 {
if current < collection[j] {
collection[j+1] = collection[j]
} else {
break;
}
}
collection[j+1] = current;
}
}
[14, 5, 4, 9, 7, 13, 2]
[14, 5, 4, 9, 7, 13, 2]
\(\rightarrow\) [5, 14, 4, 9, 7, 13, 2]
[5, 14, 4, 9, 7, 13, 2]
\(\rightarrow\) [4, 5, 14, 9, 7, 13, 2]
[4, 5, 14, 9, 7, 13, 2]
\(\rightarrow\) [4, 5, 9, 14, 7, 13, 2]
[4, 5, 9, 14, 7, 13, 2]
\(\rightarrow\) [4, 5, 7, 9, 14, 13, 2]
[4, 5, 7, 9, 14, 13, 2]
\(\rightarrow\) [4, 5, 7, 9, 13, 14, 2]
[4, 5, 7, 9, 13, 14, 2]
\(\rightarrow\) [2, 4, 5, 7, 9, 13, 14]
Total number of comparisons: 16
5 < 14 - 1 comparison
4 < 14, 4 < 5 - 2 comparisons
9 < 14, 9 < 5 - 2 comparisons
7 < 14, 7 < 9, 7 < 5 - 3 comparisons
13 < 14, 13 < 9, 2 comparisons
2 < 14, 2 < 13, 2 < 9, 2 < 7, 2 < 5, 2 < 4 - 6 comparisons
if current < collection[j]
to
if current <= collection[j]
shell sort algorithm(collection) {
for k in length(collection) / 2 to 1 by k = k / 2{
for i in 0 to k {
incremental insertion sort(collection, i, k)
}
}
}
increment insertion sort(collection, start, inc) {
for i in start+inc to length(collection) by i += inc {
for j in i to inc by j -= inc {
if collection[j] < collection[j - inc] {
swap(collection, j, j - inc)
}
}
}
}
Best Case | Average Case | Worst Case | |
---|---|---|---|
Selection sort | \(O(n^2)\) | \(O(n^2)\) | \(O(n^2)\) |
Insertion Sort | \(O(n)\) | \(O(n^2)\) | \(O(n^2)\) |
Shell Sort | \(O(n)\) | \(O(n^{1.5})\) | \(O(n^2)\) or \(O(n^{1.5})\) |
java.util
packageclass PokerCard implements Comparable<PokerCard>{
public final Suit suit;
public final Value value;
//constructor
public int compareTo(PokerCard other) {
return compare(this.value, other.value);
}
//private compare method
}
public class PokerCardSuitComparator implements Comparator<PokerCard> {
@Override
public int compare(PokerCard pc1, PokerCard pc2) {
//compare suits instead
}
}
compare
method to be implementedcompareTo
method in the Comparable
interfaceResult | Meaning |
---|---|
Negative Number | \(pc1 < pc2\) |
Zero | \(pc1 = pc2\) |
Positive Number | \(pc1 > pc2\) |
public static <T> void sort(T arry[], Comparator<T> comparator) {
//sort algorithm
//will perform comparator.compare(obj1, obj2) to organize data
}
public static void main(String args[]) {
PokerCardSuitComparator pcsc = new PokerCardSuitComparator();
PokerCard deck[] = new PokerCard[];
//create random deck
sort(deck, pcsc);
}
Note
Again, we will deal with generics later. The <T> and T syntax above
CSC 385 - Data Structures and Algorithms