Question

In: Computer Science

Write a program in Java to sort the given array using merge sort, quick sort, insertion...

Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.

Solutions

Expert Solution

Hi,

Please find the below code according to the problem statement.

-----------------------------------------------------------------------------------------

ArraySort.java

-----------------------------------------------------------------------------------------

import java.util.Arrays;
import java.util.Scanner;

public class ArraySort {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);
       System.out.print("Please enter the array size : ");
       int size = sc.nextInt();
       System.out.println("\nPlease enter " + size + " elements : ");
       int[] arr = new int[size];
       for (int i = 0; i < size; i++)
           arr[i] = sc.nextInt();
       boolean flag = false;
       while (!flag) {
           System.out.println("Please choose one of the Below sorting techniques :");
           System.out.println("A. Merge Sort\n" + "B. Quick Sort\n" + "C. Insertion Sort\n" + "D. Selection Sort\n"
                   + "E. Bubble Sort");
           char input = sc.next().toLowerCase().charAt(0);
           switch (input) {
           case 'a':
               mergeSort(arr, 0, arr.length - 1);
               System.out.println("Merge Sort is used, Elements of array after sort are : " + Arrays.toString(arr));
               flag = true;
               break;
           case 'b':
               quickSort(arr, 0, arr.length - 1);
               System.out.println("Quick Sort is used, Elements of array after sort are : " + Arrays.toString(arr));
               flag = true;
               break;
           case 'c':
               insertionSort(arr);
               System.out
                       .println("Insertion Sort is used, Elements of array after sort are : " + Arrays.toString(arr));
               flag = true;
               break;
           case 'd':
               selectionSort(arr);
               System.out
                       .println("Selection Sort is used, Elements of array after sort are : " + Arrays.toString(arr));
               flag = true;
               break;
           case 'e':
               bubbleSort(arr);
               System.out.println("Bubble Sort is used, Elements of array after sort are : " + Arrays.toString(arr));
               flag = true;
               break;
           default:
               System.out.println("Invalid Input!! Please try again");
           }
       } sc.close();
   }

   public static void mergeSort(int[] array, int left, int right) {
       if (right <= left)
           return;
       int mid = (left + right) / 2;
       mergeSort(array, left, mid);
       mergeSort(array, mid + 1, right);
       merge(array, left, mid, right);
   }

   static void merge(int[] array, int left, int mid, int right) {
       // calculating lengths
       int lengthLeft = mid - left + 1;
       int lengthRight = right - mid;

       // creating temporary subarrays
       int leftArray[] = new int[lengthLeft];
       int rightArray[] = new int[lengthRight];

       // copying our sorted subarrays into temporaries
       for (int i = 0; i < lengthLeft; i++)
           leftArray[i] = array[left + i];
       for (int i = 0; i < lengthRight; i++)
           rightArray[i] = array[mid + i + 1];

       // iterators containing current index of temp subarrays
       int leftIndex = 0;
       int rightIndex = 0;

       // copying from leftArray and rightArray back into array
       for (int i = left; i < right + 1; i++) {
           // if there are still uncopied elements in R and L, copy minimum of the two
           if (leftIndex < lengthLeft && rightIndex < lengthRight) {
               if (leftArray[leftIndex] < rightArray[rightIndex]) {
                   array[i] = leftArray[leftIndex];
                   leftIndex++;
               } else {
                   array[i] = rightArray[rightIndex];
                   rightIndex++;
               }
           }
           // if all the elements have been copied from rightArray, copy the rest of
           // leftArray
           else if (leftIndex < lengthLeft) {
               array[i] = leftArray[leftIndex];
               leftIndex++;
           }
           // if all the elements have been copied from leftArray, copy the rest of
           // rightArray
           else if (rightIndex < lengthRight) {
               array[i] = rightArray[rightIndex];
               rightIndex++;
           }
       }
   }

   public static void insertionSort(int[] array) {
       for (int i = 1; i < array.length; i++) {
           int current = array[i];
           int j = i - 1;
           while (j >= 0 && current < array[j]) {
               array[j + 1] = array[j];
               j--;
           }
           // at this point we've exited, so j is either -1
           // or it's at the first element where current >= a[j]
           array[j + 1] = current;
       }
   }

   public static void selectionSort(int[] array) {
       for (int i = 0; i < array.length; i++) {
           int min = array[i];
           int minId = i;
           for (int j = i + 1; j < array.length; j++) {
               if (array[j] < min) {
                   min = array[j];
                   minId = j;
               }
           }
           // swapping
           int temp = array[i];
           array[i] = min;
           array[minId] = temp;
       }
   }

   public static void bubbleSort(int[] a) {
       boolean sorted = false;
       int temp;
       while (!sorted) {
           sorted = true;
           for (int i = 0; i < a.length - 1; i++) {
               if (a[i] > a[i + 1]) {
                   temp = a[i];
                   a[i] = a[i + 1];
                   a[i + 1] = temp;
                   sorted = false;
               }
           }
       }
   }

   static int partition(int[] array, int begin, int end) {
       int pivot = end;

       int counter = begin;
       for (int i = begin; i < end; i++) {
           if (array[i] < array[pivot]) {
               int temp = array[counter];
               array[counter] = array[i];
               array[i] = temp;
               counter++;
           }
       }
       int temp = array[pivot];
       array[pivot] = array[counter];
       array[counter] = temp;

       return counter;
   }

   public static void quickSort(int[] array, int begin, int end) {
       if (end <= begin)
           return;
       int pivot = partition(array, begin, end);
       quickSort(array, begin, pivot - 1);
       quickSort(array, pivot + 1, end);
   }
}

Sample output:

1.

2.

3.

4.

5.

I hope this helps you!!

Thanks.


Related Solutions

write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick sort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and space does your scheme entail?
Write a MIPS program that uses an implementation of quick sort to sort an array of...
Write a MIPS program that uses an implementation of quick sort to sort an array of numbers (Translate the following code into (Mars Assembly language). Quicksort Implementation - C int partition(int arr [] , int left , int right) { int i=left, j=right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr [ i ] < pivot) i ++; while (arr [ j ] > pivot) j −−; if (i <= j)...
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge...
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
1a .Write a program that perform insertion sort (ascending order) on an input array and print...
1a .Write a program that perform insertion sort (ascending order) on an input array and print the total number of comparisons that have been made. You can implement by using any programming languages such as C/C++. For example, in the case of array B = [ 30 , 10 , 20 , 40 ], there are 4 needed comparisons to perform insertion sort (30 vs 10, 20 vs 30, 20 vs 10, and 40 vs 30). 1b. Write a program...
Java program to implement the merge sort your own and test it to sort a list...
Java program to implement the merge sort your own and test it to sort a list of names based on the frequency.
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a...
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a data set (txt file) then do the sorting algorithm to measure how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718...
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in...
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in place. * * @param students array to be sorted, can be empty, but cannot be null */ public static void sortGPA(Student[] students) { // TODO: implement this } Student class: public class Student extends Person { private double GPA; public Student(String lastName, String firstName, double gpa) { super(lastName, firstName); this.GPA = gpa; } public double getGPA() { return GPA; } @Override public boolean equals(Object...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT