Question

Question7 Use Quicksort to sort the array A <H.U.RRI CAN E in alphabetical order. Draw the tree of the recursive calls made Upload Choose a File Question 8


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

qsort.cpp

#include<iostream>
using namespace std;

void quickSort(char arr[], int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = arr[((left + right) / 2)];
  

/* partition */
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
cout<<"i and j are"<<i<<" "<<j<<"and corresponding array value is"<<arr[i]<<" " <<arr[j]<<endl;
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
cout<<"entering while loop"<<endl;
for(int i=0;i<7;i++)
cout<<arr[i]<<" "<<endl ;
}
}
cout<<"recursion"<<endl;

/* recursion */
if (left < j)
quickSort(arr, left, j);

if (i< right)
quickSort(arr, i, right);
}
int main(){
char arr[9]= {'h','u','r','r','i','c','a','n','e'};
for(int i=0;i<9;i++)
cout<<arr[i]<<" " ;
quickSort(arr,0,8);
cout<<endl;
for(int i=0;i<9;i++)
cout<<arr[i]<<" " ;
int wait;
cin>>wait;
return 0;
}

output showing recursive calls:

Select CAProgram Files (x86)\Dev-CpplConsolePauser.exe h u rri c an e i and j are1 8and corresponding array value isu e enterSelect CProgram Files (x86)\Dev-CpplConsolePauser.exe recursion i and j are0 3and corresponding array value ish c ntering whiSelect CProgram Files (x86)\Dev-CpplConsolePauser.exe entering while loop recursion i and j are5 7and corresponding array val

you can change input in the program to check for different values also.

Add a comment
Know the answer?
Add Answer to:
Question7 Use Quicksort to sort the array A <H.U.RRI CAN E in alphabetical order. Draw 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
  • 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:...

  • QUESTION: //Sort the array arr[] for (int i = 0; i < arr.length - 1; i++)...

    QUESTION: //Sort the array arr[] for (int i = 0; i < arr.length - 1; i++) { //outer int index = i; for (int j = i + 1; j < arr.length; i++) if (arr[j] < arr[index]) index = j; int smallerNumber = arr[index]; arr[index] = arr[i]; arr[i] = smallerNumber; }//for i In the array= 16 13 15 14 19 24 9 3, the index of the smallest number at the outer iteration 4 = Answer

  • [5 marks] Using selection sort algorithm to sort the array int array[7]-5, 6, 2, 7, 9,...

    [5 marks] Using selection sort algorithm to sort the array int array[7]-5, 6, 2, 7, 9, 4, 3). Assuming we call the following function using statement selection sort (int array, 7); Please fill the table to show the changes of the array int_array after each iteration. The first column is filled. void selection_sort (int list[], int list_size) for (int i = 0; i < list size - 1; 1++) int current min = list[1]; int current_min_index-i for (int j -...

  • Java Question: Code the Merge Sort in such a way that it outputs the number of...

    Java Question: Code the Merge Sort in such a way that it outputs the number of comparisons and the number of swaps performed when sorting a random set of items. Then use it to sort 1000, 5000, 10,000, and 100,000 integers. Tabulate the results and compare it to the number of comparisons and swaps calculated using the formulas given in Table 8.6. Table 8.6 Performance of the Quicksort Algorithm Speed Memory Overhead Range Bytes lgorithm Range Effort Comments Binary Tree...

  • Call stack question! Long one... The call stack is part of main memory that is reserved...

    Call stack question! Long one... The call stack is part of main memory that is reserved for function calling. Like all r memory it is finite, so can be exhausted resulting in a stack overflow. Recursive functions allocate space on the stack for each recursive call: if there are many such recursive calls a stack overflow can result. The questions that follow ask you to investigate recursive functions and stack overflows. Note that when running programs in the Linux terminal...

  • Can someone please help me in solving question in attached image. Thanks Suppose that an array...

    Can someone please help me in solving question in attached image. Thanks Suppose that an array A of n 1 numbers is given. We want to create a n x n matrix B such that, for 0 i<j<n, Bli.j] AIk] = Ali]Ali+1] for n ij 0, B[i.j] 0 +Aljl k-i I need to write a program where it accepts A as array and return B as matrix defined above, for O(n 2) algorithm

  • Rank the elements in each of the following sets in order of increasing atomic radius (Use...

    Rank the elements in each of the following sets in order of increasing atomic radius (Use the appropriate <-> symbol to separate substances in the lot.) (a) , cherad Help X.X (b), A C chemPad Help Greek

  • can you solve it by explaning step by step and can you draw the graph of...

    can you solve it by explaning step by step and can you draw the graph of them and explain throgh the graph 5. Solve sin x < cos x on (0,21). (A) 0, 51 (B) SA -21 4 (C) π 5π 4² 4 (D) 571 ,210 4 (E) (0,2TC)

  • Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify...

    Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify the class by adding sort member function (refer to Algorithm Analysis notes for sorting algorithms) Sample usage: a. sort(); Add the needed code in main to test your function. template <class T> 1/you can use keyword typename instead of class class Array { private: T *ptr; int size; public: Array(T arr[], int s); void print(); template <class T> Array<T>:: Array (T arr[], int s)...

  • Java. Please use the list of numbers from input.txt Objectives - practice sorting arrays - practice...

    Java. Please use the list of numbers from input.txt Objectives - practice sorting arrays - practice file 10 Description Assume there is a file called input.txt containing multiple integers (one on each line) Create a code that writes the numbers from input.txt into a file called output.txt in sorted order (ascending). Submission Demonstrate the functionality of your code to me by the assigned due date. <Back input.txt 100 91 52 (Васк input.txt on On @ morom 0 8:55368.241 22

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