Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Searching & Sorting (Answer each question seperately) 5. Sort the following

ID: 3930018 • Letter: C

Question

C++ Searching & Sorting

(Answer each question seperately)

5. Sort the following list using the selection sort algorithm. Show the list after each iteration of the outerforloop.
36, 55, 17, 35, 63, 85, 12, 48, 3, 66

6. Consider the following list: 5, 18, 21, 10, 55, 20
The first three keys are in order. To move 10 to its proper position using the insertion sort as described in this chapter, exactly how many key comparisons are executed?

7. Consider the following list: 7, 28, 31, 40, 5, 20
The first four keys are in order. To move 5 to its proper position using the insertion sort as described in this chapter, exactly how many key comparisons are executed?

8. Consider the following list: 28, 18, 21, 10, 25, 30, 12, 71, 32, 58, 15
This list is to be sorted using the insertion sort algorithm. Show the resulting list after six

passes of the sorting phase – that is, after six iterations of the for loop.

Explanation / Answer

#include <iostream>
using namespace std;
void print(int a[],int n)
{
   for(int i=0;i<n;i++)
   {
       if(i>0)
       {
           cout<<',';
       }
       cout<<a[i];
   }
   cout<<endl;
}
int main()
{
   int a[] = {36, 55, 17, 35, 63, 85, 12, 48, 3, 66};
   int len = 10,min=0,temp;
   for(int i=0;i<len;i++)
   {
       min = i;
       for(int j=i+1;j<len;j++)
       {
           if(a[j]<a[min])
           {
               min=j;
           }
       }
       temp = a[min];
       a[min] = a[i];
       a[i] = temp;
       print(a,len);
   }

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote