Reputation: 39
#include <stdio.h>
int main(){
int array[] ={4, 5, 3, 1,6,2,6,10,22,10,10};
for(int i = 0; i <= 10; i++){
for (int j = 0; j <= 9; j++){
printf("Array a[i] is %d\n", array[i]);
if(array[i] < array[j]){
// Swap
int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
printf("Array a[i,j] is %d %d\n", array[i],array[j]);
}
}
for (int i = 0; i <= 10; i++){
printf("%d", array[i]);
}
}
Could anyone check whether this sorting algorithm correct or not, because it seems to me that it does work. If it is correct, could you tell the name of this sorting algorithm please? Thank you!
I have tried to compile this code with different given output and it seems to me that it work just fine!
Upvotes: 0
Views: 52
Reputation: 656
This looks to be a correct, effective, but inefficient version of insertion sort. Typically with insertion sort, the inner loop won't start at 0 every time, it'll start at i + 1
since you know everything before index i
should already be sorted.
Upvotes: 2