INSERTTION SORT
According to Introduce to Algorithms, I coded a small program to solve Insertion sort problem! I present to you below:
Firstly, Let's see what the problem is?
Input: A sequence of n number <n1, n2,..n>
Output: A reordering <a'1, a'2,...a'n> of the input sequence such as a'1 < a'2 < a'3 <.... < a'n.Next, What does pseudocode illustrate?
Trying to analyze it...
Let "j" from 2 to length of A, "j" start at 2 caused by "j" is index of wrong array A, we set the index is 2 as a key to compare with the others.
Call a variable "i" as explorer. It'll point to address of each member in array to find which is smaller from "j-1" to "1". If the previous char is smaller than the posterior one, they will be exchange each other. After that, set the next wrong of array to be a key and continue! Easy.
This is my code in C programming language :
06:33
0 nhận xét:
Đăng nhận xét