填空题

A graph with 30 vertices and 40 edges must have at most 21 connected component(s).

A graph with 15 vertices and 38 edges must have at most 6 connected component(s).

程序填空题

Bubble sort is a simple sorting algorithm. Suppose we have a list of integers and want to sort them in ascending order. Bubble sort repeatedly scans the list from the head to the tail, and swaps two adjacent numbers if they are in the wrong order. Please complete the following program to implement bubble sort.

typedef struct node *nodeptr;
struct node{int value;nodeptr next;/* some other fields */
};nodeptr BubbleSort (nodeptr h)
{/* h is the head pointer of the list with a dummy head node */nodeptr p, q;int flag_swap;if (!h->next)  return h;do{flag_swap = 0;p = h;while (p->next->next){if ( **p->next->value > p->next->next->value**){flag_swap++;q = p->next;**p->next = q->next;****q->next = p->next->next;****p->next->next = q;**}else p = p->next;}} while (flag_swap > 0);return h;
}

Please fill the array with the results after the following union/find operations.

union( find(2), find(7) )
union( find(3), find(5) )
union( find(0), find(2) )
union( find(5), find(7) )
union( find(5), find(6) )

Note: Assume union-by-size (if two sets are equal-sized, the first root will be the root of the result) and find-with-path-compression. S[i] is initialized to be −1 for all 0≤i≤7.

The function is to sort the list { r[1] … r[n] } in non-decreasing order. Unlike selection sort which places only the minimum unsorted element in its correct position, this algorithm finds both the minimum and the maximum unsorted elements and places them into their final positions.

void  sort( list r[], int n )
{int i, j, mini, maxi;for (i=1; i<n-i+1; i++) {mini = maxi = i;for( j=i+1;**j<=n-i+1**; ++j ){if( **r[j]->key < r[mini]->key** ) mini = j; else if(r[j]->key > r[maxi]->key) maxi = j;}if( mini != i ) swap(&r[mini], &r[i]);if( maxi != n-i+1 ){if( **maxi == i**) swap(&r[mini], &r[n-i+1]);else swap(&r[maxi], &r[n-i+1]);}}
}

The function is to find the K-th largest element in a list A of N elements. The function BuildMinHeap(H, K) is to arrange elements H[1] … H[K] into a min-heap. Please complete the following program.

ElementType FindKthLargest ( int A[], int N, int K )
{   /* it is assumed that K<=N */ElementType *H;int i, next, child;H = (ElementType *)malloc((K+1)*sizeof(ElementType));for ( i=1; i<=K; i++ ) H[i] = A[i-1];BuildMinHeap(H, K);for ( next=K; next<N; next++ ) {H[0] = A[next];if ( H[0] > H[1] ) {for ( i=1; i*2<=K; i=child ) {child = i*2;if ( child!=K && **H[child+1]<H[child]** child++;if ( **H[0]>H[child]** )H[i] = H[child];else break;}H[i] = H[0];}}return H[1];
}

The function Unweighted is to find the unweighted shortest path from Vertex S to every other vertices in a given Graph. The distances are stored in dist[], and path[] records the paths. The Graph is defined as the following:

typedef struct GNode *PtrToGNode;
struct GNode{int Nv;          /* Number of vertices */int Ne;          /* Number of edges    */AdjList List;    /* adjacency matrix */
};
typedef PtrToGNode Graph;
void Unweighted( Graph G, Queue Q, int dist[], int path[], Vertex S )
{Vertex V, W;NodePtr ptr;dist[S] = 0;Enqueue(S, Q);while ( !IsEmpty(Q) ) {V = Dequeue( Q );for ( ptr=G->List[V].FirstEdge; ptr; ptr=ptr->Next) {W = ptr->AdjV;if ( dist[W] == INFINITY ) {**dist[W] = dist[V] + 1**);path[W] = V;**Enqueue(W, Q)**);}}}
}

None-recursive Quick Selection algorithm
The function is to find the K-th smallest element in a list A of N elements. The initial function call is QSelect(A, N, K). Please complete the following program.

ElementType QSelect( ElementType A[], int N, int K )
{ElementType Pivot;int L, R, Left, Right, K1;Left = 0; Right = N-1; K1 = K;for(;; ) {L = Left, R = Right+1;Pivot = A[Left];while (1) {while ( A[++L] < Pivot ) ;**while ( A[--R] > Pivot );**if ( L < R ) Swap( &A[L], &A[R] );else break;}Swap( &A[Left], &A[R] );if ( K1 < (L-Left) )Right = R-1;else if ( K1 > (L-Left) ){**K1 = K1-(L-Left)**; Left = L;}elsereturn Pivot;}
}

Concatenation of lists is an operation where the elements of one list are added at the end of another list. For example, if we have a linked list L1→1→2→3 and another one L2→4→5→6. The function ListConcat is to return the head pointer of the list L→1→2→3→4→5→6.

The list structure is defined as the following:
typedef struct Node *PtrToNode;
struct Node{int Data;PtrToNode Next;
};
typedef PtrToNode List;
Please fill in the blanks.List ListConcat( List L1, List L2 )
{List Tmp = L1;if ( !L1 ) return L2;while ( Tmp->Next )**Tmp = Tmp->Next**;**Tmp->Next = L2**;return **L1**);
}

