-->
当前位置:首页 > 题库 > 正文内容

程序填空题:Bubble Sort

Luz4年前 (2021-05-10)题库2081
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.

```c++
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](3) ){
flag_swap++;
q = p->next;
@@[p->next = q->next](3);
@@[q->next = p->next->next](3);
@@[p->next->next = q](3);
}
else p = p->next;
}
} while (flag_swap > 0);
return h;
}
```





答案:
第1空:p->next->value > p->next->next->value

第2空:p->next = q->next

第3空:q->next = p->next->next

第4空:p->next->next = q

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。