Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

insert sort

$
0
0
The following recursive algorithm for insert sort is faulty. Can anyone help me to figure out the mistake?
//precondition:Array is sorted till (n-1)th element or index n-2.
//postcondition:array is sorted till Nth element.
void insertSort(int A[],int n)
{
if(n==1)
return;

int x=A[n-1];

int i;
for(i=n-2;i>=0;i--)
{
if(A[n-1]<A[i])
break;
}

int j;
if(i!=-1)
for(j=n-2;j>=i;j--)
A[j+1]=A[j];


A[i]=x;
}

void insertSortR(int A[],int n)
{
if(n>1)
insertSortR(A,n-1);

insertSort(A,n);
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>