首页 技术 正文
技术 2022年11月6日
0 收藏 350 点赞 1,061 浏览 847 个字

由于上次的桶排序占用空间太多,这次又有了一个新的办法

直接上代码:

#include <bits/stdc++.h>
using namespace std;
int n;
void bubble(double a[],int n)
{
double t;
for (int i = 0;i < n - 1;i ++)
{
for (int j = 0;j < n - i - 1;j ++)
{
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
}
int main()
{
double tong[10][10];
cin >> n;
double arr[10];
for (int i = 0;i < n;i ++)
{
cin >> arr[i];
}
memset(tong,0,sizeof(tong));
for (int i = 0;i < n;i ++) //列
{
int t = 10.0 * arr[i];
tong[i][t] = arr[i];
}
for (int i = 1;i < 10;i ++)
{
int t = 10.0 * arr[i];
bubble(tong[i][t],n);
}
for (int i = 0;i < 10;i ++)
{
for (int j = 0;j < 10;j ++)
{
if (tong[i][j] > 0)
{
printf("%.2lf ",tong[i][j]);
}
}
continue;
}
cout << endl;
return 0;
}
/*
10
0.78 0.17 0.39 0.26 0.72 0.94 0.21 0.12 0.23 0.68
*/

运行结果:

主要思路

基本类似于分治思想就是把一个规模为N的问题分解为K个规模较小的问题,这些子问题相互独立且与原问题性质相同,求出子问题的解就可以得到原问题的解。流程如下:

1、建立好对应的桶

2、把要排序的数组分别放入对应的桶中

3、统计元素在桶中出现的次数

4、按照桶的顺序输出同理的元素

这个代码还不完整,不能做到排序

未完待续~

参考视频:[https://www.bilibili.com/video/av17940595?from=search&seid=5095049522483939948]

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294