直接插入排序

来源:互联网 发布:e博乐网络 编辑:程序博客网 时间:2024/06/05 14:10

时间复杂度:

    最好情况: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);}

这里写图片描述

0 0
原创粉丝点击