Question

In: Computer Science

write a program in C language Create a function to perform the insertion sort. Now the...

write a program in C language

Create a function to perform the insertion sort. Now the program will perform the following steps:

  1. Prompt the user to enter the number of array elements (say, N).
  2. Read the number of elements (N).
  3. Use dynamic memory allocation to allocate an array of N single precision floating-point elements (C type float).
  4. Read the N single precision floating-points elements to the allocated array.
  5. Invoke a function to sort the array using insertion sort (the insertion sort function should take the array reference and number of elements as the arguments and sort the array in-place). The insertion sort function should have the return type as void, i.e., it does not return anything.
  6. Print the sorted array.

Code snippet:

1. Function Declaration

void displayArray(float *arr, int size); 
void sortArray(float *arr, int size); 

2. Function definition

void displayArray(float *arr, int size){ 
    printf("["); 
    for (int i=0; i<size-1; i++) { 
            printf("%f, ",arr[i]); 
    } 
    printf("%f]\n", arr[size-1]);      
} 

3. Function call/invocation

displayArray(arr, N); 
sortArray(arr, N); 

4. Dynamic memory allocation for array

float *arr = (float*) malloc(N * sizeof(float)); 

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <stdio.h>

#include <stdlib.h>

void displayArray(float *arr, int size);

void sortArray(float *arr, int size);

int main()

{

    int N, i;

    float *arr = NULL;

    printf("Enter the number of array elements: ");

    scanf("%d", &N);

    arr = (float *)malloc(N * sizeof(float));

    printf("Enter %d elements: ", N);

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

    {

        scanf("%f", &arr[i]);

    }

    printf("Entered elements are: ");

    displayArray(arr, N);

    sortArray(arr, N);

    printf("After sorting: ");

    displayArray(arr, N);

    return 0;

}

void displayArray(float *arr, int size)

{

    printf("[");

    for (int i = 0; i < size - 1; i++)

    {

        printf("%.1f, ", arr[i]);

    }

    printf("%.1f]\n", arr[size - 1]);

}

void sortArray(float *arr, int size)

{

    int i, j;

    float temp;

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

    {

        temp = arr[i];

        j = i - 1;

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

        {

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

            j = j - 1;

        }

        arr[j + 1] = temp;

    }

}


Related Solutions

In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football...
In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football League list of current players. It's going to be a big list(over 1000 names). Please identify, in comments, which part of the code is for the Selection Sort and which part of the code is for Insertion Sort. It must have both. Inputting data from a text file named "Players.txt" Only want the name of the player(first name then last name), their team name,...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
1a .Write a program that perform insertion sort (ascending order) on an input array and print...
1a .Write a program that perform insertion sort (ascending order) on an input array and print the total number of comparisons that have been made. You can implement by using any programming languages such as C/C++. For example, in the case of array B = [ 30 , 10 , 20 , 40 ], there are 4 needed comparisons to perform insertion sort (30 vs 10, 20 vs 30, 20 vs 10, and 40 vs 30). 1b. Write a program...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
c++ For your program, you will choose either the Selection Sort algorithm or the Insertion Sort...
c++ For your program, you will choose either the Selection Sort algorithm or the Insertion Sort algorithm and create a recursive implementation of that algorithm. Your program should: Randomly generate an array of at least 20 values. Display the contents of that (unsorted) array. Use the recursive implementation of either Selection or Insertion Sort to sort the values. Display the contents of the now sorted array, to demonstrate the success of the algorithm.
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that sorts the array below in ascending order.  LISP is a recursive language so the program will use recursion to sort. Since there will be no loops, you will not need the variables i, j, and temp, but still use the variable name array for the array to be sorted.             Array to be sorted is 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT