Question

In: Computer Science

1) You must implement a recursive Quicksort algorithm that will read integers from the attached MyList.txt...

1) You must implement a recursive Quicksort algorithm that will read integers from the attached MyList.txt file. Your algorithm must sort the list(integers)in ascending order.

2)You must implement a recursive Mergesort algorithm that will read integers from the attached MyList.txt file. Your algorithm must sort the list(integers)in ascending order.

My List.txt Values

7

3

4

1

4

4

9

9

4

8

4

5

3

9

2

3

7

0

6

4

4

5

0

1

9

2

1

7

4

7

8

7

8

3

6

3

5

9

7

3

7

8

8

2

5

9

1

2

7

2

0

1

7

5

4

3

0

5

9

2

0

7

8

9

8

4

8

2

9

2

2

1

1

5

7

5

7

5

8

7

3

2

7

8

0

1

5

1

7

6

9

2

9

6

3

9

2

6

0

5

8

9

7

5

3

5

4

2

4

5

7

7

6

9

7

1

9

4

2

9

1

1

6

4

6

5

7

3

5

1

6

8

5

9

3

5

Solutions

Expert Solution

// QuickSort.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class QuickSort {

   // method to sort the input array of integers using quick sort in ascending order
   public static void quickSort(int[] array) {
       quickSort(array,0,array.length-1);
   }
  
   // recursive method to sort the array using quick sort
   private static void quickSort(int[] array, int begin, int end) {
       int pivotKey;
       // if begin index < end index
       if(begin < end)
       {
           pivotKey = partition(array,begin,end); // get the pivot index
           quickSort(array, begin, pivotKey-1); // call quick sort to sort the list from begin to pivotKey - 1
           quickSort(array, pivotKey+1, end); // call quick sort to sort the list from pivotKey+1 to end
       }
   }
  
   // Partition part of an array, and return the index where the pivot
   // ended up where pivot is the mid element of the array
   private static int partition(int[] array, int begin, int end) {
      
       int mid = (begin+end)/2; // get the index of mid element
       swap(array, begin, mid); // swap the begin and mid index elements
       int pivotkey = begin; // set pivotkey to begin
       int pivot = array[begin]; // get the element at pivot key
      
       // loop from begin+1 to end
       for(int i=begin+1;i<=end;i++)
       {
           // if ith element < pivot, swap elements at indices i and pivotkey+1 and increment pivotKey by 1
           if(array[i] < pivot)
           {
               swap(array, i, ++pivotkey);
              
           }
       }
      
       // after the loop, swap the elements at begin and pivotkey to place the pivot in its correct position
       swap(array, begin, pivotkey);
      
       return pivotkey; // return the pivotkey
   }
  
   // method to swap two elements in an array
   private static void swap(int[] array, int i, int j) {

       int x = array[i];
       array[i] = array[j];
       array[j] = x;
   }
      
   public static void main(String[] args) throws FileNotFoundException
   {
       String filename = "MyList.txt";
       File inFile = new File(filename);
       Scanner fileScan = new Scanner(inFile); // open input file
       int[] array = null;
       int num;
       // loop to populate the array with integers read from file
       while(fileScan.hasNext())
       {
           num = fileScan.nextInt();
           if(array == null) // array is not allocated
               array = new int[1]; // create an array of size 1
           else
           {   // expand the array by 1 element
               int temp[] = new int[array.length+1];
               for(int i=0;i<array.length;i++)
                   temp[i] = array[i];
               array = temp;
           }
             
           array[array.length-1] = num; // insert num at the end of array
       }
      
       fileScan.close(); // close the file
      
       // display the unsorted array with 10 elements in a line
       System.out.println("UNSORTED ARRAY: ");
       for(int i=0;i<array.length;i++)
       {
           System.out.printf("%5d",array[i]);
           if((i+1)%10 == 0)
               System.out.println();
       }
      
       System.out.println();
      
       quickSort(array); // sort the array using quick sort
      
       // display the sorted array with 10 elements in a line
       System.out.println("SORTED ARRAY: ");
       for(int i=0;i<array.length;i++)
       {
           System.out.printf("%5d",array[i]);
           if((i+1)%10 == 0)
               System.out.println();
       }
      
       System.out.println();
   }

}

//end of QuickSort.java

Output:

Input file: MyList.txt

Output:

