Q:

Write A Program To Insertion Sort Using Dynamic Array

0

Write A Program To Insertion Sort Using Dynamic Array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
 int *a,i,s,j,temp;
 
 cout<<"ENTER THE SIZE OF ARRAY : \n"<<endl;
 cin>>s;
 
 a=(int*)malloc(s*sizeof(int));
 
 cout<<"ENTER THE NUMBER\n"<<endl;
 for(i=0;i<s;i++)
 {
  cout<<"ENTER THE ELEMENT "<<i+1<<" :";
  cin>>a[i];
 }
 
 for(i=1;i<s;++i)
  for(j=i;j>=1;--j)
   if(a[j]<a[j-1])
   {
    temp = a[j];
    a[j] = a[j-1];
    a[j-1] = temp;
   }
  cout<<"\nSORTED ARRAY IN ACCENDING ORDER :\n\n"<<endl;
  for(i=0;i<s;i++) 
  cout<<a[i]<<" ";
}

 

Output:

ENTER THE SIZE OF ARRAY : 

 

5

ENTER THE NUMBER

 

ENTER THE ELEMENT 1 :12

ENTER THE ELEMENT 2 :32

ENTER THE ELEMENT 3 :569

ENTER THE ELEMENT 4 :456

ENTER THE ELEMENT 5 :25

 

SORTED ARRAY IN ACCENDING ORDER :

12 25 32 456 569 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now