Chapter 2 Getting Started 2.1 Insertion sort
6408 단어 algorithmsanddatastructures
Chapter 2 Getting Started
2.1 Insertion sort
insertion sort is an efficient algorithm for sorting a small number of elements.
INSERTION-SORT(A)
1 for j = 2 to A.length
2 key = A[j]
3 // Insert A[j] into the sorted sequence A[1~j-1].
4 i = j - 1
5 while i > 0 and A[j] > key
6 A[i+1] = A[i]
7 i = i - 1
8 A[i+1] = key
C:
for(i=1;i<n;i++)
{
key=a[i];
j=i-1;
while(j>=0&&key>a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=key;
}
The pseudocode just looks like this :
SEARCH(A, v):
for i = 1 to A.length
if A[i] == v
return i
return NIL
I’m going to state the loop invariant as:
At the start of each iteration of the for loop, the subarray A[1..i−1] consists of elements that are different than ν.
Here are the three properties:
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
하나 추가문제 설명 정수 배열 digits으로 표시되는 큰 정수가 제공됩니다. 큰 정수에는 선행 0이 포함되어 있지 않습니다. 설명 題目 題目 理解 理解 理解 理解 考量 考量 簡單 是 進位 進位 進位 進位 進位 也 很 很 簡...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.