Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Language is C++, modify the example codes to do the following: Generate an array of structs with 50 points, which are randomly initiated with x
Language is C++, modify the example codes to do the following:
Generate an array of structs with 50 points, which are randomly initiated with x (0.0-30.0) and y (0.0-20). Then modify the Quick Sort algorithm to sort points. If two points has same x values, then compares y values.. Assume that the struct is
struct point { double x; double y; };
example code:
#include#include #include #include #define N 20 using namespace std; void swap(int A[], int i, int min); bool small(int a, int b); void print(int A[]); void fillRandom(int A[], int size); void sort(int A[], int lo, int hi); int partition(int A[], int lo, int hi); int main(int argc, const char * argv[]) { int A[N]={0}; fillRandom(A,N); print(A); //Shuffle(A); shuffle the array. commentted out for now sort(A, 0, N-1); print(A); } void sort(int A[], int lo, int hi) { if (hi <= lo) return; int j = partition(A, lo, hi); sort(A, lo, j-1); sort(A, j+1, hi); } int partition(int A[], int lo, int hi) { int i = lo, j = hi+1; while (true) { while (small(A[++i], A[lo])) // find item on left to swap if (i == hi) break; while (small(A[lo], A[--j])) // find item on right to swap if (j == lo) break; if (i >= j) break; //check if pointers cross swap(A, i, j); } swap(A, lo, j); return j; // return index of item now known to be in place } void swap(int A[], int i, int min) { int tmp= A[i]; A[i]=A[min]; A[min]=tmp; } void fillRandom(int A[], int size) { for (int i = 0; i < N; i++) A[i]=rand() % 50; } void print(int A[]) { for (int i = 0; i < N; i++) cout << A[i] << ", "; cout << endl; } bool small(int a, int b) { if (a <= b) return true; return false; }
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