#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!
1条答案
按热度按时间tuwxkamq1#
这看起来是insertion sort的一个正确的、有效的版本,但是效率不高。通常,对于插入排序,内部循环不会每次都从0开始,它将从
i + 1
开始,因为在索引i
应该已经排序之前,你已经知道了所有的事情。