Double Insertion Sort is a variation on Insertion Sort that

Double Insertion Sort is a variation on Insertion Sort that works from the middle of the array out. At each iteration, some middle portion of the array is sorted. On the next iteration, take the two adjacent elements to the sorted portion of the array. If they are out of order with respect to each other, then swap them. Now, push the left element toward the right in the array so long as it is greater than the element to its right. And push the right element toward the left in the array so long as it is less than the element to its left. The algorithm begins by processing the middle two elements of the array if the array is even. If the array is odd, then skip processing the middle item and begin with processing the elements to its immediate left and right. First, explain what the cost of Double Insertion Sort will be in comparison to standard Insertion sort, and why. (Note that the two elements being processed in the current iteration, once initially swapped to be sorted with respect to each other, cannot cross as they are pushed into sorted position.) Then, implement Double Insertion Sort, being careful to properly handle both when the array is odd and when it is even. Compare its running time in practice against standard Insertion Sort.

Solution

1)The time complexity for the Double Insertion Sort is

Worst case O(n);

Best Case O(n);

Average Case O(n);

2)Executed in Dev C++

#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system(\"pause\") or input loop */

int main(int argc, char** argv) {
   int arr[20],i,j,n,k,mid,temp;
   cout << \"\\t\\tWelcome to Double Insertion Sort\ \";
   cout << \"Enter size of array:\\t\";
   cin >> n;
   cout << \"Enter array elements:\ \";
   for(i=0;i<n;i++){
       cin >> arr[i];
   }
   mid = n/2;
   cout <<\"\\t\"<< mid << \"\ \" ;
   if(n % 2 == 0){
       cout << \"EVEN array :\ \";
       for(k=0;k<n;k++){          
               for(i=mid-1;i>=0;i--){
                   for(j=mid;j<n;j++){
                       cout << i << \"\\t\" << j << \"\ \" ;
                       if(arr[i] >= arr[j]){
                           temp=arr[i];
                           arr[i] = arr[j];
                           arr[j] = temp;
                           break;
                       }
                       else{
                           break;
                       }
                      
                   }
                   mid ++;
               }
       }
   }
   else{
       cout << \"ODD array:\ \";
       //for(k=0;k<n;k++){
           for(i=mid-1;i>=0;i--){
               for(j=mid+1;j<n;j++){
                   cout << i << \"\\t\" << j << \"\ \" ;
                   if(arr[i] >= arr[j]){
                       temp=arr[i];
                       arr[i] = arr[j];
                       arr[j] = temp;
                       break;
                   }
                   else{
                       break;
                   }
               }
               mid++;
           }
       //}
   }
   cout << \"Array after sort:\ \";
   for(i=0;i<n;i++){
       cout << arr[i] << \"\\t\" ;
   }
   return 0;
}

 Double Insertion Sort is a variation on Insertion Sort that works from the middle of the array out. At each iteration, some middle portion of the array is sort
 Double Insertion Sort is a variation on Insertion Sort that works from the middle of the array out. At each iteration, some middle portion of the array is sort

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site