You can print the contents of the array at the end of each iteration in the for
loop to diagnose where the error is in your code.
#include <iostream>
using namespace std;
template <int N>
void print(int (&arr)[N])
{
for(int var =0; var<N; var++)
{
cout << arr[var] << " ";
}
cout << endl;
}
int main()
{
int A[6]={51,2,4,6,1,3};
int temp;
int j;
int length = 6;
for(j=2;j<length;j++)
{
int key;
key = A[j];
int i;
i=j-1;
while(i>=0 && A[i]>key)
{
temp = A[i+1];
A[i+1]=A[i];
A[i]=temp;
i=i-1;
}
A[i+1]=key;
cout << endl << "Final result:" << endl;
print(A);
}
print(A);
return 0;
}
Output:
51 2 4 6 1 3
51 2 4 6 1 3
1 51 2 4 6 3
1 51 2 3 4 6
Final result:
1 51 2 3 4 6
I saw the following errors in your logic.
Your for
loop needs to start with j=1
, not j=2
. Remember that the index 1 is used to access the second element of the array, not the first element of the array.
for(j=1;j<length;j++)
{
...
}
The conditional of the while
is not correct. It breaks the loop too early. The iteration of the elements and the swapping of the elements need to be separated.
while(i>=0)
{
if ( A[i]>A[i+1] )
{
temp = A[i+1];
A[i+1]=A[i];
A[i]=temp;
}
i=i-1;
}
I did not follow the reason behind the existence of the variable key
. I think it can be removed. YMMV.
Here's an updated version of the program and its output.
#include <iostream>
using namespace std;
template <int N>
void print(int (&arr)[N])
{
for(int var =0; var<N; var++)
{
cout << arr[var] << " ";
}
cout << endl;
}
int main()
{
int A[6]={51,2,4,6,1,3};
int temp;
int j;
int length = 6;
for(j=1;j<length;j++)
{
int i;
i=j-1;
while(i>=0)
{
if ( A[i]>A[i+1] )
{
temp = A[i+1];
A[i+1]=A[i];
A[i]=temp;
}
i=i-1;
}
print(A);
}
cout << endl << "Final result:" << endl;
print(A);
return 0;
}
Output:
2 51 4 6 1 3
2 4 51 6 1 3
2 4 6 51 1 3
1 2 4 6 51 3
1 2 3 4 6 51
Final result:
1 2 3 4 6 51