Question

hello, this is my C++ programm and I need help for this two question. how can...


hello, this is my C++ programm and I need help for this two question. how can i implement the two questions in my code



Write a recursive function template quicksort that takes a container of type T (T is, for example, array <int, 1000> or vector <string>) as a parameter and applies the algorithm described to the container so that it is not sorted in descending order. The function template should receive the container object, an initial index and an end index as arguments.

Another function template partition is to be called by quicksort to carry out the partitioning step. Test the result of your sorting by sorting an array <int, 1000> filled with random values and then a vector <string> filled with at least five words in an unsorted order and checking the sorting results with lessEqualSorted.


#include<iostream>

using namespace std;

//partition function

int partition(int arr[], int left, int right) {

    //initial value left

    int indexOfPivot = left;

    int i;

    //label to go back and repeat two steps

label:

    //firstr step

    i = right;

    //finding smaller or equal element

    while (i > indexOfPivot&& arr[i] > arr[indexOfPivot])

        i--;

    if (i == indexOfPivot)

        return indexOfPivot;

    swap(arr[indexOfPivot], arr[i]); //swapping

    indexOfPivot = i;

    //second step

    i = left;

    ////finding greater element

    while (i < indexOfPivot && arr[i] <= arr[indexOfPivot])

        i++;

    if (i == indexOfPivot)

        return indexOfPivot;

    swap(arr[indexOfPivot], arr[i]); //swapping

    indexOfPivot = i;

    goto label; //repeating step1 and 2

}

//recursive function

void qsort(int arr[], int start, int end) {

    if (start < end) {

        int p = partition(arr, start, end);

        qsort(arr, start, p - 1);   //left half sorting

        qsort(arr, p + 1, end); //right half sorting

    }

}

int main() {

    //testing quicksort

    int arr[] = { 16,17,18,7,6,9,14,1,15,16,7,10,1,19,13,18,9,7,7,9 }; //ist von chegg

    //int arr[] = { 75, 70, 65, 68, 98, 78, 100, 93, 55, 61, 81, 84 };

    int n = 20;

    //int n = 1000;

    cout << "Before Sorting: \n";

    for (int i = 0; i < n; i++) {

        cout << arr[i] << " ";

    }

    qsort(arr, 0, n - 1);

    cout << "\nAfter Sorting: \n";

    for (int i = 0; i < n; i++) {

        cout << arr[i] << " ";

    }

    cout << endl;

}

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

#include<iostream>
using namespace std;

//partition function
template <class T>
int partition(T arr[], int left, int right) {
//initial value left
int indexOfPivot = left;
int i;

//label to go back and repeat two steps
label:

//firstr step
i = right;

//finding smaller or equal element
while (i > indexOfPivot&& arr[i] > arr[indexOfPivot])
i--;
if (i == indexOfPivot)
return indexOfPivot;
swap(arr[indexOfPivot], arr[i]); //swapping
indexOfPivot = i;

//second step
i = left;

////finding greater element
while (i < indexOfPivot && arr[i] <= arr[indexOfPivot])
i++;
if (i == indexOfPivot)
return indexOfPivot;
swap(arr[indexOfPivot], arr[i]); //swapping
indexOfPivot = i;
goto label; //repeating step1 and 2
}

//recursive function
template <class T>
void qsort(T arr[], int start, int end) {
if (start < end) {
int p = partition(arr, start, end);
qsort(arr, start, p - 1); //left half sorting
qsort(arr, p + 1, end); //right half sorting
}
}

int main() {
//testing quicksort
int arr1[] = { 16,17,18,7,6,9,14,1,15,16,7,10,1,19,13,18,9,7,7,9 }; //ist von HomeworkLib
int n = 20;
//int n = 1000;
cout << "Before Sorting: \n";
for (int i = 0; i < n; i++) {
cout << arr1[i] << " ";
}
qsort(arr1, 0, n - 1);
cout << "\nAfter Sorting: \n";
for (int i = 0; i < n; i++) {
cout << arr1[i] << " ";
}
cout << endl;

int arr2[] = { 75, 70, 65, 68, 98, 78, 100, 93, 55, 61, 81, 84 };
n = 12;

cout << "Before Sorting: \n";
for (int i = 0; i < n; i++) {
cout << arr2[i] << " ";
}
qsort(arr2, 0, n - 1);
cout << "\nAfter Sorting: \n";
for (int i = 0; i < n; i++) {
cout << arr2[i] << " ";
}
cout << endl;

char arr3[] = {'j' , 'a' , 'i'};
n = 3;

cout << "Before Sorting: \n";
for (int i = 0; i < n; i++) {
cout << arr3[i] << " ";
}
qsort(arr3, 0, n - 1);
cout << "\nAfter Sorting: \n";
for (int i = 0; i < n; i++) {
cout << arr3[i] << " ";
}
cout << endl;

string arr4[] = {"jai" , "sethia" , "jain"};
n = 3;

cout << "Before Sorting: \n";
for (int i = 0; i < n; i++) {
cout << arr4[i] << " ";
}
qsort(arr4, 0, n - 1);
cout << "\nAfter Sorting: \n";
for (int i = 0; i < n; i++) {
cout << arr4[i] << " ";
}
cout << endl;
}

Add a comment
Know the answer?
Add Answer to:
hello, this is my C++ programm and I need help for this two question. how can...
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
  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • I want to compare the runtimes and swap operations times among quick Sort, selection Sort and...

    I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...

  • I need help with this code: #include <iostream> #include <cstdlib> #include <string> using namespace std; void...

    I need help with this code: #include <iostream> #include <cstdlib> #include <string> using namespace std; void dump(int ar[], int size) { cout << "\nDUMP [ "; for (int i=0; i<=size; i++) { cout << ar[i] << " "; } cout << "]\n\n"; } void quicksort(int ar[], int start, int end) { cout << "TOP OF SORT ===========================" << endl; dump(ar, start, end); int pivot = ar[end]; int left = start; int right = end - 1; int tmp; cout <<...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble...

    C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...

  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • I am trying to implement this version of quicksort in C/c++ but I am getting stuck....

    I am trying to implement this version of quicksort in C/c++ but I am getting stuck. Below is how the algorithm is supposed to work. This is the partitioning scheme I am trying to implement. 17, -10, 7, 19, 21, 23, -13, 31, 59 # ^ ^ start 17, -10, 7, 19, 21, 23, -13, 31, 59 # ^ ^ move left pointer to first element larger than pivot. 3 compares 17, -10, 7, 19, 21, 23, -13, 31, 59...

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • Hello, I am getting two errors with this code and I don't understand why? can you...

    Hello, I am getting two errors with this code and I don't understand why? can you help me please? This is JAVA public class Main { private static int partition(int a[],int start,int end) //this function takes first element as pivot. { int pivotValue; int endOfLeftList; pivotValue = a[start]; endOfLeftList = start; // At this point A[endOfLeftList] == pivotValue for (int scan = start + 1; scan <= end; scan ++) { if (a[scan] < pivotValue) { endOfLeftList ++; swap(a, endOfLeftList,...

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