Code:
/*
插入排序
INSERTION-SORT(A)
for j = 2 to A.length
key = A[j]
//Insert A[j] into the sorted sequence A[1..j-1].
while i > 0 and A[i] > key
A[i+1] = A[i]
i = i - 1
A[i+1] = key
*/
/*
两个N位二进制数相加伪代码
flag = 0
for j=1 to n
do key = A[j] + B[j] + flag
C[j] = key mod 2
if key > 1
flag = 1
if flag = 1
C[n+1] = 1
*/
#include <iostream>
#include <string.h>
using namespace std;
int main(){
int A[10];
int i,j,key;
for(i=0;i<10;i++){
cin>>A[i];
}
/*
升序排序
for(j=1;j<sizeof(A)/sizeof(A[0]);j++){
key = A[j];
i = j-1;
while(i>=0 && A[i]>key)
{
A[i+1] = A[i];
i = i-1;
}
A[i+1] = key;
}
*/
//降序排序
for(j=1;j<sizeof(A)/sizeof(A[0]);j++){
key = A[j];
i = j-1;
while(i >= 0 && A[i] < key)
{
A[i+1] = A[i];
i = i-1;
}
A[i+1] = key;
}
/**
线性查找问题
for i = 1 to A.length
if A[i] = v
return i
return NIL
*/
cout<<endl;
for(i=0;i<10;i++){
cout<<A[i]<<" ";
}
return 0;
}
时间复杂度为 O(n^2) 最快时间复杂度为 O(n)