Ask Question

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

+1
Answers (1)
  1. 7 August, 11:11
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers