Question

quickSort function. Calling previous functions already implemented. This is a bit different type of quicksort function...

quickSort function. Calling previous functions already implemented.

This is a bit different type of quicksort function where my partition function has 3 parameters instead of 4.

So I need to write a function quicksort that actually puts all 3 of my functions together and finalizes the sorting process

"There should be almost nothing done besides calling the other 3 functions and recursively calling itself (except to check for basecase)

class quicksort
{
private:
int left;
int right;
int* array;
  
public:
void swapListValues(int* array, int left, int right );
int findAndSwapPivot(int* array, int left, int right );
int partitionList(int* array, int left, int right, int pivot );
void quickSort(int* array, int left, int right );
};

----

void quicksort::swapListValues(int* array, int left, int right )
{
int temp = array[left];
array[left] = array[right];
array[right] = temp;
}

----

int quicksort::findAndSwapPivot(int* array, int left, int right )
{
int temp = (right + left) / 2;
int pivot = array[temp];

swapListValues(array, temp, right );

return pivot;
  
}

----

int quicksort::partitionList(int* array, int left, int right, int pivot) {

// one lower than range
int smallerIndex = left-1;
// start at rightmost element
int largerIndex = right + 1;

//infinite loop while conditions are met
for (;;)
{
// find a larger-than-pivot element starting from the left
while (array[++smallerIndex] < pivot);

while (pivot < array[--largerIndex])
{
if (largerIndex == left)
break;
}

if (smallerIndex >= largerIndex) break;
swapListValues(array, smallerIndex, largerIndex);
}

// return the index of the pivot-position
return smallerIndex;
}

void quicksort::quickSort(int* array, int left, int right )
{

}

main function (just uncomment to test)

int main(int argc, char** argv)

{

const int MAX_TEST_SIZE = 100;
int testvals[MAX_TEST_SIZE];
int expected[MAX_TEST_SIZE];
int length;
  
quicksort sortAndSearch;

cout << "Test quickSort() -----------------------------------------------" << endl;

// sort a list of size 1
int length = 1;
memcpy(testvals, (const int[]){8},
sizeof(int) * length);
memcpy(expected, (const int[]){8},
sizeof(int) * length);
// sortAndSearch.quickSort(testvals, 0, length-1);
// cout << "quickSort() test 1, sort list of size 1" << endl
// << " expected: " << tostring(expected, length) << endl
// << " actual: " << tostring(testvals, length) << endl
// << endl;
// assert(listsAreEqual(testvals, expected, length));

return 0;

}

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

//~ quicksort working but i have problem using bottom commented code
//~ calling tostring(), assert() functions. may be this is because gcc version problem
//~ should be working on your pc... let me know if there is problem. thanks...

#include <iostream>
#include <iomanip>
#include <assert.h>
using namespace std;

class quicksort {
private:
int left;
       int right;
       int * array;

public:
       void swapListValues(int * array, int left, int right);
       int findAndSwapPivot(int * array, int left, int right);
       int partitionList(int * array, int left, int right, int pivot);
       void quickSort(int * array, int left, int right);
};

void quicksort::swapListValues(int * array, int left, int right) {
int temp = array[left];
array[left] = array[right];
array[right] = temp;
}
  
int quicksort::findAndSwapPivot(int * array, int left, int right) {
int temp = (right + left) / 2;
int pivot = array[temp];
swapListValues(array, temp, right);
return pivot;

}
  
int quicksort::partitionList(int * array, int left, int right, int pivot) {
// one lower than range
int smallerIndex = left - 1;
// start at rightmost element
int largerIndex = right + 1;
//infinite loop while conditions are met
for (;;) {
// find a larger-than-pivot element starting from the left
while (array[++smallerIndex] < pivot);
while (pivot < array[--largerIndex]) {
if (largerIndex == left)
break;
}
if (smallerIndex >= largerIndex) break;
swapListValues(array, smallerIndex, largerIndex);
}
// return the index of the pivot-position
return smallerIndex;
}
void quicksort::quickSort(int * array, int left, int right)
   {
       if (left < right)
       {
           /* piv is index of the pivot-position */
           int piv = array[right]; //starting from right initialy
           piv = partitionList(array, left, right, piv);
  
           // Separately sort elements before
           // partition and after partition
           quickSort(array, left, piv - 1);
           quickSort(array, piv + 1, right);
       }   
   }

//main function(just uncomment to test)
int main(int argc, char ** argv) {
const int MAX_TEST_SIZE = 100;
int testvals[MAX_TEST_SIZE];
int expected[MAX_TEST_SIZE];
int length;

quicksort sortAndSearch;
cout << "Test quickSort() -----------------------------------------------" << endl;
// sort a list of size 1
length = 6;
memcpy(testvals, (const int[]) {
8,2,3, 1,4,5
},
sizeof(int) * length);
memcpy(expected, (const int[]) {
1,2,3, 4,5,8
},
sizeof(int) * length);
  
sortAndSearch.quickSort(testvals, 0, length-1);
  
//added for loop to print sorted array - testvals
for (int i = 0; i < length; i++)
       cout<< testvals[i] << " ";          
      
  
//~ cout << "quickSort() test 1, sort list of size 1" << endl
//~ << " expected: " << tostring(expected, length) << endl
//~ << " actual: " << tostring(testvals, length) << endl
//~ << endl;
//~ assert(listsAreEqual(testvals, expected, length));
return 0;
}

Add a comment
Know the answer?
Add Answer to:
quickSort function. Calling previous functions already implemented. This is a bit different type of quicksort function...
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++. 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: ";...

  • Language C++ (Please include a short description & Screenshot of output) Implement a Priority...

    Language C++ (Please include a short description & Screenshot of output) Implement a Priority queue using a SORTED list. Use Quick sort after adding a new node. Example of quick sort below. Adopt to your program the code below. #include <iostream> void quickSort(int a[ ], int first, int last); int pivot(int a[], int first, int last); void swap(int& a, int& b); void swapNoTemp(int& a, int& b); void print(int array[], const int& N); using namespace std; int main() { int test[]...

  • Rewrite this code in Java please #include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace...

    Rewrite this code in Java please #include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace std; long length = 1000; const long max_length = 100000; int list[max_length]; void read() {     ifstream fin("random.dat", ios::binary);     for (long i = 0; i < length; i++)     {         fin.read((char*)&list[i], sizeof(int));     }     fin.close(); } void bubbleSort() {     int temp;     for(long i = 0; i < length; i++)     {         for(long j = 0; j< length-i-1; j++)...

  • 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 <<...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

  • 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...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • 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               {                     ...

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