《挑战程序设计竞赛》阅读笔记一 之 ALDS1_1_A Insertion Sort

来源:互联网 发布:圣安德鲁斯大学cs知乎 编辑:程序博客网 时间:2024/06/05 09:47

《挑战程序设计竞赛》阅读笔记一 之 ALDS1_1_A Insertion Sort

第二章

ALDS1_1_A Insertion Sort

这个没有什么好说的,非常简单的插入排序

#include <iostream>using namespace std;int main() {    int a[105];    int n;    cin>>n;    for(int i=0;i<n;i++){        cin>>a[i];    }    cout<<a[0];    for(int i=1;i<n;i++){        cout<<" "<<a[i];    }    cout<<endl;    for(int i=1;i<n;i++){        int temp=a[i];        int j=i-1;        while (j>=0 && a[j]>temp){            a[j+1]=a[j];            j--;        }        a[j+1]=temp;        cout<<a[0];        for(int i=1;i<n;i++){            cout<<" "<<a[i];        }        cout<<endl;    }    return 0;}

代码随手写的,函数没有写到main外面,这个不太好。

阅读全文
0 0
原创粉丝点击