Question

In: Computer Science

Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7...

Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7

In java please.

Solutions

Expert Solution

CODE:

OUTPUT:

RAW CODE:

import java.util.*; // for Arrays.toString(array)
class quick_sort {

    void sort(int array[], int low, int high)
   {
       if (low < high)
       {
           int pi = partition(array, low, high); // correct place
           sort(array, low, pi-1); // sorting
           sort(array, pi+1, high); // sorting
       }
   }

   int partition(int array[], int low, int high)
   {
       int i = low-1 , j ,temp;
       int pivot = array[high]; // takes last element as pivot
       for (j=low; j<high; j++)
       {
           if (pivot > array[j] ) // if the j'th value is smaller then the pivot element then increment i and swap i'th and j'th elements
           {
               ++i;
               temp = array[j];
               array[j] = array[i];
               array[i] = temp;
           }
       }
       // at last swap i+1'th position value and high position value
       temp = array[high];
       array[high] = array[i+1];
       array[i+1] = temp;

       return i+1;
   }
  
}
class merge_sort {
   void merge(int array[], int left, int middle, int right)
   {
       // creating two new arrays : left_array[l : m] and right_array[m+1 : r]
       int size1 = middle - left + 1;
       int []left_array = new int[size1];

       int size2 = right - middle;
       int []right_array = new int[size2];

       int i,j;
       // assiging values from array to left_array and right_array
       for (i = 0; i < size1; ++i)
           left_array[i] = array[left + i];
       for (j = 0; j < size2; ++j)
           right_array[j] = array[middle + 1 + j];

       // merging two arrays from index 0
       i = 0;
       j = 0;
       int k = left;
       while (i < size1 && j < size2) {
           if (left_array[i] <= right_array[j]) { // checking if the left_array[i] is less than or equal to right_array[j] , if it is
               array[k] = left_array[i]; // then assign left_array[i] value to array[k]
               i++; // increase 'i' value means go to next element of left_array
           }
           else {
               array[k] = right_array[j]; // else assign right_array[i] value to array[k]
               j++; // increase 'j' value means go to next element of right_array
           }
           k++; // increase 'k' value means go to next element of array
       }

       // if the left_array is completed and right_array is remaining then add right_array to array at last
       while (j < size2) {
           array[k] = right_array[j];
           j++;// go to next element of right_array
           k++;
       }

       // if the right_array is completed and left_array is remaining then add left_array to array at last
       while (i < size1) {
           array[k] = left_array[i];
           i++; // go to next element of left_array
           k++; // go to next element of array
       }

      
   }
   // sort method
   void sort(int array[], int left, int right)
   {
       if (left < right) {
           int middle = (left + right) / 2; // middle
           sort(array, left, middle); // sorting first half
           sort(array, middle + 1, right); // sorting second half
           merge(array, left, middle, right); // merging two halfs
       }
   }
}
public class Main{
   public static void main(String args[])
   {
       int array1[] = {6, 9, 8, 12, 3, 1,7}; // given array
       int array2[] = {6, 9, 8, 12, 3, 1,7}; // given array
       System.out.println("Array Before Sorted : ");
       System.out.println(Arrays.toString(array1));

       quick_sort quick = new quick_sort(); // creating new quick_sort instance
       quick.sort(array1, 0, array1.length - 1 ); // calling sort method
       System.out.println("Quick Sort : ");
       System.out.println(Arrays.toString(array1)); // printing array after sorted

       merge_sort merge = new merge_sort(); // creating new merge_sort instance
       merge.sort(array2, 0, array2.length - 1); // calling sort method
       System.out.println("Merge Sort : ");
       System.out.println(Arrays.toString(array2)); // printing array after sorted
   }
}

NOTE:
If You have any doubts feel free to comment in comment section.
DO VOTE(LIKE).


Related Solutions

The quick sort algorithm always divides the list into two equal sized sublists, then sorts each...
The quick sort algorithm always divides the list into two equal sized sublists, then sorts each sublists, and then combines both sublists.. True of False
The multi-merge sort algorithm (M&M sort) works like merge sort, except that instead of dividing the...
The multi-merge sort algorithm (M&M sort) works like merge sort, except that instead of dividing the array into 2 partitions, it divides the array into p partitions. It recursively sorts each of the p partitions and then merges the results doing a p-way merge (except, of course, for the base case of a singleton or empty array). (a) Write the recurrence for T(n) for M&M sort for the case where p is a constant. Then, give a closed-form solution to...
3 6 4 8 1 10 2 9 11 12 15 22 3 6 7 5...
3 6 4 8 1 10 2 9 11 12 15 22 3 6 7 5 8 1 12 14 Each column represents a different treatment given to sick rats. Each cell is a different rat. Use statistical analysis and use post hoc testing using contrasts to find the best treatment. Treatment 1: vitamins Treatment 2: prescription pills Treatment 3: brain surgery Treatment 4: shock therapy Treatment 5: dietary changes
x 2 8 5 9 4 3 9 6 7 8 y 3 6 5 7...
x 2 8 5 9 4 3 9 6 7 8 y 3 6 5 7 9 7 4 6 9 9 -5.48x + 0.17 5.48x + 0.17 -0.17x + 5.48 0.17x + 5.48
A=[ 7 8 -2 -6 7 4 1 ; 2 4 -4 -13 9 9 -12...
A=[ 7 8 -2 -6 7 4 1 ; 2 4 -4 -13 9 9 -12 ; 6 6 0 -9 8 9 -4 ; 1 8 -14 -22 5 8 -1 ; 4 9 -10 -14 7 4 -1] B=[ 19 4 4 14 -3 -7 -5 ; 21 -6 -5 10 14 -2 4 ; 22 -4 5 13 5 -6 4 ; 41 20 0 26 11 -1 -27 ; 29 14 -2 20 3 -4 -19]...
Prove by argument that heapsort and merge sort are asymptotically optimal comparison sorts.
Prove by argument that heapsort and merge sort are asymptotically optimal comparison sorts.
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 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.
implement merge sort,quick sort, and radix sort algorithms in java and test how long it will...
implement merge sort,quick sort, and radix sort algorithms in java and test how long it will take to sort with random data sets of users input numbers.
Solve the following Double Linked List sort: use int. array = {8, 3, 12, 9, 6,...
Solve the following Double Linked List sort: use int. array = {8, 3, 12, 9, 6, 2} and provide visualization development to the solution.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT