基于非比較的排序:计数排序(countSort),桶排序(bucketSort),基数排序(radixSort)
计数排序
count[arr[i ] ] ] = a[ i ];//count[ arr[ i] ]在sorted[ ]中相应的位置,等于a[ i ]就是相应元素的值。
public class CountSort {
static int[] countSort(int[] a, int range/*数组元素的范围*/){
int count[] = new int[range];
for (int i = 0; i < a.length; i++) {
count[a[i]]++;
}
for (int i = 1; i < count.length; i++) {
count[i] += count[i-1];
}
int sortArr[] = new int[a.length];
for (int i = 0; i < sortArr.length; i++) {
count[a[i]]--;
sortArr[count[a[i]]] = a[i];
}
return sortArr;
}
public static void main(String[] args) {
int[] a = {1,34,66,90,99,34,56,2,3,47,66,99};
int[] sortArr = countSort(a,100);
for (int i = 0; i < sortArr.length; i++) {
System.out.print(sortArr[i]+" ");
}
}
}
桶排序
#include <time.h>
#include <iostream>
#include <iomanip>
using namespace std; /*initial arr*/
void InitialArr(double *arr,int n)
{
srand((unsigned)time(NULL));
for (int i = 0; i<n;i++)
{
arr[i] = rand()/double(RAND_MAX+1); //(0.1)
}
} /* print arr*/
void PrintArr(double *arr,int n)
{
for (int i = 0;i < n; i++)
{
cout<<setw(15)<<arr[i];
if ((i+1)%5 == 0 || i == n-1)
{
cout<<endl;
}
}
} void BucketSort(double * arr,int n)
{
double **bucket = new double*[10];
for (int i = 0;i<10;i++)
{
bucket[i] = new double[n];
}
int count[10] = {0};
for (int i = 0 ; i < n ; i++)
{
double temp = arr[i];
int flag = (int)(arr[i]*10); //flag标识小树的第一位
bucket[flag][count[flag]] = temp; //用二维数组的每一个向量来存放小树第一位同样的数据
int j = count[flag]++; /* 利用插入排序对每一行进行排序 */
for(;j > 0 && temp < bucket[flag][j - 1]; --j)
{
bucket[flag][j] = bucket[flag][j-1];
}
bucket[flag][j] =temp;
} /* 全部数据又一次链接 */
int k=0;
for (int i = 0 ; i < 10 ; i++)
{
for (int j = 0 ; j< count[i];j++)
{
arr[k] = bucket[i][j];
k++;
}
}
for (int i = 0 ; i<10 ;i++)
{
delete bucket[i];
bucket[i] =NULL;
}
delete []bucket;
bucket = NULL;
} void main()
{
double *arr=new double[10];
InitialArr(arr, 10);
BucketSort(arr, 10);
PrintArr(arr,10);
delete [] arr;
}基数排序
#include<stdio.h>
#define MAX 20
#define SHOWPASS
#define BASE 10 void print(int *a, int n) {
int i;
for (i = 0; i < n; i++) {
printf("%d\t", a[i]);
}
} void radixsort(int *a, int n) {
int i, b[MAX], m = a[0], exp = 1;
//寻找最大位数的那个数
for (i = 1; i < n; i++) {
if (a[i] > m) {
m = a[i];
}
} while (m / exp > 0) {
int bucket[BASE] = { 0 };
//进行计数排序
for (i = 0; i < n; i++) {
bucket[(a[i] / exp) % BASE]++;
} for (i = 1; i < BASE; i++) {
bucket[i] += bucket[i - 1];
} for (i = n - 1; i >= 0; i--) {
b[--bucket[(a[i] / exp) % BASE]] = a[i];
} for (i = 0; i < n; i++) {
a[i] = b[i];
}
//计数排序结束
exp *= BASE;//进入下个位数 #ifdef SHOWPASS
printf("\nPASS : ");
print(a, n);
#endif
}
} int main() {
int arr[MAX];
int i, n; printf("Enter total elements (n <= %d) : ", MAX);
scanf("%d", &n);
n = n < MAX ? n : MAX; printf("Enter %d Elements : ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
} printf("\nARRAY : ");
print(&arr[0], n); radixsort(&arr[0], n); printf("\nSORTED : ");
print(&arr[0], n);
printf("\n"); return 0;
}
基于非比較的排序:计数排序(countSort),桶排序(bucketSort),基数排序(radixSort)的更多相关文章
- 数据结构与算法-排序(十)桶排序(Bucket Sort)
摘要 桶排序和基数排序类似,相当于基数排序的另外一种逻辑.它是将取值范围当做创建桶的数量,桶的长度就是序列的大小.通过处理比较元素的数值,把元素放在桶的特定位置,然后遍历桶,就可以得到有序的序列. 逻 ...
- 桶排序和计数排序的理解实现和比较(Java)
比较和非比较的区别 常见的快速排序.归并排序.堆排序.冒泡排序等属于比较排序.在排序的最终结果里,元素之间的次序依赖于它们之间的比较.每个数都必须和其他数进行比较,才能确定自己的位置.比较排序的优势是 ...
- 计数排序和桶排序(Java实现)
目录 比较和非比较的区别 计数排序 计数排序适用数据范围 过程分析 桶排序 网络流传桶排序算法勘误 桶排序适用数据范围 过程分析 比较和非比较的区别 常见的快速排序.归并排序.堆排序.冒泡排序等属于比 ...
- go实现堆排序、快速排序、桶排序算法
一. 堆排序 堆排序是利用堆这种数据结构而设计的一种排序算法.以大堆为例利用堆顶记录的是最大关键字这一特性,每一轮取堆顶元素放入有序区,就类似选择排序每一轮选择一个最大值放入有序区,可以把堆排序看成是 ...
- 【leetcode 桶排序】Maximum Gap
1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...
- 记数排序 & 桶排序 & 基数排序
为什么要写这样滴一篇博客捏...因为一个新初一问了一道水题,结果就莫名其妙引起了战斗. 然后突然发现之前理解的桶排序并不是真正的桶排序,所以写一篇来区别下这三个十分相似的排序辣. 老年菜兔的觉醒!!! ...
- 排序算法-桶排序(Java)
package com.rao.sort; import java.util.*; /** * @author Srao * @className BucketSort * @date 2019/12 ...
- 算法相关——Java排序算法之桶排序(一)
(代码中对应一个数组的下标),将每个元素放入对应桶中,再将所有元素按顺序输出(代码中则按顺序将数组i下标输出arrary[i]次),即为{0,1,3,5,5,6,9}. 1.2 代码实现 /* *@ ...
- php桶排序简单实现
桶排序中最重要的环节是映射函数. 初步学习桶排序的过程中,映射比较简单.实现代码如下: /** * 第一种桶排序的办法,每个桶存储相同值的数据 * */ function bucketSort($no ...
随机推荐
- 1、树莓派3B开箱+安装系统
说白了,树莓派就是英国人为学生开发的一款微型电脑.电脑能干什么,那就多了.英国小学生有用树莓派做气象站的,有检测家长开门回家的(可以安心玩游戏了),总之脑洞有多大就可以玩多大. 了解到了之后就一直心水 ...
- 中断、轮询、事件驱动、消息驱动、数据流驱动(Flow-Driven)?
轮询.事件驱动.消息驱动.流式驱动 ---数据流驱动 Unidirectional Architecture? 中断.事件.消息这样一种机制来实现更好的在多任务系统里运行... 阻塞,非阻塞同步,异步 ...
- springboot之使用@ConfigurationProperties注解
springboot之使用@ConfigurationProperties注解 代码已经上传至github https://github.com/gittorlight/springboot-exam ...
- Docker简介与安装(一)
Docker简介 Docker 是 Docker.Inc 公司开源的一个基于 LXC技术之上构建的Container容器引擎, 源代码托管在 GitHub 上, 基于Go语言并遵从Apache2.0协 ...
- ODBC在注册表中的位置
增加一个oracle的odbc regedit打开注册表 64位 :HKEY_LOCAL_MACHINE -> SOFTWARE -> 32位: HKEY_LOCAL_MACHINE -& ...
- 001 Anaconda的介绍与安装
1.官网 www.continuum.io 2.ananconda的版本 同一个版本下对应一个python3与python2,在这里下载使用python 2.7的版本. 3.概述 Anaconda是一 ...
- 15:链表中倒数第K个节点
/** * 面试题15:链表中倒数第K个节点 * 输入一个链表,输出该链表中倒数第k个结点. */ public class _15_linked_K { public static void mai ...
- 2017-2018-1 20179202《Linux内核原理与分析》第十周作业
一.设备与模块 1.设备类型 块设备:随机访问设备中的内容,通过块设备结点访问,通常被挂载为文件系统 字符设备:不可寻址,仅提供数据的流式访问,通过字符设备结点访问,应用程序通过直接访问设备节点与字符 ...
- ubuntu 16.04.1 LTS postgresql安装配置
postgresql安装--------------------二进制安装:wget https://get.enterprisedb.com/postgresql/postgresql-9.5.6- ...
- vue.js-过滤器 filters使用详细示例
什么也不说了,直接上干货: 1.首先,获取后台数据到页面,并调用过滤器 在<script>中添加 onRefreshItems (currentPage, perPage) { if (t ...