编程实现将给定的一组整数采用选择排序法按由小到大的顺序排序。要求:
1) 编写函数create()根据已知整数数组构造一个单链表,假设结点只有一个整数数据域。
2) 编写函数sort()采用选择排序方法对已知链表进行排序。
3) 在主函数中完成数据的输入与结果的输出。
===我自己写了一个程序,但是不确定选择排序法是什么,求大侠帮忙看下我写的代码sort()是选择排序吗?感觉自己混淆了选择排序法和冒泡排序了,求教如何区别?
#include
#include
typedef struct list
{ int data;
struct list next;
} SLIST;
SLIST *creat(int *a)
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; i
{ q=(SLIST *)malloc(sizeof(SLIST));
q->data=a[i]; p->next=q; p=q;
}
p->next=0;
return h;
}
void sort(SLIST *h)
{ SLIST *p, *q; int t;
p = h ;
while (p) {
q = p->next ;
while (q) {
if (p->data> q->data)
{
t = p->data;
p->data = q->data;
q->data = t;
}
q = q->next;
}
p = p->next;
}
}
main()
{ SLIST *head,*p;
int a[8]={11,10,25,18,29,22,15,19};
head=creat(a);
printf("\nThe list before sorting:\n");
p=head->next;
if (p==NULL)
printf("\nThe list is NULL!\n");
else
{ printf("\nHead");
do
{
printf("->%d",p->data);
p=p->next;
} while(p!=NULL);
printf("->End\n");
}
sort(head);
printf("\nThe list after sorting:\n");
p=head->next;
if (p==NULL)
printf("\nThe list is NULL!\n");
else
{ printf("\nHead");
do
{
printf("->%d",p->data);
p=p->next;
} while(p!=NULL);
printf("->End\n");
}
}
/
1.程序运行结果为:
The list before sorting:
Head->11->10->25->18->29->22->15->19->End
The list after sorting:
Head->10->11->15->18->19->22->25->29->End
*/