Question
Please I need Solution in an hour

1-Sort by steps in ascending way by Quick sort (3-way-partitioning) (ZAHRLI SME TJD BUNK) 2-Sort by steps ascending way by he


1-Sort by steps in ascending way by Quick sort (3-way-partitioning) for (ZAHRLISMET JD BUNK)
2-Sort by steps ascending way by heap sort and heap construction and tree for (ZAHRLISMETJD BUNK).
3. Create the binary search tree( BST ) from (ZAHRLIS MET JDBUN K). And in steps, Then delete the min and explain the result
4. Create with steps search tree 2-3 of the (ZAHRLISMET JD BUN K)
5. Create with steps red-black BST for (ZAHRLISMETJD BUNK)

show the steps for each algorthim
C++
0 0
Add a comment Improve this question Transcribed image text
Answer #1

[N.B: Please post individual questions seperately. Only one question can be solved at a time as these are all seperate questions and not subparts.and also can not be solved all together within 2 hours.  Solution of question 1 is given below.]

question 1:

C++ program for quick sort using 3- way partition algorithm


#include <bits/stdc++.h>
using namespace std;

/* The following function partitions the array[] in three parts
1) array[l..i] contains elements smaller than pivot
2) array[i+1..j-1] contains all occurrences of pivot
3) array[j..r] contains elements that are greater than pivot */
void createpartition(char array[], int l, int r, int &i, int &j)
{
i = l-1, j = r;
int lp = l-1, rq = r;
char value = array[r];
  
while (true)
{
// From left, find the first element >=value
  
while (array[++i] < value);
  
// From right, find the first element <= value

while (value < array[--j])
if (j == l)
break;
  
// when i and j cross, then we are done
if (i >= j) break;
  
// Swap, so that smaller goes to left and greater goes to right
swap(array[i], array[j]);
  
// Move all same left occurrence of pivot to beginning of
// array and keep count using lp
if (array[i] == value)
{
lp++;
swap(array[lp], array[i]);
}
  
// Move all same right occurrence of pivot to end of array
// and keep count using rq
if (array[j] == value)
{
rq--;
swap(array[j], array[rq]);
}
}
  
// Move pivot element to its correct index
swap(array[i], array[r]);
  
// Move all left same occurrences from beginning to adjacent to array[i]
  
j = i-1;
for (int k = l; k < lp; k++, j--)
swap(array[k], array[j]);
  
// Move all right same occurrences from end to adjacent to array[i]
  
i = i+1;
for (int k = r-1; k > rq; k--, i++)
swap(array[i], array[k]);
}
  
// 3-way partition based quick sort
void quicksort(char array[], int l, int r)
{
if (r <= l) return;
  
int i, j;
  
createpartition(array, l, r, i, j);
  
// Recursive function call
quicksort(array, l, j);
quicksort(array, i, r);
}
  
// print the array
void printarr(char array[], int n)
{
for (int i = 0; i < n; ++i)
cout<<array[i]<<" ";
cout<<endl;
}
  
// Driver code
int main()
{
char array[] = {'Z','A','H','R','L','I','S','M','E','T','J','D','B','U','N','K'};
int size = strlen(array)-1;//sizeof(array) / sizeof(int)
cout<<"The given array is ";
printarr(array, size);
cout<<endl;
quicksort(array, 0, size - 1);
cout<<"The Sorted array is ";
printarr(array, size);
return 0;
}
  

Output:

The given array is Z A H R L I S M ET JD BUNK The Sorted array is A B D E H I J K L M N R S T U Z

Add a comment
Know the answer?
Add Answer to:
Please I need Solution in an hour show the steps for each algorthim C++ 1-Sort by...
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
ADVERTISEMENT