Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Instructions: Editon only where it displays edit here in the quicksort.cpp file for it to pass all tests in the main.cpp file and output 100%
Instructions: Editon only where it displays edit here in the quicksort.cpp file for it to pass all tests in the main.cpp file and output 100%
//main.cpp
#include#include #include "quicksort.h" using namespace std; inline void _test(const char* expression, const char* file, int line) { cerr << "test(" << expression << ") failed in file " << file << ", line " << line << "." << endl; abort(); } #define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__)) int main() { srand(2017); int A[11] = {6, -1, 9, 3, 4, 6, 0, -5, -9, 7, 1}; quicksort(A, 11); cout << "10% earned." << endl; test(A[0] == -9); cout << "20% earned." << endl; test(A[1] == -5); test(A[2] == -1); test(A[3] == 0); test(A[4] == 1); test(A[5] == 3); test(A[6] == 4); test(A[7] == 6); test(A[8] == 6); test(A[9] == 7); test(A[10] == 9); cout << "50% earned." << endl; int B[100]; for (int i = 0; i < 100; ++i) B[i] = 100-i; quicksort(B, 100); cout << "55% earned." << endl; test(B[0] == 1); test(B[1] == 2); test(B[2] == 3); cout << "65% earned." << endl; test(B[99] = 98); test(B[99] = 99); test(B[99] = 100); cout << "75% earned." << endl; int len = 1000000; int* C = new int[len]; for (int i = 0; i < len; ++i) C[i] = rand(); clock_t start = clock(); quicksort(C, len); clock_t end = clock(); float duration = static_cast (end-start)/CLOCKS_PER_SEC; for (int i = 0; i < len-1; ++i) test(C[i] <= C[i+1]); test(duration < 1.0); cout << "100% earned." << endl; cout << "Sorting " << len << " ints took " << duration << " seconds." << endl; delete[] C; }
===============================================
//quicksort.cpp
#include "quicksort.h" void quicksort(int* A, int len) { // EDIT HERE // Algorithm: // 0. If len < 2, return. // 1. Pick a pivot element (A[0]?) // 2. Count the number of elements < A[0]. Call it s. // 3. Move pivot to index s by swapping with element currently there. // 4. Create two index variables, l and r. // 5. While l < s and s < r: // 5a. Move l rightward until element > pivot is found. // 5b. Move r leftward until element < pivot is found. // 5c. Swap A[l] and A[r]. // 6. Call quicksort on elements left of pivot. // 7. Call quicksort on elements right of pivot. }
===================================================
//quicksort.h
#ifndef QUICKSORT_H #define QUICKSORT_H void quicksort(int* array, int length); #endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started