Posted on 2006-06-14 23:50
Enjoy Life 阅读(665)
评论(0) 编辑 收藏 引用 所属分类:
DS study
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20
#define MAX_LENGTH 100
typedef int RedType;
typedef struct SqList{
RedType r[MAXSIZE+1];
int length;
}SqList;
//
此处用指针或者引用都可以,但是不能直接用结构来作为参数传人
InsertSort(SqList &L):OK InsertSort(SqList L):ERROR,
没法正常工作
/*
用结构来做,比较常用
void main()
{
int i;
SqList L;
printf("\nPlease input the length you want to sort\n");
scanf("%d",&L.length);
for(i=1;i<=L.length;i++){
printf("\nPlease input the %dth data\n",i);
scanf("%d",&L.r[i]);
}
printf("\nYou input data is:");
for(i=1;i<=L.length;i++)
printf("%5d",L.r[i]);
InsertSort(&L);
printf("\nThe sorted data is:");
for(i=1;i<=L.length;i++)
printf("%5d",L.r[i]);
}
*/
void SelectSort(SqList *L);
//
用指针来做
void main()
{
int i;
SqList *L;
if(!(L=(SqList *)malloc(sizeof(SqList)))){
printf("OverFlow\n");
return;
}
printf("\nPlease input the length you want to sort\n");
scanf("%d",&L->length);
for(i=1;i<=L->length;i++){
printf("\nPlease input the %dth data\n",i);
scanf("%d",&L->r[i]);
}
printf("\nYou input data is:");
for(i=1;i<=L->length;i++)
printf("%5d",L->r[i]);
SelectSort(L);
printf("\nThe sorted data is:");
for(i=1;i<=L->length;i++)
printf("%5d",L->r[i]);
free(L);
}
void SelectSort(SqList *L) //SelectSort() sub-function
{ int i,j,k,temp;
for(i=1;i<=L->length;++i)
{ k=i;
for(j=i+1;j<=L->length;++j)
if(L->r[j]<L->r[k])
k=j;
if(i!=k)
{ temp=L->r[i];
L->r[i]=L->r[k];
L->r[k]=temp;
}
}
}//SelectSort() end