关于网站建设与维护论文,wordpress上帝模式,wordpress 商业,手机壳定制网站制作1.表插入排序只是求得一个有序的链表#xff0c;它是修改指针的值来代替移动记录#xff0c;操作过程如下 2.但是这样只能进行顺序查找#xff0c;不能进行随机查找#xff0c;为了能实现有序表的折半查找#xff0c;需要对记录进行重新排列。操作过程如下#xff1a; 3.… 1.表插入排序只是求得一个有序的链表它是修改指针的值来代替移动记录操作过程如下 2.但是这样只能进行顺序查找不能进行随机查找为了能实现有序表的折半查找需要对记录进行重新排列。操作过程如下 3.测试程序如下 #includeiostream
#includecstring
#includealgorithm
#includecstdio
using namespace std;
typedef struct xxx{int head;//头结点 int a[100];int next[100];//记录下一个元素的位置 int len;xxx(){head 1;memset(next, 0, sizeof(next));}void outList(){for(int i1; ilen; i){couta[i] ;}coutendl;}
}Listx; Listx Lx;void table_insertion_sort(){//表插入排序相当于静态链表 for(int i2; iLx.len; i){int pre, p;for(pLx.head; p Lx.a[p]Lx.a[i]; prep, pLx.next[p]);if(p0){Lx.next[pre] i;} else if(pLx.head){Lx.next[i] Lx.head;Lx.head i;} else {Lx.next[pre] i;Lx.next[i] p;} }//输出for(int iLx.head; i; i Lx.next[i]) coutLx.a[i] ;coutendl;
}void arrang_table() {int p Lx.head, q;for(int i1; iLx.len; i){while(p i) p Lx.next[p];//第i个记录在表中的位置不应该小于 i如果小于i说明该元素已经被交换位置了可以通过next继续寻找 q Lx.next[p];//指向下一个节点 if(p!i){//第p个元素应该在第i个位置 swap(Lx.a[i], Lx.a[p]);swap(Lx.next[i], Lx.next[p]);Lx.next[i] p;//该元素之前的位置 p指向被移走的记录使得以后可由while循环找回 }p q;}for(int i1; iLx.len; i) coutLx.a[i] ;coutendl;
}int main()
{int i;scanf(%d, Lx.len);for(i1; iLx.len; i)scanf(%d, Lx.a[i]);table_insertion_sort();arrang_table();return 0;
} 转载于:https://www.cnblogs.com/hujunzheng/p/4677484.html