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

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.
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
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.
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the insertion algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here          ...
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the bubble sort algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here       ...
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.
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]...
1, 1, 1, 1, 1, 3, 3, 3, 3, 6, 6, 6, 7, 7, 7, 12,...
1, 1, 1, 1, 1, 3, 3, 3, 3, 6, 6, 6, 7, 7, 7, 12, 12, 13, 17 1) Use three arguments to explain the character of the shape of the distribution. 2) Determine the presence of outliers in either bound of the distribution by the Fences Criterion. 3) Determine the presence of outliers in either bound using the Z-scores criterion. 4) Summarize data in general.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT