Ask Question
Today, 10:00

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

+2
Answers (1)
  1. Today, 13:37
    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?
Get an answer to your question ✅ “Write a C+ + program that usesInsertion Sort to sort an unsorted list of numbers. ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers