啊哈磊老师的《啊哈!算法》学习记录。
之前笔者写过一篇比较详细的排序算法: 小白学六大排序算法(C语言版),这里就只对书中提到的算法进行记录。
排序
桶排序
#include<stdio.h>
int main()
{
int i,j;
int t;
int book[100];
for(i=0;i<100;i++)
{
book[i]=0;
}
for(i=1;i<=5;i++)
{
scanf("%d",&t);//容易写错之处
book[t]++;
}
for(i=0;i<100;i++)
{
for(j=1;j<=book[i];j++)
{
printf("%d ",i);
}
}
getchar();getchar();
return 0;
}
冒泡排序
#include<stdio.h>
int main()
{
int i,j,k,n;
scanf("%d",&n);
int book[100];
for(i=1;i<=n;i++)
{
scanf("%d",&book[i]);
}
for(i=1;i<=n-1;i++)
{
for(j=1;j<n-i;j++)//无等号
{
if(book[j]>book[j+1])
{
k=book[j+1];
book[j+1]=book[j];
book[j]=k;
}
}
}
for(i=1;i<=n;i++)
{
printf("%d ",book[i]);
}
getchar();getchar();
return 0;
}
冒泡排序(结构)
#include<stdio.h>
struct student//()//这里无()
{
char name[20];
char score;
};
int main()
{
int i,j;
struct student book[100],temp;//temp定义在struct
int n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s %d",book[i].name,&book[i].score); //%s不用&
}
for(i=1;i<=n-1;i++)
{
for(j=1;j<n-i;j++)
{
if(book[j].score>book[j+1].score)
{
temp=book[j];
book[j]=book[j+1];
book[j+1]=temp;
}
}
}
for(i=1;i<=n;i++)
{
printf("%s\n",book[i].name);
}
getchar();getchar();
return 0;
}
快速排序
#include<stdio.h>
int book[100],n;//定义数组的位置
void quicksort(int left,int right)
{
int i,j,k;
int temp;
temp=book[left];
if(left>right)
return;
i=left;
j=right;
while(i!=j)
{
while(i<j&&book[j]>=temp)
j--;
while(i<j&&book[i]<=temp)
i++;
if(i<j)
{
k=book[i];
book[i]=book[j];
book[j]=k;
}
}
book[left]=book[i];//此处的写法,易和下面位置颠倒
book[i]=temp;
quicksort(left,i-1);
quicksort(i+1,right);
}
int main()
{
int i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&book[i]);
}
quicksort(1,n);
for(i=1;i<=n;i++)
{
printf("%d ",book[i]);
}
getchar();getchar();
return 0;
}
排序且去重
书上写了一个例题,叫做 “小哼买书” ,基本意思是:不仅要按一定顺序排列,而且要把里面的重复元素去掉。
桶排序去重
#include<stdio.h>
int main()
{
int book[100];
int i;
for(i=1;i<100;i++)
{
book[i]=0;
}
int n,t;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&t);
book[t]=1; //这里不再统计书的个数,出现的话记作 1
}
for(i=1;i<100;i++)
{
if(book[i]==1)
{
printf("%d ",i);
}
}
getchar();getchar();
return 0;
}
冒泡排序去重
#include<stdio.h>
int main()
{
int i,j,n;
int temp;
int book[100];
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&book[i]);
}
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
{
if(book[j]<book[j+1])
{
temp=book[j];
book[j]=book[j+1];
book[j+1]=temp;
}
}
}
printf("%d ",book[1]);//先把第一个输出
for(i=2;i<=n;i++)
{
if(book[i]!=book[i-1])//后面的元素和前面的比较
printf("%d ",book[i]);
}
return 0;
}
快速排序去重
#include<stdio.h>
int book[100],n;
void quicksort(int left,int right)
{
int i,j,k;
int temp;
i=left;
j=right;
temp=book[left];
if(i>j)
return;
while(i!=j)
{
while(i<j&&book[j]>=temp)
j--;
while(i<j&&book[i]<=temp)
i++;
if(i<j)
{
k=book[i];
book[i]=book[j];
book[j]=k;
}
}
book[left]=book[i];
book[i]=temp;
quicksort(left,i-1);
quicksort(i+1,right);
}
int main()
{
int i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&book[i]);
}
quicksort(1,n);
printf("%d ",book[1]);//同上
for(i=2;i<=n;i++)
{
if(book[i]!=book[i-1])
{
printf("%d ",book[i]);
}
}
return 0;
}
代码纯手打,可能有写的有点繁琐的地方,希望大家谅解!