C语言 新的排序算法

rbl8hiat  于 2022-12-02  发布在  其他
关注(0)|答案(1)|浏览(123)
#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!

tuwxkamq

tuwxkamq1#

这看起来是insertion sort的一个正确的、有效的版本,但是效率不高。通常,对于插入排序,内部循环不会每次都从0开始,它将从i + 1开始,因为在索引i应该已经排序之前,你已经知道了所有的事情。

相关问题