Algorithms Basics

Bottled basis ** sorting algorithms, sort of (simplified version). **

**

The basic idea

The number of times each tag appearing in buckets, then the output thereof seq.
Features: Suitable for ordering value smaller array is sorted.

Code to achieve the following

#include<stdio.h>

int main ()
{
	int book[1001];
	int i,j,t,n;
	for(i=0;i<1000;i++)
	book[i]=0;
	scanf("%d",&n); //输入一个数n表示接下来有n个数处理
	for(i=0;i<n;i++) //循环读入n个数,并进行桶排序
	{
		scanf("%d",&t); //把每个变量读入到变量t中
		book[t]++; //进行计数,对编号为t的桶标记
	}
	for(i=1000;i>=0;i--) //依次判断1000到0的桶
	for(j=1;j<=book[i];j++) //出现几次就打印几次
	printf("%d",i);
	getchar(); getchar();
	return 0;

Input data validation

10
56 87 1000 0 19 890 999 10 748 8

operation result

1000 999 890 748 87 56 19 10 8 0

Remark

If you want to achieve from small to large
simply

for(i=1000;i>=0;i--)

Changed

for(i=0;i<=1000;i++)

On it.

Published 12 original articles · won praise 0 · Views 725

Guess you like

Origin blog.csdn.net/weixin_43252204/article/details/83585286