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
* 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.
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
One way to improve quick sort is to use an insertion sort on lists that have...
One way to improve quick sort is to use an insertion sort on lists that have a small length (call it the “partition limit”). Why does this make sense?
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT