Question

In: Computer Science

Modify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order.

Programming Language : Java

Modify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order.

NOTE: Do not move the last element into the first element of the array. You must treat the algorithm as if the pivot is actually sitting in the last location of the array.

After it has been sorted in descending order, go through all the items in the array and make sure that they all have the same number of digits as the largest element in the array by adding additional 5’s to the end of the numbers. For example, given the following sorted array:

324, 46, 6

After adding the additional digits, the array will look like the following:

324, 465, 655

Then sort the new array using radix sort indescending order.

Read the original data elements from a file. The elements in the file will be separated by some kind of white space, just like before. The number of elements will not exceed 10.

Solutions

Expert Solution

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

public class Sort {

    private void quickSort(int array[], int start, int end) {
        if (start < end) {
            /* pi is partitioning index, array[pi] is
              now at right place */
            int pi = partition(array, start, end);

            // Recursively sort elements before
            // partition and after partition
            quickSort(array, start, pi - 1);
            quickSort(array, pi + 1, end);
        }
    }

    /* Taking last element as pivot, places the pivot
     element at its correct position in sorted array,
     and places all smaller elements to right and all
     larger elements to left */
    private int partition(int array[], int start, int end) {
        int pivot = array[end];
        int i = (start - 1); // index of larger element
        for (int j = start; j < end; j++)
            if (array[j] > pivot) {
                i++;
                // swap array[i] and array[j]
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }

        // swap array[i+1] and array[end] (or pivot)
        int temp = array[i + 1];
        array[i + 1] = array[end];
        array[end] = temp;
        return i + 1;
    }

    private void printArray(int array[]) {
        for (int item : array) System.out.print(item + " ");
        System.out.println();
    }

    private void equifyDigits(int[] array) {

        //Find out length of the greatest number
        int maxLength = String.valueOf(array[0]).length();

        for (int i = 1; i < array.length; i++) {

            //Get length of subsequent numbers
            int length = String.valueOf(array[i]).length();

            /* If length is less than that of the
            greatest number, keep appending 5 to the
            end until length becomes equal*/
            while (length < maxLength) {
                array[i] = (array[i] * 10) + 5;
                length++;
            }
        }
    }

    private void radixSort(int array[], int n) {
        // Find the maximum number to know number of digits
        int m = getMax(array, n);

        // Do counting sort for every digit. Note that instead
        // of passing digit number, exp is passed. exp is 10^i
        // where i is current digit number
        for (int exp = 1; m / exp > 0; exp *= 10)
            countSort(array, n, exp);
    }

    // A function to do counting sort of array[] according to
    // the digit represented by exp.
    private static void countSort(int array[], int n, int exp) {
        int output[] = new int[n]; // output array
        int i;
        int bucket[] = new int[10];
        Arrays.fill(bucket, 0);

        // Store count of occurrences in count[]
        for (i = 0; i < n; i++)
            bucket[9 - (array[i] / exp) % 10]++;

        // Change count[i] so that count[i] now contains
        // actual position of this digit in output[]
        for (i = 1; i < 10; i++)
            bucket[i] += bucket[i - 1];

        // Build the output array
        for (i = n - 1; i >= 0; i--) {
            output[bucket[9 - (array[i] / exp) % 10] - 1] = array[i];
            bucket[9 - (array[i] / exp) % 10]--;
        }

        // Copy the output array to array[], so that array[] now
        // contains sorted numbers according to curent digit
        for (i = 0; i < n; i++)
            array[i] = output[i];
    }

    private int getMax(int array[], int n) {
        int max = array[0];
        for (int i = 1; i < n; i++)
            if (array[i] > max)
                max = array[i];
        return max;
    }

    public static void main(String args[]) throws Exception {

        String data[] = new String(Files.readAllBytes(Paths.get("C:\\Path\\to\\file\\test.txt")))
                .split(" ");
        int n = data.length;
        int arr[] = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = Integer.parseInt(data[i]);
        }

        Sort ob = new Sort();

        ob.quickSort(arr, 0, n - 1);
System.out.print("After Quick Sorting: ");
ob.printArray(arr);
ob.equifyDigits(arr);
System.out.print("After Appending 5: ");
ob.printArray(arr);
ob.radixSort(arr, n);
System.out.print("After Radix Sorting: ");
        ob.printArray(arr);
    }
}

Related Solutions

in C++, Modify the quicksort algorithm such that it uses the last item as the pivot...
in C++, Modify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order. NOTE: Do not move the last element into the first element of the array. You must treat the algorithm as if the pivot is actually sitting in the last location of the array. After it has been sorted in descending order, go through all the items in the array and make...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT