Professional Documents
Culture Documents
Insertion Sort 1
Insertion Sort 1
j = -1 -1 >= 0 a[ 0]= 0
Void insertion _sort (int a[ ] , int n)
{
int i , j, temp ; 0 1 2 3 4 5 6
for( i = 1; i < n ; i++)
{
0 5 1 9 2 6 4
temp =a [i] ;
for ( j = i – 1 ; j >= 0 && a[j] > temp; j--)
0 1 5 9 2 6 4
{
a[j+1] = a[j] ;
}
a[j+1] = temp;
} Outer for loop i=2 i<n
} N=7 2 2<7
temp =a[ i ] temp = 1