数组-PTA-类似二分法解决有序数组的插入

高等教育出版社教材-浙大版《数据结构(第2版)》题目集。

有序数组的插入

本题要求将任一给定元素插入从大到小排好序的数组中合适的位置,以保持结果依然有序。

函数接口定义:

bool Insert( List L, ElementType X );

其中List结构定义如下:

typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* 保存线性表中最后一个元素的位置 */
};

L是用户传入的一个线性表,其中ElementType元素可以通过>、==、<进行比较,并且题目保证传入的数据是递减有序的。函数Insert要将X插入Data[]中合适的位置,以保持结果依然有序(注意:元素从下标0开始存储)。但如果X已经在Data[]中了,就不要插入,返回失败的标记false;如果插入成功,则返回true。另外,因为Data[]中最多只能存MAXSIZE个元素,所以如果插入新元素之前已经满了,也不要插入,而是返回失败的标记false

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10
typedef enum {false, true} bool;
typedef int ElementType;

typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* 保存线性表中最后一个元素的位置 */
};

List ReadInput(); /* 裁判实现,细节不表。元素从下标0开始存储 */
void PrintList( List L ); /* 裁判实现,细节不表 */
bool Insert( List L, ElementType X );

int main()
{
    List L;
    ElementType X;

    L = ReadInput();
    scanf("%d", &X);
    if ( Insert( L, X ) == false )
        printf("Insertion failed.\n");
    PrintList( L );

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例1:

5
35 12 8 7 3
10

输出样例1:

35 12 10 8 7 3
Last = 5

输入样例2:

6
35 12 10 8 7 3
8

输出样例2:

Insertion failed.
35 12 10 8 7 3
Last = 5

啊,关于此题,想说三点:
① “二分插入”的方法由二分查找演变而来,原理是先进行二分查找,直至查找区间只有两个元素,就把目标元素插入它们中间。
② 这种插入法有一处需要注意。请比较如下代码:

	if (begin + 1 == end) {
		if (L->Data[begin] == X || L->Data[end] == X)
			return false;
		else return DoInsert(L, X, begin + 1);
	}
	if (L->Data[mid] > X) return InnerInsert(L, mid + 1, end, X);
	if (L->Data[mid] < X) return InnerInsert(L, begin, mid - 1, X);

和如下代码:

	if (begin + 1 == end) {
		if (L->Data[begin] == X || L->Data[end] == X)
			return false;
		else return DoInsert(L, X, begin + 1);
	}
	if (L->Data[mid] > X) return InnerInsert(L, mid, end, X);
	if (L->Data[mid] < X) return InnerInsert(L, begin, mid, X);

第一段是由二分查找无脑改来的。值得注意的是第一种方法将无法把目标元素插入mid和mid+1(mid和mid-1)之间。换句话说,我们只能把元素插入至给定序列的内部,而首尾是做不到的。由此必须使用第二段代码。
这样可以解决一部分问题,但是我们能类似地将begin-1和end+1作为查找序列进行查找吗?这可能会导致溢出问题。所以我们直接在最开始时就判断目标元素是否将被放至首尾:

bool Insert(List L, ElementType X)
{
	if (L->Last - 0 + 1 >= MAXSIZE)return false;
	if (X > L->Data[0])		 return DoInsert(L, X, 0);
	if (X < L->Data[L->Last])return DoInsert(L, X, L->Last + 1);
	return InnerInsert(L, 0, L->Last, X);
}

这样一来,与其说我们写的是“二分插入”,不如说是“内部二分插入”,因为它只能解决内部插入问题。故名为InnerInsert


bool InnerInsert(List L, Position begin, Position end, ElementType X)
{
	int mid = (begin + end) / 2;
	if (begin > end)return false;
	if (L->Data[mid] == X)return false;
	
	if (begin + 1 == end) {
		if (L->Data[begin] == X || L->Data[end] == X)
			return false;
		else return DoInsert(L, X, begin + 1);
	}
	if (L->Data[mid] > X) return InnerInsert(L, mid, end, X);
	if (L->Data[mid] < X) return InnerInsert(L, begin, mid, X);
}

至于“外部二分插入”的解决,我们留给了总体的二分插入函数Insert.

③ 这一条是普适经验:当函数的return出口非常多时,建议用翁恺在mooc中提到的“唯一出口”原则,因为不小心真的会漏掉哪里的return而且半天都发现不了!

My answer:

bool InnerInsert(List L, Position begin, Position end, ElementType X);
bool DoInsert(List L, ElementType X, Position index);
bool Insert(List L, ElementType X)
{
	if (L->Last - 0 + 1 >= MAXSIZE)return false;
	if (X > L->Data[0])		 return DoInsert(L, X, 0);
	if (X < L->Data[L->Last])return DoInsert(L, X, L->Last + 1);
	return InnerInsert(L, 0, L->Last, X);
}

bool InnerInsert(List L, Position begin, Position end, ElementType X)
{
	if (begin > end)return false;
	
	int mid = (begin + end) / 2;
	if (L->Data[mid] == X)return false;
	if (begin + 1 == end) {
		if (L->Data[begin] == X || L->Data[end] == X)
			return false;
		else return DoInsert(L, X, begin + 1);
	}
	if (L->Data[mid] > X) return InnerInsert(L, mid, end, X);
	if (L->Data[mid] < X) return InnerInsert(L, begin, mid, X);
}

bool DoInsert(List L, ElementType X, Position index)
{
	int i;
	for (i = MAXSIZE - 1; i >= index + 1; i--) {
		L->Data[i] = L->Data[i - 1];
	}
	L->Data[index] = X;
	L->Last++;
	return true;
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值