Question

The following code is a Java code for Quick-sort. I would like this code to be...

The following code is a Java code for Quick-sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear).

//Java Program to Implement Quick Sort

import java.util.Scanner;

/** Class QuickSort **/
public class QuickSort
{
/** Quick Sort function **/
public static void sort(int[] arr)
{
quickSort(arr, 0, arr.length - 1);
}
/** Quick sort function **/
public static void quickSort(int arr[], int low, int high)
{
int i = low, j = high;
int temp;
int pivot = arr[(low + high) / 2];

/** partition **/
while (i <= j)
{
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j)
{
/** swap **/
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

i++;
j--;
}
}

/** recursively sort lower half **/
if (low < j)
quickSort(arr, low, j);
/** recursively sort upper half **/
if (i < high)
quickSort(arr, i, high);
}
/** Main method **/
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
int n, i;
/** Accept number of elements **/
System.out.println("Enter number of integer elements");
n = scan.nextInt();
/** Create array of n elements **/
int arr[] = new int[ n ];
/** Accept elements **/
System.out.println("\nEnter "+ n +" integer elements");
for (i = 0; i < n; i++)
arr[i] = scan.nextInt();
/** Call method sort **/
sort(arr);
/** Print sorted Array **/
System.out.println("\nElements after sorting ");
for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}

PLEASE make sure the code runs properly and no errors appear!

0 0
Add a comment Improve this question Transcribed image text
Answer #1


//Java Program to Implement Quick Sort

import java.util.Scanner;

/** Class QuickSort **/
public class QuickSort {
   /** Quick Sort function **/
   public static void sort(int[] arr) {
       quickSort(arr, 0, arr.length - 1);
   }

   /** Quick sort function **/
   public static void quickSort(int arr[], int low, int high) {
       int i = low, j = high;
       int temp;
       int pivot = arr[(low + high) / 2];

       /** partition **/
       while (i <= j) {
           while (arr[i] < pivot)
               i++;
           while (arr[j] > pivot)
               j--;
           if (i <= j) {
               /** swap **/
               temp = arr[i];
               arr[i] = arr[j];
               arr[j] = temp;

               i++;
               j--;
           }
       }

       /** recursively sort lower half **/
       if (low < j)
           quickSort(arr, low, j);
       /** recursively sort upper half **/
       if (i < high)
           quickSort(arr, i, high);
   }

   /** Main method **/
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       int n, i;
       /** Accept number of elements **/
       System.out.println("Enter number of integer elements");
       n = scan.nextInt();
       /** Create array of n elements **/
       if (n > 10) {
           System.err.println("Can't accept more than 10 numbers : ");
           return;
       }
       int arr[] = new int[n];
       /** Accept elements **/
       System.out.println("\nEnter " + n + " integer elements");
       for (i = 0; i < n; i++)
           arr[i] = scan.nextInt();
       /** Call method sort **/
       sort(arr);
       /** Print sorted Array **/
       System.out.println("\nElements after sorting ");
       for (i = 0; i < n; i++)
           System.out.print(arr[i] + " ");
       System.out.println();
       System.out.println("\nRange "+ arr[arr.length - 1] +"-"+arr[0]+" = " +(arr[arr.length - 1] - arr[0]));
   }
}

Add a comment
Know the answer?
Add Answer to:
The following code is a Java code for Quick-sort. I would like this code to be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT