Hi,
I was writing insertion sort in c++.
However the output is very strange.
input:79261458
output: 11111458
Following is the code.
Could someone help to point out the error?
Thank you!
I was writing insertion sort in c++.
However the output is very strange.
input:79261458
output: 11111458
Following is the code.
Could someone help to point out the error?
Thank you!
Code:
#include <iostream>
using namespace std;
void insertionSort(int* input, int size)
{
for(int j = 1; j <size; j++){
int value = *(input+j);
for(int k = 0; k < j; k++){
if(*(input+k) > value){
for(int l = j; l > k; l--){
*(input+l) = *(input+l-l);
}
*(input+k) = value;
}
}
}
}
int main()
{
int input[] = {7,9,2,6,1,4,5,8};
insertionSort(input, 8);
for(int i = 0; i <8; i++)
cout<< *(input+i);
cout << endl;
system("pause");
return 0;
}
Comment