今日任务
2.冒泡排序和选择排序
#define MAX 10
typedef struct{
int id;
}data;
typedef struct seq{
data arr[MAX];
int len;
}seqList,*seqListP;
/*
* function: 选择排序
* @param [ in]
* @param [out]
* @return
*/
int selectSort(seqListP p){
if(is_empty(p))
return 1;
for (int i=0; i<p->len-1; i++)
{
int max=i;
for (int j=i+1; j<p->len; j++)
{
if (p->arr[j].id>p->arr[max].id)
max=j;
}
data d=p->arr[i];
p->arr[i]=p->arr[max];
p->arr[max]=d;
}
puts("select Sort success");
output(p);
return 0;
}
/*
* function: 冒泡排序
* @param [ in]
* @param [out]
* @return
*/
int bubbling(seqListP p){
if(is_empty(p))
return 1;
for (int i=0; i<p->len-1; i++)
{
for (int j=0; j<p->len-i-1; j++)
{
if(p->arr[j].id>p->arr[j+1].id)
{
data d=p->arr[j];
p->arr[j]=p->arr[j+1];
p->arr[j+1]=d;
}
}
}
puts("bubblingSort success");
output(p);
return 0;
}
运行结果:
思维导图