The function is to sort N elements in non-decreasing order by Insertion Sort.

void InsertionSort( ElementType A[ ], int N )
{int P, i;ElementType Tmp;for ( P=1; P<N; P++ ) {Tmp = A[P];for ( i=P; **i>0 && A[i-1]>Tmp**; i-- )A[i] = A[i-1];**A[i] = Tmp** ;}
}

算法竞赛基础训练题_填空题相关推荐

  1. 算法竞赛基础训练题_选择题

    算法竞赛基础训练题 选择题 For a sequentially stored linear list of length N, the time complexities for query and ...

  2. 2009年9月三级网络技术考前预测_填空题部分

    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 2009年9月 ...

  3. python少儿编程讲师笔试题_小码王教育儿童编程教师面试:做笔试题(填空题和编程题,填空题 - 职朋职业圈...

    为了帮助职业圈网友能够及时了解小码王教育的面试流程以及面试过程所涉及的面试问题,职业圈小编把刚获得的小码王教育面试经验马上编辑好,快速提供给大家,以便能够尽快帮助到有需要的人.这次面试总共花了1天.面 ...

  4. c语言题库选择填空题,C语言题库(填空题部分)

    <C语言题库(填空题部分)>由会员分享,可在线阅读,更多相关<C语言题库(填空题部分)(11页珍藏版)>请在人人文库网上搜索. 1.的双曲馀弦值. 二.填空题一.用习语言读写文 ...

  5. python填空题_Python题库-填空题

    『Python题库 - 填空题』Python笔试填空题 part 1. Python语言概述和Python开发环境配置 part 2. Python语言基本语法元素(变量,基本数据类型, 基础运算) ...

  6. c语言填空题题库6,二级C语言题库-填空题.doc

    二级C语言题库-填空题 第 PAGE \* Arabic \* MERGEFORMAT 24 页第 PAGE \* Arabic \* MERGEFORMAT 24 页 1.下列给定程序中,函数fun ...

  7. 现在学校应用较多的计算机学籍管理系统,计算机题库(填空题)

    题库为大学,计算机文化基础题库(填空题),文档来源于安徽理工大学,适用于高校计算机基础类课程 计算机考试500题(填空) 1下列叙述中不属于电子计算机特点的是D<高度智能的思维方式> 2按 ...

  8. android界面布局题,【填空题】Android 系统中, 用于定义布局显示在界面上的风格。...

    [填空题]Android 系统中, 用于定义布局显示在界面上的风格. 更多相关问题 [37]A.anotherB.each otherC.the otherD.one another Tabor ma ...

  9. python填空题_python填空题

    以下为<python填空题>的无排版文字预览,完整内容请下载 1.Python 提供了 6 个与字符串相关的函数.其中,_________函数可以返回字符串的长度. 在 Python 中, ...

最新文章

  1. 当程序崩溃的时候怎么办
  2. 【编程题目】输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印...
  3. php获取http头信息,php如何获取http头信息
  4. 2019第十届蓝桥杯C/C++ A组省赛 —— 第二题: 数列求值
  5. Citrix VDI攻略之四:PVS安装及配置
  6. ps图片拖不进去_PS教学:你的logo还不够高大上?7步教做出黄色金属质感logo
  7. 检测范围_论文检测系统的检测范围有哪些
  8. (4.12)全面解析-SQL事务+隔离级别+阻塞+死锁
  9. 博士女友的朋友圈都藏着什么秘密?
  10. 计数排序(count sort)
  11. 设计模式----单例模式(c++实现)
  12. 华为2022年软挑赛初赛试题及初级代码(成渝赛区)
  13. Builder模式创建pojo类
  14. ISSCC上的微型计算机:体积更小、功耗更少、算力更强
  15. 什么是项目沟通管理?
  16. 计算机界一些有意思的命名,程序员们也都很有情调嘛!
  17. 浅析Tier和Layer的区别
  18. springcloud(一)微服务概述
  19. EditText光标始终保持在文字末尾
  20. 2022.09青少年软件编程(Python)等级考试试卷(四级)

热门文章

  1. 口袋妖怪letsgo服务器维护,口袋妖怪letsgo新手玩法-新手攻略分享-可可网
  2. 普通话考试是从题库里抽吗_你必须知道这6件事儿,才能去参加普通话考试
  3. OSChina 周六乱弹 —— 有人要提亲吗?
  4. 句句戳心,尤其在这个时候...
  5. 不能验证这个“安装 OS X EI Capitan”应用程序副本解决方法(已修正)
  6. C++模拟化学物质合成程序《化学家》V1.0.5
  7. 网站被黑了怎么办、网络被劫持了怎么办?网络被劫持的解决方法
  8. PHP常用六大设计模式
  9. android 摇一摇动画效果,Android实现微信摇一摇功能
  10. 2021高考正安二中成绩查询,2021年江西高考主要科目考试结束 6月23日公布成绩