Question
Language is C++ Given the below algorithms. Compare both algorithms in terms of number of swaps, assignments and comparisons are done. Steps to follows are
Language is C++
Given the below algorithms. Compare both algorithms in terms of number of "swaps", "assignments" and "comparisons" are done.
Steps to follows are here:
- Define two char array with 8K and 16K items in them. Let's assume the first array is 8K, and second array on is 16K. Both arrays will be randomly fill out with numbers between -125 and +125. - Sort both arrays with selection sort and report how many "swaps", "assignments" and "comparisons" are done within each arrays. - Sort both arrays with insertion sort and report how many "swaps", and "comparisons" are done within each arrays.
Make sure that the arrays shuffles before second sort.
// Selection sort for (int i = 0; i < N; i++) { int min = i; for (int j = i+1; j < N; j++) { if ( small(A[j], A[min]) == true) min = j; // compare } swap(A, i, min); // swap } | // Insertion sort for (int i = 0; i < N; i++) { for (int j = i; j > 0; j--) if ( small(A[j], A[j-1]) == true) // compare swap(A, j, j-1); // swap else break; } |
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