Answer to Question #210652 in C++ for Usman Ali

Question #210652

For the following data set {25, 74, 45, 65, 17, 31, 64, 50}, Perform insertion sort and write output of each iteration


1
Expert's answer
2021-06-27T14:10:39-0400


#include <iostream>
using namespace std;
 


void insertion_Sort(int array[], int arr_size)
{
    int n, item, k;
    for (n = 1; n <arr_size; n++)
    {
       item = array[n];
        k = n - 1;
 
        
        while (k >= 0 && array[k] > item)
        {
            array[k + 1] = array[k];
            k = k - 1;
        }
        array[k + 1] = item;
    }
}
 


void display(int array[], int arr_size)
{
    int x;
    for (x = 0; x < arr_size; x++)
        cout << array[x] << " ";
    cout << endl;
}
 


int main()
{
    int array[] = {25, 74, 45, 65, 17, 31, 64, 50};
    int array_size= sizeof(array) / sizeof(array[0]);
 
    insertion_Sort(array, array_size);
    display(array, array_size);
 
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS