插入排序
算法导论课后练习题,本人非科班出生,也未有人指导,纯属自学,难免有错误,恳请指导!
_____________________________________________
INSERTION-SORT(A)
for j = 2 to A.length
key = A[j]
i = j-1
while i > 0 and A[i] > key
A[i+1] = A[i]
i = i - 1
A[i + 1] = key
---------insertion_sort.h------------------------
void insertion_sort(double array[], int length)
{
int i, j;
int tmp;
if (length < 2)
return;
for (j = 1; j < length; j++)
{
tmp = array[j];
i = j-1;
while(i >= 0 && array[i] > tmp)
{
array[i + 1] = array[i];
i--;
}
array[i + 1] = tmp;
}
}
---------------main.c-------------------------------
#include
#include "insertion_sort.h"
int main(void)
{
printf("插入排序测试\n");
double arr[6] = {
31, 41, 59, 26, 41, 58};
printf("array=");
for (int i = 0; i < 6; i++)
{
printf("%lf ", arr[i]);
}
printf("\nsorted array=");
insertion_sort(arr, 6);
for (int i = 0; i < 6; i++)
{
printf("%lf ", arr[i]);
}
printf("\n........................................\n");
return 0;
}
------------------------------------------------------------------------------------
重写过程INSERTION_SORT,使之按非升序排序
INSERTION_SORT_DOWN(A)
for j = 1 to A.length
key = A[j]
i = j - 1
while i > 0 and A[i] < key
A[i + 1] = A[i]
i = i - 1
A[i + 1] = key
_________________________________________________________
LINEAR_SEARCH(A, *v)
for i = 1 to A.length
if *v = A[i]
return i;
*v = NIL
证明-
初始化:第一次循环迭代之前,即j=1时,如果找到v的值,则返回该值在序列A中的索引,循环结束
保持:每次迭代未找到该值,索引向后推进一个单位
终止:当找到该值,立即返回该值在序列中的索引,终止。若未找到该值,for结束,该值置为NIL
——————————————————————————————————————————————
BINARY_ADD(A, B, C)
tmp = 0
for i = 1 to A.length
if A[i] != B[i]
C[i] = 1 + tmp
tmp = 0
else if A[i] == 1 && B[i] == 1
C[i] = 0 + tmp
tmp = 1
else
C[i] = 0 + tmp
tmp = 0
C[i+1] = tmp
_____________________________________________
INSERTION-SORT(A)
for j = 2 to A.length
key = A[j]
i = j-1
while i > 0 and A[i] > key
A[i+1] = A[i]
i = i - 1
A[i + 1] = key
---------insertion_sort.h------------------------
void insertion_sort(double array[], int length)
{
int i, j;
int tmp;
if (length < 2)
return;
for (j = 1; j < length; j++)
{
tmp = array[j];
i = j-1;
while(i >= 0 && array[i] > tmp)
{
array[i + 1] = array[i];
i--;
}
array[i + 1] = tmp;
}
}
---------------main.c-------------------------------
#include
#include "insertion_sort.h"
int main(void)
{
printf("插入排序测试\n");
double arr[6] = {
31, 41, 59, 26, 41, 58};
printf("array=");
for (int i = 0; i < 6; i++)
{
printf("%lf ", arr[i]);
}
printf("\nsorted array=");
insertion_sort(arr, 6);
for (int i = 0; i < 6; i++)
{
printf("%lf ", arr[i]);
}
printf("\n........................................\n");
return 0;
}
------------------------------------------------------------------------------------
重写过程INSERTION_SORT,使之按非升序排序
INSERTION_SORT_DOWN(A)
for j = 1 to A.length
key = A[j]
i = j - 1
while i > 0 and A[i] < key
A[i + 1] = A[i]
i = i - 1
A[i + 1] = key
_________________________________________________________
LINEAR_SEARCH(A, *v)
for i = 1 to A.length
if *v = A[i]
return i;
*v = NIL
证明-
初始化:第一次循环迭代之前,即j=1时,如果找到v的值,则返回该值在序列A中的索引,循环结束
保持:每次迭代未找到该值,索引向后推进一个单位
终止:当找到该值,立即返回该值在序列中的索引,终止。若未找到该值,for结束,该值置为NIL
——————————————————————————————————————————————
BINARY_ADD(A, B, C)
tmp = 0
for i = 1 to A.length
if A[i] != B[i]
C[i] = 1 + tmp
tmp = 0
else if A[i] == 1 && B[i] == 1
C[i] = 0 + tmp
tmp = 1
else
C[i] = 0 + tmp
tmp = 0
C[i+1] = tmp