Here is the pseudo code for arranging number in non increasing format using Insertion sort. This is one of the exercise of Book Introduction to Algorithms by CLRS.
//N is array's length
for j = N-1 to 1
key = A[j];
i = j + 1;
while i<=N && A[i] > key
A[i-1] = A[i];
i = i +1;
A[i-1] = key;
Input : 5 4 3 2 6
Output : 5 4 3 6 2
5 4 6 3 2
5 6 4 3 2
6 5 4 3 2 [Final]
//N is array's length
for j = N-1 to 1
key = A[j];
i = j + 1;
while i<=N && A[i] > key
A[i-1] = A[i];
i = i +1;
A[i-1] = key;
Input : 5 4 3 2 6
Output : 5 4 3 6 2
5 4 6 3 2
5 6 4 3 2
6 5 4 3 2 [Final]
No comments:
Post a Comment