Question

[C++ language] Write down the steps how insertion sort for sorting the following elements in an...

  1. [C++ language] Write down the steps how insertion sort for sorting the following elements in an array:

22, 1, 7, -9, 121, 75, 89, 29, 500, 43

I need Steps as an algorithm plus the code and screenshotes

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The following algorithm explains insertion sort:

Algorithm
Insertion Sort

Input: Array a[0 ... n-1] with n elements which needs to be sorted.
Step 1: Read elements in array one by one from left to right, i = 0 to i = n -1
Step 2: Compare the current element(element at index i) with previous element.
Step 3: If the current element is smaller than previous element, then compare it with previous of previous element. Shift the greater elements towards right.
       Repeat Step 3. Until current element is larger than some of its predecessor.
Step 4: Insert the current element towards the right of its predecesor.

-----------------------------------------------------------------

I have included my code and screenshots in this answer. In case, there is any indentation issue due to editor, then please refer to code screenshots to avoid confusion.

------------------main.cpp-----------

#include <iostream>
using namespace std;

void print_Array(int arr[], int size)
{
   for(int i = 0; i < 10; i++)
   {
       cout << arr[i] << ", " ;
   }
   cout << endl;
}

void insertion_sort(int myArr[], int size) //insertion sorts array
{  
   int current_element;
   int prev_index;

   for (int i = 0; i < size; i++)
   {
       current_element = myArr[i];
       prev_index = i-1;
       while (prev_index >= 0 && myArr[prev_index] > current_element)
       {
           myArr[prev_index + 1] = myArr[prev_index]; //Move previous elements towards right if greater than current element
           prev_index = prev_index - 1; //Next compare previous of previous element with current element
       }
       myArr[prev_index + 1] = current_element; //when previous is smaller, insert current_element at the next position
       cout << "\nThe array after Pass "<< i << " \n";
       print_Array(myArr, size); //print array after every pass: for each i
   }
}

int main()
{
   int size = 10;
   int arr[10] = {22, 1, 7, -9, 121, 75, 89, 29, 500, 43}; //declare the array
   cout << "\n The initial array is: \n";
   print_Array(arr, size); //print initial array
  
   insertion_sort(arr, 10); //perform insertion sort

   cout << "\n The sorted array is: \n";
   print_Array(arr, size); //print sorted array
   return 0;
}

------------------Screenshot main.cpp-----------

-/pp/insertion sort/main.cpp - Sublime Text (UNREGISTERED) ti 1) 9:45 AM * main.cpp #include <iostream> using namespace std;

------------------Output-----------

9:44 AM vs@ubuntu:-/pp/insertion sort vs@ubuntu:-/cpp/insertion sorts g++ main.cpp vs@ubuntu:-/cpp/insertion sort$ ./a.out Th

-------------------------------------

I hope this helps you,

Please rate this answer if it helped you,

Thanks for the opportunity

Add a comment
Know the answer?
Add Answer to:
[C++ language] Write down the steps how insertion sort for sorting the following elements in an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT