冒泡和選擇排序

1、冒泡排序
    1)複雜度 時間O(n^2) 穩定
    2)程序實現
    void Maopao(type a[],int n)
        {
            int i,j;
            type temp;
            for(i=1;i<n;j++)//排序
            {
                for(j=0;j<10-i;j++)
                {
                    if(a[j]>a[j+1])
                    {
                        temp = a[j];
                        a[j] = a[j+1];
                        a[j+1] = temp;
                    }
                }
            }
            //輸出
             for(i=0;i<n;i++)
                printf("%d ",a[i]);
            printf("\n");
        }
2、選擇排序
    1)複雜度 時間O(n^2) 穩定
    2)程序實現
        void SelectSort(type a[],int len)
        {
            type temp;
            int nIndex = 0;
            int i,j;
            for(i=0;i<len-1;i++)
            {
                nIndex=i;
                for(j=i+1;j<len;j++)
                {
                    if(a[j]<a[nIndex])
                    {
                        nIndex = j;
                    }
                }
                if(nIndex != i)
                {
                    temp = a[i];
                    a[i]= a[nIndex];
                    a[nIndex]=temp;
                }
            }
        }排序

相關文章
相關標籤/搜索