Question

implement void Quick_Sort(int A[],int l,int r) to sort array of integers in ascending order using the...

implement void Quick_Sort(int A[],int l,int r)

to sort array of integers in ascending order using the first element as a pivot, you can add any parameter you want

Note : you are only allowed to use java language

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

public class Main
{
//method for partition
public static int partition(int a[], int l, int h)
{
int pvt = a[l];
int i = l - 1;
int temp;
  
for (int j = l; j <= h - 1; j++)
{
if (a[j] < pvt)
{
i++;
  
//swapping
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[i + 1];
a[i + 1] = a[h];
a[h] = temp;
return i + 1;
}
  
//method for quick sort
public static void Quick_Sort(int A[], int l, int r)
{
if (l < r)
{
  
int p = partition(A, l, r);
Quick_Sort(A, l, p - 1);
Quick_Sort(A, p + 1, r);
}
}
   public static void main(String[] args)
   {
   //array declaration and initialization
int a[] = {20, 55, 11, 64, 97, 99, 74, 85, 34, 35, 74, 11, 47, 65, 69};
  
//method calling
Quick_Sort(a, 0, 14);
  
System.out.println("The array after sorting is: ");
for(int i=0; i<=14; i++)
{
System.out.print(a[i] + " ");
}
      
   }
}

OUTPUT:

The array after sorting is:
11 11 69 47 34 35 55 20 85 65 74 74 64 97 99

Add a comment
Know the answer?
Add Answer to:
implement void Quick_Sort(int A[],int l,int r) to sort array of integers in ascending order using the...
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
  • Write a JAVA program to sort a given array of integers (1 Dimensional) in ascending order...

    Write a JAVA program to sort a given array of integers (1 Dimensional) in ascending order (from smallest to largest). You can either get the array as input or hardcode it inside your program.

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • You want to sort (in increasing order) the following array of integers using quicksort as we...

    You want to sort (in increasing order) the following array of integers using quicksort as we have described it and used it in class. You are asked to specifically show your steps and the resulting array after one pass of quicksort. Show and explain each of your steps. Note 1: in case you are not using the algorithm presented and traced in class, you are expected to show all your steps accompanied with algorithm instructions and variables' values. Note 2:...

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as...

    Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as described in the last section of Chapter 7 in your text. It should handle variable amounts of data and variable numbers of digits in the key values. Use testing to ensure radix sort works for at least three examples of different input sizes and various max digit length. I need to implement the radix sort using the below java interface. /** * <h1><LeastSignificantDigit Radix...

  • Consider the Java program below to sort an array A in an ascending order. M, N,...

    Consider the Java program below to sort an array A in an ascending order. M, N, and K are positive integers, and A is an array of N nonnegative integers where 0 < A[i] < M for all i e {0,..., N -1}. In this program, list is a class of an integer list with the following methods. 1st.size(): returns the number of elements in the list lst 1st.get(i): returns the element at the i-th position in the list lst...

  • Language C++ (Please include a short description & Screenshot of output) Implement a Priority...

    Language C++ (Please include a short description & Screenshot of output) Implement a Priority queue using a SORTED list. Use Quick sort after adding a new node. Example of quick sort below. Adopt to your program the code below. #include <iostream> void quickSort(int a[ ], int first, int last); int pivot(int a[], int first, int last); void swap(int& a, int& b); void swapNoTemp(int& a, int& b); void print(int array[], const int& N); using namespace std; int main() { int test[]...

  • (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int...

    (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search function...

  • Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a....

    Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a. Sort the array using pivot as the middle element of the array. b. Sort the array using pivot as the median of the first, last, and middle elements of the array. c. Sort the array using pivot as the middle element of the array. However, when the size of any sublist reduces to less than 20, sort thesublis t using an insertion sort. d....

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
ADVERTISEMENT