C Searching Sorting Answer each question seperately 5 Sort

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.

Solution

#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;
}

C++ Searching & Sorting (Answer each question seperately) 5. Sort the following list using the selection sort algorithm. Show the list after each iteration

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site