数据结构算法分析

c++实现,教材源自清华大学邓俊辉版,本节内容为绪论里的算法分析。

Posted by LJJ on April 23, 2018

算法分析

  • 级数

G4tbTI.png

G4tz6g.png

  • 迭代

G4UqRf.png

G4aQW6.png

G4atwd.png

  • 正确性

看一个冒泡排序:

#include <iostream>

using namespace std;

void bubbleSort(int a[],int n){
    for(bool sorted=false ;sorted=!sorted;n--){
        for(int i=1;i<n;i++){
            if(a[i-1]>a[i]){
                swap(a[i-1],a[i]);
                sorted = false;
            }
        }
    }
}


int main()
{
    int A[15]={1,4,6,8,7,3,2,1,2,2,9};
    for(int j=0;j<11;j++)
        cout << A[j] <<",";
    bubbleSort(A,11);

    cout << "" << endl;
    for(int j=0;j<11;j++)
        cout << A[j] <<",";
    cout << "" << endl;

    cout << "排序结束!" << endl;
    return 0;
}

输出:

G4DgJA.png

它满足以下条件:

G4gkq0.png

说明该算法是正确的

  • 封底估算

G4jqhQ.png