// MergeSort.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class MergeSort {
  
   // method to sort the input array in ascending order using merge sort
   public static void mergeSort(int[] array) {

       mergeSort(array,0,array.length-1);
   }
  
   // recursive merge sort method to sort the array in ascending order
   private static void mergeSort(int[] array, int begin, int end) {

       if(begin < end) // array has atleast 2 elements
       {
           int mid = (begin+end)/2; // get the index of mid element
           mergeSort(array, begin, mid); // call mergesort to sort the left subarray from begin to mid
           mergeSort(array,mid+1, end); // call mergesort to sort the right subarray from mid+1 to end
           merge(array,begin,mid,end); // merge and left and right sorted subarray to create the sorted array
       }
   }
  
   // method that merges the two sorted subarrays to create the sorted array
   private static void merge(int[] array, int low, int mid, int high) {

       // set l to starting index of sorted left subarray and h to starting index of sorted right subarray
       int l = low, h = mid+1;
       int n = low; // set n to low
      
       // create a temporary array of same size as array
       int brr[] = new int[array.length];

       // loop that continues till we reach end of left or right subarray
       for(; l <= mid && h <= high;)
       {
           // current element of left suarray <= current element of right subarray
           if(array[l] <= array[h])
           {
               brr[n] = array[l]; // add the element from left subarray to brr
               l++; // increment the index for left subarray
           }else // current element of left subarray > current element of right subarray
           {
               brr[n] = array[h]; // add the element from right subarray to brr
               h++; // increment the index for right subarray
           }
           n++; // increment index of brr
       }
      
       // copy the leftover elements from left subarray to brr
       for(;l<=mid;l++)
           brr[n++] = array[l];
      
       // copy the leftover elements from right subarray to brr
       for(;h<=high;h++)
           brr[n++] = array[h];
      
       // loop to copy the sorted elements from brr to array
       for(n=low;n<=high;n++)
       {
           array[n] = brr[n];
       }
   }
  
   public static void main(String[] args) throws FileNotFoundException
   {
       String filename = "MyList.txt";
       File inFile = new File(filename);
       Scanner fileScan = new Scanner(inFile); // open input file
       int[] array = null;
       int num;

       // loop to populate the array with integers read from file
       while(fileScan.hasNext())
       {
           num = fileScan.nextInt();
           if(array == null) // array is not allocated
               array = new int[1]; // create an array of size 1
           else
           {
               // expand the array by 1 element  
               int temp[] = new int[array.length+1];
               for(int i=0;i<array.length;i++)
                   temp[i] = array[i];
               array = temp;
           }
          
           array[array.length-1] = num; // insert num at the end of array
       }
      
       fileScan.close();// close the file

       // display the unsorted array with 10 elements in a line
       System.out.println("UNSORTED ARRAY: ");
       for(int i=0;i<array.length;i++)
       {
           System.out.printf("%5d",array[i]);
           if((i+1)%10 == 0)
               System.out.println();
       }
      
       System.out.println();
      
       mergeSort(array); // sort the array using merge sort
      
// display the sorted array with 10 elements in a line
       System.out.println("SORTED ARRAY: ");
       for(int i=0;i<array.length;i++)
       {
           System.out.printf("%5d",array[i]);
           if((i+1)%10 == 0)
               System.out.println();
       }
      
       System.out.println();
   }

}

//end of MergeSort.java

Output:

Input file: MyList.txt

Output:


Related Solutions

The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers,...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers, lowIndex, highIndex) {    midpoint = lowIndex + (highIndex - lowIndex) / 2    pivot = numbers[midpoint]    done = false    while (!done) {       while (numbers[lowIndex] < pivot)          lowIndex++       while (pivot < numbers[highIndex])          highIndex--       if (lowIndex >= highIndex) {          done = true       }       else {          temp = numbers[lowIndex]          numbers[lowIndex] = numbers[highIndex]          numbers[highIndex] = temp                 lowIndex++          highIndex--       }    }    return highIndex } Quicksort(numbers, lowIndex, highIndex) {    if (lowIndex...
1a)Use the Lomuto's quicksort algorithm to sort the following list of integers in nondecreasing order for...
1a)Use the Lomuto's quicksort algorithm to sort the following list of integers in nondecreasing order for the first pivot item. Use pointers i and j for swapping items (if necessary) for each step leading to your answer. Note do the problem just for the first pivot item. 123,34,189,56,150,12,9,24 1b)Use the same list of numbers and use the Hoare's quicksort algorithm using pointers l and r and updating the pointers i and j and swapping items. Do the problem just for...
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Implement a recursive binary search on an array of 8-byte integers in LEGV8
1. Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your...
1. Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code. While you are coding, it is helpful to break up your code into sub-functions and test the sub-functions as you go along.
Implement and complement the recursive code to compute the product of two positive integers, x and...
Implement and complement the recursive code to compute the product of two positive integers, x and y, using only addition and/or subtraction. The function will take as its arguments two integers to multiply together ( x x y ) and will return the product. Hint: consider the following: 6 x 1 = 6 6 x 2 = 6 + (6 x 1) 6 x 3 = 6 + (6 x 2) = 6 + [6 + (6 x 1)] =...
2. Give a recursive algorithm to compute the product of two positive integers, m and n,...
2. Give a recursive algorithm to compute the product of two positive integers, m and n, using only addition and subtraction. Java Language...
1)Think of a better way to enhance Fibonacci recursive algorithm,,, report your finding. 2) Implement a...
1)Think of a better way to enhance Fibonacci recursive algorithm,,, report your finding. 2) Implement a recursive function to print an array from the middle and from left to right. 3) Trace tower of Hanoi recursive algorithm for 4 discs.
1. Read 20 integers into an array. Next, use the unique algorithm to reduce the array...
1. Read 20 integers into an array. Next, use the unique algorithm to reduce the array to the unique values entered by the user. Use the copy algorithm to display the unique values. 2. Modify the Exercise 1 above to use the unique_copy algorith. The unique values should be inserted into a vector that's initially empty. Use a back_inserter to enable the vector to grow as new items are added. Use the copy algorithm to display the unique values.
in code c++ describe a recursive algorithm for multiplying two nonnegative integers x and y based...
in code c++ describe a recursive algorithm for multiplying two nonnegative integers x and y based on the fact that xy = 2(x · (y/2)) when y is even and xy = 2(x · ⌊y/2⌋) + x when y is odd, together with the initial condition xy = 0 when y = 0.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT