Reflection
The event was fun, my favorite show was the bubble sort/bachelorette show because their jokes were funny. Though I do firmly believe that my show was the best with great acting and a wonderful script. Because although the Bachelorette did have funnsy jokes, our show told a story: The heroing story of a detective solving a crime case to catch a killer wanted for the murder of an entire party. Regardless of my feelings, I still had a fun time watching other shows and learning the ins and outs of different sorting algorithms.
Sorting algorithms
import java.util.ArrayList;
import java.util.Arrays;
public class Sorts {
public static ArrayList<Integer> selectionSort(int[] data){
ArrayList<Integer> sorted = new ArrayList<Integer>();
for(int i = 0; i < data.length; i++){
int smallest = Integer.MAX_VALUE;
int smallestIndex = -1;
for(int j = i; j < data.length; j++){
if(data[j] < smallest){
smallest = data[j];
smallestIndex = j;
}
}
if (smallestIndex != -1) {
// Add the smallest element found to the sorted list
sorted.add(smallest);
// Swap elements to maintain sorting in the original array
int temp = data[i];
data[i] = data[smallestIndex];
data[smallestIndex] = temp;
}
}
return sorted;
}
public static int[] bubbleSort(int[] data){
// Create a copy of the original array to avoid modifying it
int[] sortedArray = Arrays.copyOf(data, data.length);
// Perform bubble sort
for (int i = 0; i < sortedArray.length - 1; i++) {
for (int j = 0; j < sortedArray.length - i - 1; j++) {
if (sortedArray[j] > sortedArray[j + 1]) {
// Swap elements if they are in the wrong order
int temp = sortedArray[j];
sortedArray[j] = sortedArray[j + 1];
sortedArray[j + 1] = temp;
}
}
}
return sortedArray;
}
public static int[] insertionSort(int[] data) {
// Create a copy of the original array to avoid modifying it
int[] sortedArray = Arrays.copyOf(data, data.length);
// Perform insertion sort
for (int i = 1; i < sortedArray.length; i++) {
int key = sortedArray[i];
int j = i - 1;
// Move elements of sortedArray[0..i-1] that are greater than key, to one position ahead of their current position
while (j >= 0 && sortedArray[j] > key) {
sortedArray[j + 1] = sortedArray[j];
j--;
}
sortedArray[j + 1] = key;
}
return sortedArray;
}
public static int[] mergeSort(int[] arr) {
if (arr.length <= 1) {
return arr;
}
int middle = arr.length / 2;
int[] left = Arrays.copyOfRange(arr, 0, middle);
int[] right = Arrays.copyOfRange(arr, middle, arr.length);
left = mergeSort(left);
right = mergeSort(right);
return merge(left, right);
}
private static int[] merge(int[] left, int[] right) {
int[] result = new int[left.length + right.length];
int leftPointer = 0, rightPointer = 0, resultPointer = 0;
while (leftPointer < left.length && rightPointer < right.length) {
if (left[leftPointer] < right[rightPointer]) {
result[resultPointer++] = left[leftPointer++];
} else {
result[resultPointer++] = right[rightPointer++];
}
}
while (leftPointer < left.length) {
result[resultPointer++] = left[leftPointer++];
}
while (rightPointer < right.length) {
result[resultPointer++] = right[rightPointer++];
}
return result;
}
public static int[] quickSort(int[] arr) {
if (arr.length <= 1) {
return arr;
}
int pivot = arr[arr.length - 1];
int[] left = new int[arr.length];
int leftIndex = 0;
int[] right = new int[arr.length];
int rightIndex = 0;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] < pivot) {
left[leftIndex++] = arr[i];
} else {
right[rightIndex++] = arr[i];
}
}
left = Arrays.copyOf(left, leftIndex);
right = Arrays.copyOf(right, rightIndex);
left = quickSort(left);
right = quickSort(right);
int[] sorted = new int[arr.length];
System.arraycopy(left, 0, sorted, 0, left.length);
sorted[left.length] = pivot;
System.arraycopy(right, 0, sorted, left.length + 1, right.length);
return sorted;
}
public static void main(String[] args) {
int[] data = {9,4,6,5,7,2,1,8,3};
System.out.println("Original Array: " + Arrays.toString(data));
System.out.println("Selection Sort: " + selectionSort(data));
System.out.println("Bubble Sort: " + Arrays.toString(bubbleSort(data)));
System.out.println("Selection Sort: " + Arrays.toString(insertionSort(data)));
System.out.println("Merge Sort: " + Arrays.toString(mergeSort(data)));
System.out.println("Quick Sort: " + Arrays.toString(quickSort(data)));
}
}
Sorts sort = new Sorts();
sort.main(null);
Original Array: [9, 4, 6, 5, 7, 2, 1, 8, 3]
Selection Sort: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Bubble Sort: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Selection Sort: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Merge Sort: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Quick Sort: [1, 2, 3, 4, 5, 6, 7, 8, 9]
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public abstract class Collectable implements Comparable<Collectable> {
public final String masterType = "Collectable";
private String type; // extender should define their data type
// enumerated interface
public interface KeyTypes {
String name();
}
protected abstract KeyTypes getKey(); // this method helps force usage of KeyTypes
// getter
public String getMasterType() {
return masterType;
}
// getter
public String getType() {
return type;
}
// setter
public void setType(String type) {
this.type = type;
}
// this method is used to establish key order
public abstract String toString();
// this method is used to compare toString of objects
public int compareTo(Collectable obj) {
return this.toString().compareTo(obj.toString());
}
// static print method used by extended classes
public static void print(Collectable[] objs) {
// print 'Object' properties
System.out.println(objs.getClass() + " " + objs.length);
// print 'Collectable' properties
if (objs.length > 0) {
Collectable obj = objs[0]; // Look at properties of 1st element
System.out.println(
obj.getMasterType() + ": " +
obj.getType() +
" listed by " +
obj.getKey());
}
// print "Collectable: Objects'
for (Object o : objs) // observe that type is Opaque
System.out.println(o);
System.out.println();
}
}
class SelectionSort {
public static <T extends Comparable<T>> void selectionSort(List<T> list) {
int n = list.size();
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (list.get(j).compareTo(list.get(minIndex)) < 0) {
minIndex = j;
}
}
if (minIndex != i) {
T temp = list.get(i);
list.set(i, list.get(minIndex));
list.set(minIndex, temp);
}
}
}
}
class Airplane extends Collectable {
// Class data
public static KeyTypes key = KeyType.name; // static initializer
public static void setOrder(KeyTypes key) { Airplane.key = key; }
public enum KeyType implements KeyTypes { name, manufacturer, capacity }
// Instance data
private final String manufacturer;
private final int capacity;
private final String name;
// Constructor
Airplane(String manufacturer, int capacity, String name) {
this.setType("Airplane");
this.manufacturer = manufacturer;
this.capacity = capacity;
this.name = name;
}
/* 'Collectable' requires getKey to help enforce KeyTypes usage */
@Override
protected KeyTypes getKey() {
return Airplane.key;
}
/* 'Collectable' requires toString override
* toString provides data based off of Static Key setting
*/
@Override
public String toString() {
String output = "";
if (KeyType.name.equals(this.getKey())) {
output += this.name;
} else if (KeyType.manufacturer.equals(this.getKey())) {
output += this.manufacturer;
} else if (KeyType.capacity.equals(this.getKey())) {
output += "00" + this.capacity;
output = output.substring(output.length() - 2);
} else {
output = super.getType() + ": " + this.name + ", " + this.manufacturer + ", " + this.capacity;
}
return output;
}
// Test data initializer
public static Airplane[] airplanes() {
return new Airplane[]{
new Airplane("Boeing", 200, "Boeing 747"),
new Airplane("Airbus", 300, "Airbus A380"),
new Airplane("Boeing", 150, "Boeing 737"),
new Airplane("Embraer", 100, "Embraer E190"),
new Airplane("Bombardier", 120, "Bombardier CRJ200"),
new Airplane("Airbus", 500, "Airbus A350"),
new Airplane("Boeing", 400, "Boeing 777"),
new Airplane("Embraer", 90, "Embraer E145"),
new Airplane("Airbus", 250, "Airbus A320"),
};
}
public static void main(String[] args) {
// Inheritance Hierarchy
Airplane[] objs = airplanes(); // Array is reference type only, no methods
List<Airplane> airplanes = new ArrayList<>(Arrays.asList(objs)); // conversion required to make it a Collection
// print with name
Airplane.setOrder(KeyType.name);
Airplane.print(objs);
// convert to Collection and sort in manufacturer order
Airplane.setOrder(KeyType.manufacturer);
Collections.sort(airplanes);
//SelectionSort.selectionSort(airplanes); // Using Selection Sort algorithm
Airplane.setOrder(KeyType.name);
for (Airplane airplane : airplanes)
System.out.println(airplane);
}
}
Airplane.main(null)
class [LREPL.$JShell$24$Airplane; 9
Collectable: Airplane listed by name
Boeing 747
Airbus A380
Boeing 737
Embraer E190
Bombardier CRJ200
Airbus A350
Boeing 777
Embraer E145
Airbus A320
Airbus A380
Airbus A350
Airbus A320
Boeing 747
Boeing 737
Boeing 777
Bombardier CRJ200
Embraer E190
Embraer E145