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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!