Ask Question
11 October, 19:10

Write a C+ + program that usesInsertion Sort to sort an unsorted list of numbers.

+2
Answers (1)
  1. 11 October, 21:28
    0
    C+ + program - Insertion sort

    #include

    using namespace std;

    / * Defining function for sorting numbers*/

    void insertionSort (int array[], int n)

    {

    int i, k, a;

    for (i=1; i
    {

    k=array[i];

    a=i-1;

    while (a>=0 && array[a] > k) / / moving elements of array[0 to i-1] are greater than k, to one position / /

    {

    array[a+1] = array[a];

    a = a-1;

    }

    array[a+1] = k;

    }

    }

    / * Driver function * /

    int main ()

    {

    int array[] = { 12,56,76,43,21}; / /input integers

    int n = sizeof (array) / sizeof (array[0]); / /finding size of array

    insertionSort (array, n); / /Calling function

    for (int i = 0; i < n; i++) / /printing sorted array

    cout << array[i] << " ";

    cout << endl;

    return 0;

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a C+ + program that usesInsertion Sort to sort an unsorted list of numbers. ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers