Question

In: Computer Science

Generate a random list NUMBERS of size 100. Sort NUMBERS using QUICKSORT until the sublist has...

Generate a random list NUMBERS of size 100.

Sort NUMBERS using QUICKSORT until the sublist has size 15 or less; then use INSERTIONSORT on the sublist.

Solutions

Expert Solution

Here is the required code:

#include<iostream>
using namespace std;
template<class t>
class sorting
{
public:
   int SIZE, a[100];
   void getdata();
   void isort();
   void qsort(int[], int, int);
   int partition(int[], int , int);
   void display();
};
template<class t>
void sorting<t>::getdata()
{
   cout << "\nEnter the size of an array:";
   cin >> SIZE;
   cout << "Randomly generated elements of the array: ";
   for (int i = 1;i <= SIZE;i++)
{a[i] = (rand()%100)+1;       cout<<a[i]<< " ";}
}
template<class t>
void sorting<t>::display()
{
   cout << "\nSorted Array: ";
   for (int i = 1;i <= SIZE;i++)
       cout << a[i] << " ";
}
template<class t>
void sorting<t>::msort(int A[], int p, int r)
{
   int q;
   if (p < r)
   {
       q = (p + r) / 2;
       msort(A, p, q);
       msort(A, q + 1, r);
       merge(A, p, q, r);
   }
}
template<class t>
void sorting<t>::merge(int A[], int p, int q, int r)
{
   int n1, n2, i, j;
   n1 = q - p + 1;
   n2 = r - q;
   int L[100];
   int R[100];
   for ( i = 1;i <= n1;i++)
       L[i] = A[p+i-1];
   for ( j = 1;j <= n2;j++)
       R[j] = A[q+j];
   L[n1+1] = 32767;
   R[n2+1] = 32767;
   i = j = 1;
   for (int k = p;k <= r;k++)
   {
       if (L[i] <= R[j])
       {
           A[k] = L[i];
           i = i + 1;
       }
       else
       {
           A[k] = R[j];
           j = j + 1;
       }
   }
}
template<class t>
void sorting<t>::qsort(int A[], int p, int r)
{
   int q;
   if (p < r)
   {
       q =   partition(A, p, r);
       qsort(A, p, q - 1);
       qsort(A, q + 1, r);
   }
}
template<class t>
int sorting<t>::partition(int A[], int p, int r)
{
   int x, i, j, temp;
   x = A[r];
   i = p - 1;
   for (j = p;j <= r - 1;j++)
   {
       if (A[j] <= x)
       {
           i = i + 1;
           temp = a[i];
           a[i] = a[j];
           a[j] = temp;
       }
   }
   temp = a[i+1];
   a[i+1] = a[r];
   a[r] = temp;
   return i + 1;
}


void sorting::isort()
{
   int i, j, pivot;
   for (i = 1;i < SIZE;i++)
   {
       pivot = a[i];
       j = i - 1;
       while (j >= 0 && a[j] > pivot)
       {
           a[j+1] = a[j];
           j = j - 1;
       }
       a[j+1] = pivot;
   }
}
int main()
{
   sorting<float>s;
   int f;
           cout << "\nQuick Sort\n";
           s.getdata();
           s.qsort(s.a, 1, s.SIZE);
           s.display();
   return 0;
}


Related Solutions

To generate 100 random numbers between 1-100 in a randomData.txt file To read the 100 random...
To generate 100 random numbers between 1-100 in a randomData.txt file To read the 100 random numbers from randomData.txt and store them in an array Print the data in the array Find the smallest and the largest of the random numbers and their array position Insert an element of value100 in the 51th position of the array Delete all the elements of the array having values between 50-80 and print the residual array Sort the data in the final array(residual)...
Using Python, generate 100 random numbers ranging from 1 to 5, then plot them using matplotlib...
Using Python, generate 100 random numbers ranging from 1 to 5, then plot them using matplotlib or Seaborn as a histogram
You want to implement Radix Sort to sort a large set of random numbers using an...
You want to implement Radix Sort to sort a large set of random numbers using an auxiliary sorting algorithm for the digits and you want your algorithm to be fast. Which auxiliary sorting algorithms would you use? 1. Heap Sort 2. Merge Sort 3. Insertion Sort 4. Selection Sort 5. Basic Quick Sort
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100 and 300. Pick a number at random out of these 50 random variables. a. Determine the probability of the chosen number being greater than 200. This may be achieved by counting the numbers that are greater than 200 and dividing the count by 50. Make sure you, i.Formulate the appropriate if-conditions to check for a number being greater than 200 ii. Use a for-loop...
Propose two of your own random number generation schemes. Please generate 100 random numbers ?? (?...
Propose two of your own random number generation schemes. Please generate 100 random numbers ?? (? = 1,2, … ,100) for each scheme and show the results on the same plot for comparison (i.e., x-axis of the plot will show the index ? and y-axis will show the generated random numbers ??. You can use different colors and/or symbols to distinguish one sequence from the other). Discuss which scheme will be preferred.
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...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array to a temp array 3.) Call each of the methods to sort (bubble, selection, insertion, quick, merge), passing it the array 4.) In-between the calls, you are going to refresh the array to the original numbers. 5.) Inside of each sorting method, you are going to obtain the nanoseconds time, before and after the method Subtract the before time from the after time to...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
Generate 200 random numbers using each of the following continuous distributions and plot the histograms (a)...
Generate 200 random numbers using each of the following continuous distributions and plot the histograms (a) Uniform (use A = 3 and B = 7) (b) Exponential (use population mean = 0.2) (c) Gamma (use parameters 3 and 2) (d) Normal (use µ = 2 and σ = 5) Attach the histogram plots and reflect on what you observe. Hint: Use RAND() to generate uniform random numbers and use inverse CDF to generate random numbers for each distribution.
Generate 1000 random numbers from ??3? starting with standard normal random numbers in R.
Generate 1000 random numbers from ??3? starting with standard normal random numbers in R.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT