Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want to know how to fix my issue with the following Code, I run it through the purple version of Visual studio and it

I want to know how to fix my issue with the following Code, I run it through the purple version of Visual studio and it gives me errors at line 30.

The requirements are as follows

Generate an input array to be used for different sort implementations.

For each of the implemented sort algorithms

o make a copy of the input array

o start the timer

o run the sort algorithm on the array

o stop the timer o display the running time in milliseconds

The program needs to generate an array (not an ArrayList) that youll sort using different methods. You need to decide on the type of the array to use for testing. It is totally up to you. Your program needs to populate the array with random values corresponding to whichever type you choose. Before running each of the sort methods on the array, you should make a copy of it and run your implementation of the algorithm on the copy. You have to make sure that each algorithm starts with the exact same array as the previous one. You can use any language built-in procedure or library, if you wish.

You should implement four sorting algorithms: selection sort, merge sort, quick sort, and heap sort. You must implement those using generic methods. Your implementation should follow the pseudo-code outlined in the lectures or elsewhere

The program needs to measure the time it takes for each of your four methods to complete sorting for several different sizes of the array. It is up to you to:

either design a program that generates an array of a particular size and then run it multiple times,

or design a program that runs all different sizes on its own. In either case, your program should not be interactive (no user input should be expected).

Use may use the C++ system time method to get the time before and after the call to the sort function. The difference between the two will tell you how many nanoseconds it takes for the algorithm to complete its task. You need to make sure that you use the appropriate time unit milliseconds or nanoseconds.

Here is the Code:

#include #include

void selectionSort(int arr[], int n)

{ int minmum, ttmmpp; for (int i = 0; i < n - 1; i++) { minmum = i;

for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minmum]) minmum = j; } if (minmum != i) { ttmmpp = arr[i]; arr[i] = arr[minmum]; arr[minmum] = ttmmpp; } } } void merge_Sorrt(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; i = 0; j = 0; k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } // void mmergsortt(int arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; mmergsortt(arr, l, m); mmergsortt(arr, m + 1, r); merge_Sorrt(arr, l, m, r); } } void quick(int k[], int lb, int ub) { int i, j, key, flag = 0, ttmmpp; if (lb < ub) { i = lb; j = ub + 1; key = k[i]; while (flag != 1) { i++; while (k[i] < key) { i++; } j--; while (k[j] > key) { j--; } if (i < j) { ttmmpp = k[i]; k[i] = k[j]; k[j] = ttmmpp; } else { flag = 1; ttmmpp = k[lb]; k[lb] = k[j]; k[j] = ttmmpp; } } quick(k, lb, j - 1); quick(k, j + 1, ub); } } //heapsort void max_heapify(int* a, int i, int n) { int j, ttmmpp; ttmmpp = a[i]; j = 2 * i; while (j <= n) { if (j < n && a[j + 1] > a[j]) j = j + 1; if (ttmmpp > a[j]) break; else if (ttmmpp <= a[j]) { a[j / 2] = a[j]; j = 2 * j; } } a[j / 2] = ttmmpp; return; } void heapsort(int* a, int n) { int i, ttmmpp; for (i = n; i >= 2; i--) { ttmmpp = a[i]; a[i] = a[1]; a[1] = ttmmpp; max_heapify(a, 1, i - 1); } } int main() { int ar[] = { 10, 2, 45, 18, 16, 30, 29, 1, 1, 100 }; int time_taken; printf("input is: "); for (int ii = 0; ii < 10; ii++) { printf(" %d", ar[ii]); } clock_t t; t = clock(); selectionSort(ar, 10); t = clock() - t; double timinsec = ((double)t) / CLOCKS_PER_SEC; // in seconds double timinmilsec = (time_taken * 1000); printf(" time for selection sort in milliseconds %f ", timinmilsec); t = clock(); mmergsortt(ar, 0, 9); t = clock() - t; timinsec = ((double)t) / CLOCKS_PER_SEC; // in seconds timinmilsec = (time_taken * 1000); printf("time for merge sort in milliseconds %f ", timinmilsec); t = clock(); quick(ar, 0, 9); t = clock() - t; timinsec = ((double)t) / CLOCKS_PER_SEC; // in seconds timinmilsec = (time_taken * 1000); printf("time for quick sort in milliseconds %f ", timinmilsec); t = clock(); heapsort(ar, 10); t = clock() - t; timinsec = ((double)t) / CLOCKS_PER_SEC; // in seconds timinmilsec = (time_taken * 1000); printf("time for heap sort in milliseconds %f ", timinmilsec); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students also viewed these Databases questions

Question

Problem: Evaluate the integral: I - -[ze dx

Answered: 1 week ago

Question

Problem: Evaluate the integral: I = 1- 1 dx 9

Answered: 1 week ago

Question

Describe the Indian constitution and political system.

Answered: 1 week ago

Question

=+ Have they changed the way employers view IP?

Answered: 1 week ago