Download
INSERTION SORTING Presentation Transcript:
1.Design and Analysis of Algorithms
2.An Example: Insertion Sort
Our first algorithm is insertion sort
Input : A sequence of n numbers
Output : A permutation (reordering) of the input sequence such that
3.An Example: Insertion Sort
4.InsertionSort(A)
1 for j ? 2 to length[A] 2 do 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[i] > key) 6 do A[i+1] ? A[i] 7 i ? i - 1 8 A[i+1] ? key
5.InsertionSort(A)
1 for j ? 2 to length[A] 2 do 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[i] > key) 6 do A[i+1] ? A[i] 7 i ? i - 1 8 A[i+1] ? key
6.InsertionSort(A)
1 for j ? 2 to length[A] 2 do 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[i] > key) 6 do A[i+1] ? A[i] 7 i ? i - 1 8 A[i+1] ? key
7.InsertionSort(A)
1 for j ? 2 to length[A] 2 do 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[i] > key) 6 do A[i+1] ? A[i] 7 i ? i - 1 8 A[i+1] ? key
8.An Example: Insertion Sort
InsertionSort(A)
1 for j ? 2 to length[A] 2 do 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[i] > key) 6 do A[i+1] ? A[i] 7 i ? i - 1 8 A[i+1] ? key
9.InsertionSort(A)
1 for j ? 2 to length[A] 2 do 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[i] > key) 6 do A[i+1] ? A[i] 7 i ? i - 1 8 A[i+1] ? key
10.InsertionSort(A)
1 for j ? 2 to length[A] 2 do 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[i] > key) 6 do A[i+1] ? A[i] 7 i ? i - 1 8 A[i+1] ? key
No comments:
Post a Comment