import java.util.*;
public class MidtermNina
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
Scanner call = new Scanner(System.in);
System.out.println("INPUT 5 RANDOM INTEGERS: ");
int[] arr = new int[5];
for(int i=0; i<arr.length; i++)
{
arr[i] = console.nextInt();
}
System.out.println("SELECT SORTING METHOD: 1. BUBBLE SORT, 2.SELECTION SORT, 3.INSERTION SORT");
int sortChoice = call.nextInt();
switch(sortChoice)
{
case 1:
bubbleSorts(arr);
break;
case 2:
selectionSorts(arr);
break;
case 3:
insertionSorts(arr);
break;
default:
System.out.print("NUMBER IS NOT IN THE CHOICES! ");
break;
}
}
public static void printArray(int[] arr)
{
for(int z=0; z<arr.length;z++)
{
System.out.print(" ");
System.out.print(arr[z]);
}
System.out.println();
}
public static void bubbleSorts(int[] arr)
{
int count=0;
int count1=0;
int count2=0;
for(int i=1; i<arr.length-1; i++)
{
count++;
for(int j=0;j<arr.length-1;j++)
{
count1++;
if(arr[j] > arr[j+1])
{
count2++;
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
System.out.print("BUBBLE SORTING"+i+":");
printArray(arr);
}
System.out.println("");
System.out.println("Number of Reset:"+count);
System.out.println("Comparison: "+count1);
System.out.println("Swap: "+count2);
System.out.print("Bubble Sorted Integers: ");
for(int i=0; i<arr.length; i++)
{
System.out.print(" "+arr[i]);
}
System.out.println();
}
public static void selectionSorts(int[] arr)
{
int reset =0;
int counts =0;
int swap =0;
for(int i=0; i<arr.length-1;i++)
{
reset--;
int index=i;
for(int j=i+1;j<5;j++)
{
counts++;
if(arr[j] < arr[index])
{
swap++;
index = j;
}
}
int temp = arr[index];
arr[index] = arr[i];
arr[i] = temp;
System.out.print("Doing Sorting Pass: "+i+": ");
printArray(arr);
}
System.out.println();
System.out.println("Number of Reset: "+reset);
System.out.println("Number Comparison: "+counts);
System.out.println("Number Swap: "+swap);
System.out.print("Selection Sorting: ");
for(int i=0;i<arr.length; i++)
{
System.out.print(" "+arr[i]);
}
System.out.println();
}
public static void insertionSorts(int[] arr)
{
int reset=0;
int count=0;
int swap=0;
for(int i=1;i<arr.length;i++)
{
reset--;
int temp = arr[i];
int j= i-1;
while(j>=0 && arr[j] > temp)
{
count++;
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = temp;
swap++;
System.out.print("INSERTION SORTING: "+i+": ");
printArray(arr);
}
System.out.println();
System.out.println("Number of Reset: "+reset);
System.out.println("Comparison : "+count);
System.out.println("Swap: "+swap);
System.out.print("Insertion Sorting: ");
for(int i = 0; i<arr.length; i++)
{
System.out.print(" "+ arr[i]);
}
System.out.println();
}
}