Reputation: 136
could someone explain why this is not working?
For example, if I have this array: 3 4 1 2 55 32 1111 53
the output is going to be 3 3 3 4 32 53 55 1111.
Thanks!
void insertionSort(int arr[], int len) {
int i, j, tmp;
for (i = 1; i < len; i++) {
tmp = arr[i];
for (j = i - 1; j >= 0; j--) {
if (arr[j] > tmp) {
arr[j + 1] = arr[j];
}
else {
arr[j + 1] = tmp;
break;
}
}
}}
Upvotes: 0
Views: 234
Reputation: 17026
What you posted is not a true translation of the Wikipedia pseudo code.
void insertion_sort(int arr[], const int len) {
for (int i = 1; i < len; ++i) {
int x = arr[i];
int j = i-1;
while((j >= 0)&&(arr[j] > x)) {
arr[j + 1] = arr[j];
j = j-1;
}
arr[j+1] = x;
}
}
Welcome to StackOverlow. Before you post another question, please read all the links about Asking. Pay particular attention to the one about MCVE
Upvotes: 1