算法入门
problem sorting:
Input: n个数
Output: 输入序列的一个排序,为非递减序列
插入排序(Java)
public void InsertionSort(int[] A, int n){
for(int j=2; j<n; j++){
int key = A[j];
int i = j-1;
while(i>=0 && A[i]>key){
A[i+1] = A[i];
i = i-1;
}
A[i+1] = key;
}
}
Running Time :
- Depends on input(e.g. already sorted)
- Depends on input size(6 elements vs 6000000 elements)
Kind of analysis :
Worst-Case( usually ): T(n) = max time on any input of size n;
Average-Case( sometimes ): T(n) = expected time over all inputs of size n; 需要概率分析假设
Best-Case
What is insertionSort’s use time?
Depends on Computer!
- relative speed(on same computer)
- absolute speed(on different computer)
BIG IDEA!