I really really need help with this project. Can you please provide the CODE and
ID: 3830385 • Letter: I
Question
I really really need help with this project. Can you please provide the CODE and OUTPUT RESULTS and a basic flow chart, if possible, for the following program in JAVA LANGUAGE? Thank you.
Can you please provide the Code in JAVA LANGUAGE and OUTPUT, please
CS 334 Algorithms FINAL PROJECT (due 5/4) write an interactive program that implements a Quicksort, a Heapsort, Bubble sort, Selection sort, Insertion sort and a Mergesort. The user is prompted by your program to enter a list of 25 integers. Your program then asks the user for the sort type he/she prefers. The user is given the 6 choices. After the choice has been made your program sorts the data by all six sorting techniques, regardless of the user's preference. Your program then outputs the list using the user's choice with corresponding execution time (the time it took to do the sort) and also outputs the times taken for sorting the list by the other five selections recommending the fastest sorting technique (by times) for his/her input list. DELIVERABLES: "hard copy output of (a) program flowchart, (b) Java source program and (c) execution results. No email is accepted!Explanation / Answer
// File1
package SortingAlgo;
class BubbleSort {
public void sort(Integer[] array)
{
int length = array.length;
int c, d, swap;
for (c = 0; c < ( length - 1 ); c++)
{
for (d = 0; d < length - c - 1; d++)
{
if (array[d] > array[d+1])
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
}
}
// File2
package SortingAlgo;
public class InsertionSort{
public void sort(Integer array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
}
}
}
// File3
package SortingAlgo;
public class MergeSort
{
@SuppressWarnings("rawtypes")
public Comparable[] mergeSort(Comparable[] list)
{
//If list is empty; no need to do anything
if (list.length <= 1) {
return list;
}
//Split the array in half in two parts
Comparable[] first = new Comparable[list.length / 2];
Comparable[] second = new Comparable[list.length - first.length];
System.arraycopy(list, 0, first, 0, first.length);
System.arraycopy(list, first.length, second, 0, second.length);
//Sort each half recursively
mergeSort(first);
mergeSort(second);
//Merge both halves together, overwriting to original array
merge(first, second, list);
return list;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private void merge(Comparable[] first, Comparable[] second, Comparable[] result)
{
//Index Position in first array - starting with first element
int iFirst = 0;
//Index Position in second array - starting with first element
int iSecond = 0;
//Index Position in merged array - starting with first position
int iMerged = 0;
//Compare elements at iFirst and iSecond,
//and move smaller element at iMerged
while (iFirst < first.length && iSecond < second.length)
{
if (first[iFirst].compareTo(second[iSecond]) < 0)
{
result[iMerged] = first[iFirst];
iFirst++;
}
else
{
result[iMerged] = second[iSecond];
iSecond++;
}
iMerged++;
}
//copy remaining elements from both halves - each half will have already sorted elements
System.arraycopy(first, iFirst, result, iMerged, first.length - iFirst);
System.arraycopy(second, iSecond, result, iMerged, second.length - iSecond);
}
}
// File4
package SortingAlgo;
public class QuickSort {
private Integer[] array;
private int length;
public void sort(Integer[] inputArr) {
if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
length = inputArr.length;
quickSort(0, length - 1);
}
private void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
// calculate pivot number, I am taking pivot as middle index number
int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
// Divide into two arrays
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j);
//move index to next position on both sides
i++;
j--;
}
}
// call quickSort() method recursively
if (lowerIndex < j)
quickSort(lowerIndex, j);
if (i < higherIndex)
quickSort(i, higherIndex);
}
private void exchangeNumbers(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
// File5
package SortingAlgo;
public class SelectionSort
{
public void sort(Integer[] arr)
{
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++){
if (arr[j] < arr[index]){
index = j;
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
}
// File6
package SortingAlgo;
public class HeapSort
{
private static Integer[] a;
private static int n;
private static int left;
private static int right;
private static int largest;
public void buildheap(Integer []a) {
n = a.length-1;
for(int i=n/2; i>=0; i--){
maxheap(a,i);
}
}
public void maxheap(Integer[] a, int i) {
left = 2*i;
right = 2*i+1;
if(left <= n && a[left] > a[i]){
largest=left;
} else {
largest=i;
}
if(right <= n && a[right] > a[largest]) {
largest=right;
}
if(largest!=i) {
exchange(i, largest);
maxheap(a, largest);
}
}
public void exchange(int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
public void sort(Integer[] myarray) {
a = myarray;
buildheap(a);
for(int i=n; i>0; i--) {
exchange(0, i);
n=n-1;
maxheap(a, 0);
}
}
}
// File7
package SortingAlgo;
import java.util.Arrays;
import java.util.Scanner;
public class TestSorting {
public static void main(String[] args)
{
System.out.println("Welcome to my SORTFUN application:");
System.out.println("Please enter 25 integers seperated by a space: ");
//Unsorted array
long startTime = 0, endTime = 0, qsTime = 0, hsTime = 0, msTime = 0, bsTime = 0, isTime = 0, ssTime = 0;
char choice;
Integer arr[] = new Integer[25];
int i = 0;
Scanner in = new Scanner(System.in);
while(i<25)
{
arr[i] = in.nextInt();
i++;
}
System.out.println(Arrays.toString(arr));
System.out.println("Which sorting technique do you want to use?");
System.out.println("Type Q for QuickSort, H for HeapSort, M for MergeSort, B for BubbleSort");
System.out.println("I for InsertionSort, S for SelectionSort or E for Exit");
choice = in.next().charAt(0);
System.out.println("Selection: "+choice);
if (choice=='Q'){
QuickSort quick = new QuickSort();
startTime = System.currentTimeMillis();
quick.sort(arr);
endTime = System.currentTimeMillis();
qsTime = endTime-startTime;
}
if (choice=='H'){
HeapSort heap = new HeapSort();
startTime = System.currentTimeMillis();
heap.sort(arr);
endTime = System.currentTimeMillis();
hsTime = endTime-startTime;
}
if (choice=='M'){
MergeSort merg = new MergeSort();
startTime = System.currentTimeMillis();
merg.mergeSort(arr);
endTime = System.currentTimeMillis();
msTime = endTime-startTime;
}
if (choice=='B'){
BubbleSort bubb = new BubbleSort();
startTime = System.currentTimeMillis();
bubb.sort(arr);
endTime = System.currentTimeMillis();
bsTime = endTime-startTime;
}
if (choice=='I'){
InsertionSort inser = new InsertionSort();
startTime = System.currentTimeMillis();
inser.sort(arr);
endTime = System.currentTimeMillis();
isTime = endTime-startTime;
}
if (choice=='S'){
SelectionSort sel = new SelectionSort();
startTime = System.currentTimeMillis();
sel.sort(arr);
endTime = System.currentTimeMillis();
ssTime = endTime-startTime;
}
if (choice=='E'){
System.out.println("Function is Exiting");
System.exit(0);
}
//Check the output which is sorted array
System.out.println("Sorted list: "+Arrays.toString(arr));
System.out.println("Merge Sort Time "+msTime);
System.out.println("Quick Sort Time "+qsTime);
System.out.println("Bubble Sort Time "+bsTime);
System.out.println("Insetion Sort Time "+isTime);
System.out.println("Selection Sort Time "+ssTime);
System.out.println();
in.close();
}
}