Question

In: Computer Science

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 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 in descending 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

Program

#include<iostream>
#include<fstream>
#include<math.h>

using namespace std;

//Function partition used to partition the array
int partition(int a[], int l,int r)
{
//set last element as pivot
   int t = a[r];
   int i = l-1;
   int j = r;

   while(1)
   {
   while(t>a[--j]);

       while(t<a[++i]);

       if(i>=j) break;

       //interchange
       swap(a[i],a[j]);
   }

   if(i==j) i++;

   swap(a[r], a[i]);

   return i;
}


//Function to sort the array using quick sort in descending order
void quicksort(int a[], int l, int r)
{
   if(l>=r) return;

   int i=partition(a,l,r);

   quicksort(a,l,i-1);

   quicksort(a,i+1,r);
}

//Function to print the array
void print(int arr[], int n)
{
for(int i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}

//Function to modify the array and return max number of digit
int modifyArray(int a[], int n)
{
int max = 0;

for(int i=0; i<n; i++)
{
int t = a[i];
int count = 0;
while(t!=0)
{
count++;
t=t/10;
}
if(count>max) max = count;
}

int m = pow(10, max-1);

for(int i=0; i<n; i++)
{
while(a[i]<m)
{
a[i] = 10*a[i] + 5;
}
}
return max;
}

//Function to sort the array using radix sort in descending order
void radixSort(int a[], int n, int num)
{
int b[10][5], c[10];
int i, div,large,l;
div=1;

for(int passes=0; passes<num; passes++)
{
for(int k=0;k<10;k++)
c[k]=0;
for(i=0;i<n;i++)
{
l=(a[i]/div)%10;
b[l][c[l]++]=a[i];
}
i=0;
for(int k=9;k>=0;k--)
{
for(int j=0;j<c[k];j++)
a[i++]=b[k][j];
}
div*=10;
}
}


//main function
int main()
{
ifstream ifs;

//open the file
ifs.open("num.txt");

int arr[10], n;

//read the numbers fro the file
for(n=0; ifs>>arr[n]; n++);

//print the array
cout<<"Original array:"<<endl;
print(arr, n);

//quicksort in descending order
quicksort(arr,0,n-1);

cout<<"After quick sort:"<<endl;
//print the array
print(arr, n);

//modify the array
int num = modifyArray(arr, n);

cout<<"After modification:"<<endl;
//print the array
print(arr, n);

//radixsort in descending order
radixSort(arr, n, num);

cout<<"After radix sort:"<<endl;
//print the array
print(arr, n);

return 0;
}

Output:

Original array:
25 137 52 26 234 92 43 399 37 59
After quick sort:
399 234 137 92 59 52 43 37 26 25
After modification:
399 234 137 925 595 525 435 375 265 255
After radix sort:
925 595 525 435 399 375 265 255 234 137


Related Solutions

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 : JavaModify 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...
If we change the randomized QUICKSORT algorithm so that it repeatedly randomly selects a pivot and...
If we change the randomized QUICKSORT algorithm so that it repeatedly randomly selects a pivot and runs PARTITION until it finds a good pivot, and suppose we keep track of the pivots used so far so we never use one twice for the same array, what is the worst case cost of the algorithm? Give a recurrence for this and its asymptotic solution (you can use the master method.)
implement c++ Quicksort using median of 3 as pivot. this also pass comparator as a function...
implement c++ Quicksort using median of 3 as pivot. this also pass comparator as a function param so it can sort vector in increasing or decreasing order based on the comparator passed. the test code uses std::less comparator. #ifndef QSORT_H #define QSORT_H #include #include using namespace std; template T median_of_three(T& a, T& b, T& c, TComparator comparator) { } template size_t partition(vector& vec, TComparator& comparator, size_t low, size_t high) { // TODO: implement. } template void QuickSort(vector& vec, TComparator comparator,size_t...
Write an implementation of quicksort where the pivot is selected randomly.
Write an implementation of quicksort where the pivot is selected randomly.
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...
What type of algorithm is the Quicksort algorithm if it has random pivots?
What type of algorithm is the Quicksort algorithm if it has random pivots?
(do not need to code) Consider QuickSort on the array A[1:n] andassume that the pivot...
Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split the array A[lo:hi] into two portions such that all elements in the left portion A[lo:m] are =< x and all elements in the right portion A[m:hi] are >=x) is the first element of the array to be split (i. e., A[lo]).Construct an infinite sequence of numbers for n and construct an assignment of the numbers 1...n to the n array elements that causes QuickSort,...
Quicksort (underline the pivot at each step) 19, 22, 8, 90, 13
Quicksort (underline the pivot at each step) 19, 22, 8, 90, 13
. Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to...
. Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split the array A[lo:hi] into two portions such that all elements in the left portion A[lo:m] are ≤x and all elements in the right portion A[m:hi] are ≥x) is the second element of the array to be split (i. e., A[lo+1]). For a specific value of n at least 13 (your choice), construct an assignment of the numbers 1...n to the n array elements...
What is the reason to choose a random pivot element in Randomized Quicksort? How can using...
What is the reason to choose a random pivot element in Randomized Quicksort? How can using randomization in this way possibly help?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT