直接插入排序

    xiaoxiao2025-06-11  37

    时间复杂度:

    最好情况:O(n) 最坏情况:O( n*n ) 平均情况:O(n*n)

    空间复杂度:

    O(1)

    稳定性:

    稳定 #include<stdio.h> void printArray(int a[], int size) //打印数组元素 { int i; for(i = 0; i < size; i++) printf("%d ",a[i]); printf("\n"); } void bubble_sort(int a[], int size){ int i,j,temp; for(i = 1;i<size;i++) //默认a[0]是有序的 { if(a[i-1]>a[i]) //从右向左找大于a[i]的元素 temp = a[i]; for(j = i - 1;a[j] > temp;j--) //将大于temp的元素后移 a[j+1] = a[j]; a[j+1] = temp; //在j+1处插入temp printArray(a,size); } } int main() { int count=10,a[10]={9,1,2,3,4,5,6,7,8,0}; bubble_sort(a,count); }

    转载请注明原文地址: https://ju.6miu.com/read-1299831.html
    最新回复(0)