// Java
// Insertion Sort
// Sort.java
// By Rami Jaloudi, Programmer
import java.util.*;
public class Sort {
public static void main (String [] args){
int n = 0;
boolean run = true;
while(run){
Scanner in = new Scanner(System.in);
Scanner options = new Scanner(System.in);
System.out.println("Which option would you like to select?");
System.out.println(
"\n" +
"(1)Insertion Sort - Uses Random Integers based on your input of iterations 'n' "
+ "\n" +
"(2)Insertion Sort - Best Case Scenario; (Based on your input for how many integers we will populate, it will have an array that is in Asecending Order) or "
+ "\n" +
"(3)Insertion Sort - Worst Case Scenario; (Based on your input for how many integers we will populate, it will have an array that is in Descending Order)"
+ "\n" + "\n" +
"----------------------------------------------------------------------------------------------------------"
+ "\n"
+ "PLEASE NOTE THAT THIS PROGRAM RUNS REPITISOUSLY. YOU MAY HAVE TO SCROLL UP TO SEE YOUR PREVIOUS RESULTS."
+ "\n" +
"----------------------------------------------------------------------------------------------------------"
);
int optNumber;
optNumber = options.nextInt();
if(optNumber == 1){ //Insertion Sort
System.out.println("Enter the number of integers you would like to populate: [PROGRAM RE-RUNS AUTOMATICALLY]");
n = in.nextInt();
int A[] = new int[n];
populateArray(A);
System.out.println("-----------------------------------------");
System.out.println("UNSORTED ARRAY ");
printArray(A);
// sort the array
//insertionSort(A);
//System.out.println("\nAfter Sorting: ");
insertionSort(A);
System.out.println("-----------------------------------------");
printArray(A);
System.out.println("\n");
} else {
}
if(optNumber == 2){ // Insertion Sort - Best Case Scenario
System.out.println("Enter the number of integers you would like to populate: [PROGRAM RE-RUNS AUTOMATICALLY]");
n = in.nextInt();
int A[] = new int[n];
populateArray2(A);
System.out.println("Before Sorting: ");
System.out.println("-----------------------------------------");
printArray(A);
// sort the array
//insertionSort(A);
System.out.println("\nAfter Sorting: ");
insertionSort(A);
printArray(A);
System.out.println("-----------------------------------------");
System.out.println("\n");
} else {
}
if(optNumber == 3){ // Insertion Sort - Worst Case Scenario
System.out.println("Enter the number of integers you would like to populate: [PROGRAM RE-RUNS AUTOMATICALLY]");
n = in.nextInt();
int A[] = new int[n];
populateArray3(A);
System.out.println("Before Sorting: ");
System.out.println("-----------------------------------------");
printArray(A);
// sort the array
//insertionSort(A);
System.out.println("\nAfter Sorting: ");
insertionSort(A);
printArray(A);
System.out.println("-----------------------------------------");
System.out.println("\n");
} else {
}
}
}
// Insertion sort:
public static void insertionSort(int[] list) {
int comps = 0, COMPCOUNT = 0; int steps=0;
for(int i = 1; i < list.length; i++) { //3n = 2
int j = i; // 1
comps++;
steps++;
// comparing i with the sorted elements and then inserting it
// for e.g., sorted elements, [0..i-1]
while (j > 0 && list[j] < list[j - 1]) {
int temp = list[j];
steps++; // Counting Actual Steps (Not COMPCOUNT)
list[j] = list[j - 1]; //1
steps++; // Counting Actual Steps (Not COMPCOUNT)
list[j - 1] = temp; //1
steps++; // Counting Actual Steps (Not COMPCOUNT)
COMPCOUNT++;
j--;
steps++; // Counting Actual Steps (Not COMPCOUNT)
}
steps++; // Counting Actual Steps (Not COMPCOUNT)
//comps++; // counting when the condition is false
//COMPCOUNT++;
}
int n2 = comps * comps;
int n3 = (n2 - comps)/4;
int dev = (COMPCOUNT - n3);
int stepDev = steps - n2;
System.out.println(
"INSERTION SORT METHOD RESULTS: " + "\n"
+ "Iterations('n'):" + "\t" + "\t" + comps + "\n"
+ "-----------------------------------------" + "\n"
+ "(n^2):" + "\t" + "\t" + "\t" + "\t" + n2 + "\n"
+ "-----------------------------------------" + "\n"
+ "(n2 - n)/4 " + "\n" + "Key Comps Formula:"
+ "\t" + "\t" + n3 + "\n"
+ "-----------------------------------------" + "\n"
+ "COMPCOUNT:" + "\t" + "\t" + "\t" + COMPCOUNT + "\n"
+ "-----------------------------------------" + "\n"
+ "Difference between" + "\n"
+ "COMPCOUNT & (n2 - comps)/4:" + "\t" + dev + "\n"
+ "-----------------------------------------" + "\n"
+ "Actual Steps:" + "\t" + "\t" + "\t" + steps + "\n"
+ "-----------------------------------------" + "\n"
+ "Difference between" + "\n"
+ "Actual Steps & n^2" + "\n" + "from n^2:" + "\t" + "\t" + "\t" + stepDev + "\n"
+ "-----------------------------------------" + "\n");
}
public static void printArray(int[] B) {
System.out.println(Arrays.toString(B));
System.out.println("-----------------------------------------");
}
public static void populateArray(int[] B) {
for (int i = 0; i < B.length; i++) {
B[i] = (int) (Math.random() * 10000);
}
}
public static void populateArray2(int[] B) { // For Insertion Sort - Best Case Scenario
for (int i = 0; i < B.length; i++) {
B[i] = i + 1;
}
}
public static void populateArray3(int[] B) { // For Insertion Sort - Worst Case Scenario
for (int i = 0; i<B.length; i++) {
B[i] = B.length - i;
}
}
}
No comments:
Post a Comment