Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer in c and please get the same output Write a C program to implement selection and quick algorithms. It requires a header program

Please answer in c and please get the same output

Write a C program to implement selection and quick algorithms. It requires a header program mysort.h containing function headers of the following specifications, and mysort.c containing the implementation of the functions. You are allowed to add auxiliary functions for better design of your program.

/* Define an enumeration type named BOOLEAN with name false as 0 and true as 1. */ /* This function tests if float array a[] is sorted in increasing order between index left and right, and returns true if yes, otherwise false. */ BOOLEAN is_sorted(float *a, int left, int right); /* This function sorts the elements of float array a[] from index left to right in increasing order, using the selection sort algorithm. */ void select_sort(float *a, int left, int right); /* This function sorts the elements of float array a[] from index left to right in increasing order, using the quick sort algorithm. */ void quick_sort(float *a, int left, int right); 

Use the provided testing program mysort_main.c to test your program. The screen output should be like the following.

Public test

command: gcc mysort.c mysort_main.c -o mysort command: mysort Algorithm correctness testing: a[]:3.0, 1.0, 4.0, 5.0, 2.0, 7.0, 6.0, 9.0, 8.0 is_sorted(a):0 selection sort:1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 is_sorted(b):1 quick sort:1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 is_sorted(b):1 command: mysort time Algorithm runtime testing: time_span(selection_sort(10000 numbers) for 10 times)(ms):1774.0 time_span(quick_sort(10000 numbers) for 1000 times)(ms):1645.0 time_span(selection_sort(10000 numbers))/time_span(quick_sort(10000 numbers)):107.8

mysort_main.c:

#include #include #include #include "mysort.h"

// uncomment following line to do runtime testing. #define TEST void display_array(float *a, int n); // copy array from a to b void copy_array(float *a, float *b, int n);

int main(int argc, char *args[]) { int i, n; if (argc == 1) { // for correctness verification float a[] = {3, 1, 4, 5, 2, 7, 6, 9, 8}; // input array for correctness testing n = sizeof(a)/sizeof(float); // array length float b[n]; // array to sort printf("Algorithm correctness testing: "); printf("a[]:"); display_array(a, n); printf(" "); printf("is_sorted(a):%d ", is_sorted(a, 0, n-1)); printf("selection sort:"); copy_array(a, b, n); select_sort(b, 0, n-1); display_array(b, n); printf(" is_sorted(b):%d ", is_sorted(b, 0, n-1));

printf("quick sort:"); copy_array(a, b, n); quick_sort(b, 0, n-1); display_array(b, n); printf(" is_sorted(b):%d ", is_sorted(b, 0, n-1)); } else { // for runtime comparison

// conditional compiling for runtime testine. #ifdef TEST printf("Algorithm runtime testing: "); //run time measurement clock_t t1, t2; int len = 10000; float a1[len]; float b1[len]; //generate randomly an array of len elements more modular len srand(time(NULL)); for (i=0;i

// measuring the test overhead time span t1=clock(); for (i=0; i<1000; i++) { copy_array(a1, b1, len); is_sorted(b1, 0, len-1); } t2=clock(); double time_span0 = (double) (t2-t1)/1000; //printf("time_span0:%f ", time_span0); //run time measuring for selection_sort int m1 = 10; t1=clock(); for (i=0; i< m1; i++) { copy_array(a1, b1, len); select_sort(b1, 0, len-1); if (is_sorted(b1, 0, len-1) == false) printf("not sorted: "); } t2=clock(); double time_span1 = (double) t2-t1; time_span1 -= time_span0*m1; // remove the overhead time printf("time_span(selection_sort(%d numbers) for %d times)(ms):%0.1f ", len, m1, time_span1); //run time measuring for quick_sort int m2 = 1000; t1=clock(); for (i=0; i< m2; i++) { copy_array(a1, b1, len); quick_sort(b1, 0, len-1); if (is_sorted(b1, 0, len-1)== false) printf("not sorted:%d "); } t2=clock(); double time_span2 = (double) t2-t1; time_span2 -= time_span0*m2; // remove the overhead time printf("time_span(quick_sort(%d numbers) for %d times)(ms):%0.1f ", len, m2, time_span2); printf("time_span(selection_sort(%d numbers))/time_span(quick_sort(%d numbers)):%0.1f ", len, len, (time_span1/m1)/(time_span2/m2)); #endif } return 0; }

void display_array(float *a, int n) { int i; if (n>0) printf("%.1f", *(a+0)); for (i=1; i

void copy_array(float *a, float *b, int n) { int i; for (i = 0; i < n; i++) *(b+i) = *(a+i); }

mysort.h

#ifndef MYSORT_H #define MYSORT_H typedef enum {false, true} BOOLEAN; BOOLEAN is_sorted(float *a, int left, int right); void select_sort(float *a, int left, int right); void quick_sort(float *a, int left, int right); #endif

mysort.c

#include "mysort.h" BOOLEAN is_sorted(float *a, int left, int right) { // your implementation } void select_sort(float *a, int left, int right) { // your implementation } void quick_sort(float *a, int left, int right) { // your implementation }

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

Step: 3

blur-text-image

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

Mysql Examples Explanations Explain Examples

Authors: Harry Baker ,Ray Yao

1st Edition

B0CQK9RN2J, 979-8872176237

More Books

Students also viewed these Databases questions

Question

When is it appropriate to use a root cause analysis

Answered: 1 week ago