Question
Kindly help....use Github and gcc tool.....I always remember to give a thumbs up to correct answers...help Asap
github.com Part 3 (15 pts) - Write a C source code module called insertionsort.c that performs an Insertion sort on a fixed size array. Provide a header file (insertionsort.h) so that your insertion sort function can be called from an external program. Define a symbol called IS_VERBOSE and use conditional compilation (#ifdef.#endif) to enable and disable reporting of diagnostic information as the sort proceeds. You can model this information on the information about each passthrough that is present in the starter code (a working Bubble Sort program). insertionsort.c should implement a function with the following prototype: int insertionSort (int arrll, int size, int* ncompares_ptr, int* nswaps_ptr); where arr[l is the integer array to sort, size is the number of elements in the array and ncompares_ptr and nswaps_ptr are pointers to int variables that will be written with the total number of compares performed and the total number of swaps performed to complete the sort. The function should return an integer containing the total number of steps (compares swaps) required to sort the array.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//header file

#ifndef INSERTIONSORT_H
#define INSERTIONSORT_H
int insertionSort(int arr[],int size,int *ncompares_ptr,int *nswaps_ptr);
#endif

//c file

#include <stdio.h>

#include <math.h>

#include "insertionsort.h"//including header

// A utility function to print an array of size n

void printArray(int arr[], int n)

{

int i;

for (i=0; i < n; i++)

printf("%d ", arr[i]);

printf("\n");

}

  

/* Function to sort an array using insertion sort*/

int insertionSort(int arr[],int size,int *ncompares_ptr,int *nswaps_ptr)

{

int i, key, j;

int n=size;

for (i = 1; i < n; i++)

{

  

key = arr[i];

  

j = i-1;

/* Move elements of arr[0..i-1], that are

greater than key, to one position ahead

of their current position */

while (j >= 0 && arr[j] > key)

{

//incrementing number of comparisions and swaps

(*ncompares_ptr)++;

(*nswaps_ptr)++;

arr[j+1] = arr[j];

j = j-1;

}

arr[j+1] = key;

//printArray(arr, n);

  

}

  

//returning sum of comparision and swaps

return (*nswaps_ptr)+(*ncompares_ptr);

}

/* Driver program to test insertion sort */

int main()

{

int arr[] = {21,14,32,10,44,8,2,11,20,26};

int n = sizeof(arr)/sizeof(arr[0]);

printf("Array before sorting:\n");

printArray(arr, n);

int c=0,s=0;;

int sum = insertionSort(arr, n,&c,&s);

printf("Array after sorting:\n");

printArray(arr, n);

printf("Comparisions : %d\n Swaps : %d\n Sum : %d\n",c,s,sum);

return 0;

}

output:

Array before sorting:
21 14 32 10 44 8 2 11 20 26
Array after sorting:
2 8 10 11 14 20 21 26 32 44
Comparisions : 24
Swaps : 24
Sum : 48


Process exited normally.
Press any key to continue . . .

Add a comment
Know the answer?
Add Answer to:
Kindly help....use Github and gcc tool.....I always remember to give a thumbs up to correct answers...help...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • LANGUAGE = C i. Write a program that takes int numbers from user until user gives...

    LANGUAGE = C i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks) ii. In this part take another number...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

  • Can I get some help with this question for c++ if you can add some comments...

    Can I get some help with this question for c++ if you can add some comments too to help understand that will be much appreciated. Code: #include <cstdlib> #include <getopt.h> #include <iostream> #include <string> using namespace std; static long comparisons = 0; static long swaps = 0; void swap(int *a, int *b) {     // add code here } void selectionSort(int *first, int *last) {     // add code here } void insertionSort(int *first, int *last) {     // add code here }...

  • Hi i will give you a thumbs up if you do this problem correctly. Sorting Analysis Code and Essay ...

    Hi i will give you a thumbs up if you do this problem correctly. Sorting Analysis Code and Essay Due: 4/22/2019(Monday) Introduction And now for something completely different.   Different sorting algorithms are better for different size data sets.   Other sorting algorithms are better for data sets of a specific type – for instance, data that is already ordered. In this assignment you will implement four different sorting algorithms and collect statistics for each of those algorithms while sorting multiple different...

  • Can someone please help, third time I'm asking. I need a basic javascript with no push...

    Can someone please help, third time I'm asking. I need a basic javascript with no push or splice command. Please don't post a picture of the code,because my vision is poor and I won't be able to see it. Also, I need breakdown of what's in HTMLand what' s in javascript. All requirements are below. Thanks for your help. This is a 2 part assignment, but both parts can be completed in one program. Also, please follow ALL Required Programming...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT