Sunday, March 20, 2016

Insertion Sort | Java


// 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;
 }
      }
}

Friday, March 18, 2016

Java - Insertion Sort v. Bubble Sort | Algorothims


// Java
// Insertion Sort
// Sort.java
// Java - Insertion Sort v. Bubble Sort
// 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);  
                                                                System.out.println("Enter the number of integers you would like to randomly populate:   [RUN AGAIN &amp; COMPARE]");
                                                                n = in.nextInt(); 
                                                                int A[] = new int[n];
                                                               
                                                                Scanner options = new Scanner(System.in);
                                                                System.out.println("Which option would you like to select?");
                                                                System.out.println("(1)Insertion Sort; "
                                                                                                + "\n"
                                                                                                + "(2)Bubble Sort; or "
                                                                                                + "\n" +
                                                                                                "(3)Bubble Sort 2nd Version");
                                                                int optNumber;
                                                                optNumber = options.nextInt();
                                                               
                                                                if(optNumber == 1){
                                                                                populateArray(A);
                                                                                System.out.println("Before Sorting: ");
                                                                                System.out.println("-----------------------------------------");
                                                                                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){
                                                                                populateArray(A);
                                                                                System.out.println("Before Sorting: ");
                                                                                System.out.println("-----------------------------------------");
                                                                                printArray(A);
                                                                               
                                                                                // sort the array
                                                                                //insertionSort(A);
                                                                                System.out.println("\nAfter Sorting: ");
                                                                                bubbleSort(A);
                                                                                printArray(A);
                                                                                System.out.println("-----------------------------------------");
                                                                               
                                                                                System.out.println("\n");
                                                                               
                                                                               
                                                                } else {
                                                                }
                                                               
                                                                if(optNumber == 3){
                                                                                populateArray(A);
                                                                                System.out.println("Before Sorting: ");
                                                                                System.out.println("-----------------------------------------");
                                                                                printArray(A);
                                                                               
                                                                                // sort the array
                                                                                //insertionSort(A);
                                                                                System.out.println("\nAfter Sorting: ");
                                                                                bubbleSort2(A);
                                                                                printArray(A);
                                                                                System.out.println("-----------------------------------------");
                                                                               
                                                                                System.out.println("\n");
                                                                               
                                                                               
                                                                } else {
                                                                }
                                                                }
                                               
                                               
                                }
               
  // Bubble sort:  
  public static void bubbleSort(int[] list) {
    int comps = 0;
    int COMPCOUNT = 0;
   
    for(int j = 0; j &lt; list.length - 1; j++) {
      for(int i = 0; i &lt; list.length - 1; i++) {
       
        // count the number of comparisons
        comps++;
       
        // compare neighboring elements
        if(list[i] &gt; list[i+1]) {
         
          // swap i with i+1
          int temp = list[i];
          list[i] = list[i + 1];
          list[i + 1] = temp;
         
          // count number of swaps
          COMPCOUNT++;
        }
      }
    }
    //System.out.println(list);
    System.out.println("BUBBLE SORT 1: " + "Comparisons: " + comps + " COMPCOUNT: " + COMPCOUNT);
  }
 
  //Variation of Bubble Sort (improved)
  //Stop sorting when the array has become sorted &amp; do not compare elements of the array with elements that are in their correct sorted position
 
  public static void bubbleSort2(int[] list) {
    int comps = 0;
    int COMPCOUNT = 0;
    boolean didSwap = true;
   
    // didSwap tracks whether any elements of the array
    // were swapped during the last iteration of the loop.
    // If no elements were swapped, the the array is
    // sorted, so we can stop.
    for(int j = 0; j &lt; list.length - 1 &amp;&amp; didSwap; j++) {
      didSwap = false;
     
      // at this point, j elements are already in their
      // correct sorted position, so we do not compare
      // with the last j elements in the list
      for(int i = 0; i &lt; list.length - 1 - j; i++) {
         
        // count the number of comparisons
        comps++;
       
        // compare neighboring elements
        if(list[i] &gt; list[i+1]) {
         
          didSwap = true;
         
          // swap i with i+1
          int temp = list[i];
          list[i] = list[i + 1];
          list[i + 1] = temp;
         
          // count number of swaps
          COMPCOUNT++;
        }
      }
    }
    //System.out.println(list);
    System.out.println("BUBBLE SORT 2: " + "Comparisons: " + comps + " COMPCOUNT: " + COMPCOUNT);
   
  }
 
  /**
   * Insertion sort:
   * - pick an element and put it into its correct
   * position in the sorted part of the list
   * - repeat list.length times
   */
  public static void insertionSort(int[] list) {
    int comps = 0, COMPCOUNT = 0;
   
    for(int i = 1; i &lt; list.length; i++) {
   
      int j = i;     
           
      // compare i with sorted elements and insert it
      // sorted elements: [0..i-1]
      while (j &gt; 0 &amp;&amp; list[j] &lt; list[j - 1]) {
       
        int temp = list[j];
        list[j] = list[j - 1];
        list[j - 1] = temp;
       
        COMPCOUNT++;
        comps++;  // loop condition true
        
        j--;
      }
      comps++; // checking loop condition when false
    }
    //System.out.println(list);
   
    System.out.println("INSERTION SORT: " + "Comparisons: " + comps + " COMPCOUNT: " + COMPCOUNT);
  }
 
 
  public static void printArray(int[] B) {
      System.out.println(Arrays.toString(B));
  }

  public static void populateArray(int[] B) {
      for (int i = 0; i &lt; B.length; i++) {
          B[i] = (int) (Math.random() * 100);
      }
  }
 
 
 
 
}