首页 > 编程语言 >17种编程语言实现排序算法-桶排序

17种编程语言实现排序算法-桶排序

时间:2023-01-29 11:38:58浏览次数:69  
标签:index 17 编程语言 int brr ++ var array 排序


开源地址

​https://gitee.com/lblbc/simple-works/tree/master/sort/​

覆盖语言:C、C++、C#、Java、Kotlin、Dart、Go、JavaScript(JS)、TypeScript(TS)、ArkTS、swift、PHP。
覆盖平台:安卓(Java、Kotlin)、iOS(SwiftUI)、Flutter(Dart)、Window桌面(C#)、前端(微信小程序、uni-app、vue)、后端(Java、Kotlin、NodeJS、Python、PHP、Go、C、C++)、鸿蒙

17种编程语言实现排序算法-桶排序_排序算法


17种编程语言实现排序算法-桶排序_算法_02

1. 安卓Java版

开发工具:​​下载Android Studio​​

private static void sort(int[] array) {
int max = array[0];
for (int value : array) {
if (value > max) {
max = value;
}
}
int[] bucketArray = new int[max + 1];
for (int value : array) {
bucketArray[value]++;
}
int index = 0;
for (int i = 0; i < bucketArray.length; i++) {
for (int j = 0; j < bucketArray[i]; j++) {
array[index++] = i;
}
}
}

17种编程语言实现排序算法-桶排序_排序算法_03

2. 安卓Kotlin版

开发工具:​​下载Android Studio​​

private fun sort(array: IntArray) {
var max = array[0]
for (value in array) {
if (value > max) {
max = value
}
}
val bucketArray = IntArray(max + 1)
for (value in array) {
bucketArray[value]++
}
var index = 0
for (i in bucketArray.indices) {
for (j in 0 until bucketArray[i]) {
array[index++] = i
}
}
}

17种编程语言实现排序算法-桶排序_开发工具_04

3. NodeJS

开发工具:​​下载Visual Studio Code​​

function sort() {
const array = [2, 1, 5, 4, 3];
var tmpArr = [];

for (var i = 0; i < array.length; i++) {
var num = array[i];
tmpArr[num] = tmpArr[num] || 0;
tmpArr[num]++;
num == null;
}

var result = ''
for (var i = 0; i < tmpArr.length; i++) {
if (tmpArr[i]) {
result += i + ' ';
}
}
return result
}

17种编程语言实现排序算法-桶排序_开发工具_05

4. Php

开发工具:​​下载Visual Studio Code​​

public function sort()
{
$array = [2, 1, 5, 4, 3];
$resultArray = [];
$length = count($array);
for ($i = 0, $max = $array[$i]; $i < $length; $i++) {
if ($max < $array[$i]) {
$max = $array[$i];
}
$bucket[$array[$i]] = [];
array_push($bucket[$array[$i]], $array[$i]);
}
for ($i = 0; $i <= $max; $i++) {
if (!empty($bucket[$i])) {
$l = count($bucket[$i]);
for ($j = 0; $j < $l; $j++) {
$resultArray[] = $bucket[$i][$j];
}
}
}
return var_dump($resultArray);
}

17种编程语言实现排序算法-桶排序_i++_06

5. Python

开发工具:​​下载PyCharm​​

array = [2, 1, 5, 4, 3]


def sort():
min_num, max_num = min(array), max(array)
bucket_num = (max_num - min_num) // 3 + 1
buckets = [[] for _ in range(bucket_num)]
for num in array:
buckets[(num - min_num) // 3].append(num)
new_array = list()
for i in buckets:
for j in sorted(i):
new_array.append(j)
return new_array

17种编程语言实现排序算法-桶排序_开发工具_07

6. Swift(SwiftUI版)

开发工具:XCode(mac电脑自带)

var array = [2, 1, 5, 4, 3]
func sort()
{
let maxValue = array.max()
var bucket:[Int] = Array.init(repeatElement(0, count: maxValue! + 1))
var newArray:[Int] = Array.init()

for index in array {
let numId = index
bucket[numId] += 1
}

for index in bucket.indices {
while bucket[index] > 0 {
newArray.append(index)
bucket[index] -= 1
}
}

array = newArray
}

17种编程语言实现排序算法-桶排序_开发工具_08

7. uni-app

开发工具:​​下载HBuilderX​​

sort() {
let array = [2, 1, 5, 4, 3]
var brr = []
var index, buckets, baseVal
var arrMaxVal, arrMinVal
var sortedIndex = 0,
n = array.length
arrMaxVal = array[0]
arrMinVal = array[0]
for (var i = 0; i < n; i++) {
if (arrMaxVal < array[i]) {
arrMaxVal = array[i]
}
if (arrMinVal > array[i]) {
arrMinVal = array[i]
}
}

if (arrMaxVal == arrMinVal) return
buckets = Math.ceil(Math.sqrt(n))
baseVal = Math.ceil((arrMaxVal - arrMinVal + 1) / buckets)
for (var i = 0; i < n; i++) {
index = Math.floor((array[i] - arrMinVal) / baseVal)
if (!brr[index]) {
brr[index] = [array[i]]
} else if (Array.isArray(brr[index])) {
for (var j = brr[index].length - 1; j >= 0 && brr[index][j] > array[i]; j--) {
brr[index][j + 1] = brr[index][j]
}
brr[index][j + 1] = array[i]
}
}

for (var i = 0; i < brr.length; i++) {
if (Array.isArray(brr[i])) {
for (var j = 0; j < brr[i].length; j++) {
array[sortedIndex++] = brr[i][j]
}
}
}
},

17种编程语言实现排序算法-桶排序_排序算法_09

8. vue

开发工具:​​下载Visual Studio Code​​

sort() {
let array = [2, 1, 5, 4, 3]
var brr = []
var index, buckets, baseVal
var arrMaxVal, arrMinVal
var sortedIndex = 0,
n = array.length
arrMaxVal = array[0]
arrMinVal = array[0]
for (var i = 0; i < n; i++) {
if (arrMaxVal < array[i]) {
arrMaxVal = array[i]
}
if (arrMinVal > array[i]) {
arrMinVal = array[i]
}
}

if (arrMaxVal == arrMinVal) return
buckets = Math.ceil(Math.sqrt(n))
baseVal = Math.ceil((arrMaxVal - arrMinVal + 1) / buckets)
for (var i = 0; i < n; i++) {
index = Math.floor((array[i] - arrMinVal) / baseVal)
if (!brr[index]) {
brr[index] = [array[i]]
}
else if (Array.isArray(brr[index])) {
for (var j = brr[index].length - 1;j >= 0 && brr[index][j] > array[i];j--) {
brr[index][j + 1] = brr[index][j]
}
brr[index][j + 1] = array[i]
}
}

for (var i = 0; i < brr.length; i++) {
if (Array.isArray(brr[i])) {
for (var j = 0; j < brr[i].length; j++) {
array[sortedIndex++] = brr[i][j]
}
}
}
},

17种编程语言实现排序算法-桶排序_i++_10

9. 微信小程序

开发工具:​​下载微信开发者工具​​

sort() {
let array = [2, 1, 5, 4, 3]
var brr = []
var index, buckets, baseVal
var arrMaxVal, arrMinVal
var sortedIndex = 0,
n = array.length
arrMaxVal = array[0]
arrMinVal = array[0]
for (var i = 0; i < n; i++) {
if (arrMaxVal < array[i]) {
arrMaxVal = array[i]
}
if (arrMinVal > array[i]) {
arrMinVal = array[i]
}
}

if (arrMaxVal == arrMinVal) return
buckets = Math.ceil(Math.sqrt(n))
baseVal = Math.ceil((arrMaxVal - arrMinVal + 1) / buckets)
for (var i = 0; i < n; i++) {
index = Math.floor((array[i] - arrMinVal) / baseVal)
if (!brr[index]) {
brr[index] = [array[i]]
}
else if (Array.isArray(brr[index])) {
for (var j = brr[index].length - 1;j >= 0 && brr[index][j] > array[i];j--) {
brr[index][j + 1] = brr[index][j]
}
brr[index][j + 1] = array[i]
}
}

for (var i = 0; i < brr.length; i++) {
if (Array.isArray(brr[i])) {
for (var j = 0; j < brr[i].length; j++) {
array[sortedIndex++] = brr[i][j]
}
}
}
}

17种编程语言实现排序算法-桶排序_Math_11

10. 鸿蒙(ArkTS)

开发工具:​​下载DevEco Studio​

sort() {
let array = [2, 1, 5, 4, 3]
var brr = []
var index, buckets, baseVal
var arrMaxVal, arrMinVal
var sortedIndex = 0
var n = array.length
arrMaxVal = array[0]
arrMinVal = array[0]
for (var i = 0; i < n; i++) {
if (arrMaxVal < array[i]) {
arrMaxVal = array[i]
}
if (arrMinVal > array[i]) {
arrMinVal = array[i]
}
}

if (arrMaxVal == arrMinVal) return
buckets = Math.ceil(Math.sqrt(n))
baseVal = Math.ceil((arrMaxVal - arrMinVal + 1) / buckets)
for (var i = 0; i < n; i++) {
index = Math.floor((array[i] - arrMinVal) / baseVal)
if (!brr[index]) {
brr[index] = [array[i]]
}
else if (Array.isArray(brr[index])) {
for (var j = brr[index].length - 1;j >= 0 && brr[index][j] > array[i]; j--) {
brr[index][j + 1] = brr[index][j]
}
brr[index][j + 1] = array[i]
}
}

for (var i = 0; i < brr.length; i++) {
if (Array.isArray(brr[i])) {
for (var j = 0; j < brr[i].length; j++) {
array[sortedIndex++] = brr[i][j]
}
}
}
}

17种编程语言实现排序算法-桶排序_Math_12

11. Go语言

开发工具:​​下载Visual Studio Code​​

func bucketSort(array []int) {
bucket := make(map[int][]int)
for _, val := range array {
bucket[val/10] = append(bucket[val/10], val)
}
for _, val := range bucket {
sort.Ints(val)
}
var keys []int
for key := range bucket {
keys = append(keys, key)
}
sort.Ints(keys)
index := 0
for _, key := range keys {
for _, val := range bucket[key] {
array[index] = val
index++
}
}
}

17种编程语言实现排序算法-桶排序_算法_13

12. Java

开发工具:​​下载IntelliJ IDEA​​

private static void sort(int[] array) {
int max = array[0];
for (int value : array) {
if (value > max) {
max = value;
}
}
int[] bucketArray = new int[max + 1];
for (int value : array) {
bucketArray[value]++;
}
int index = 0;
for (int i = 0; i < bucketArray.length; i++) {
for (int j = 0; j < bucketArray[i]; j++) {
array[index++] = i;
}
}
}

17种编程语言实现排序算法-桶排序_开发工具_14

13. Kotlin

开发工具:​​下载IntelliJ IDEA​​

private fun sort(array: IntArray) {
var max = array[0]
for (value in array) {
if (value > max) {
max = value
}
}
val bucketArray = IntArray(max + 1)
for (value in array) {
bucketArray[value]++
}
var index = 0
for (i in bucketArray.indices) {
for (j in 0 until bucketArray[i]) {
array[index++] = i
}
}
}

17种编程语言实现排序算法-桶排序_算法_15

14. Flutter

开发工具:​​下载IntelliJ IDEA​​

sort(List<int> array) {
int max = array[0];
for (int i = 0; i < array.length - 1; i++) {
if (array[i] > max) {
max = array[i];
}
}

List<num> bucketArray = List.filled(max + 1, 0);
for (int i = 0; i < array.length - 1; i++) {
bucketArray[array[i]] += 1;
}

int index = 0;
for (int i = 0; i < bucketArray.length; i++) {
for (int j = 0; j < bucketArray[i]; j++) {
array[index++] = i;
}
}
}

17种编程语言实现排序算法-桶排序_Math_16

15. C语言

开发工具:​​下载Visual Studio​​

void sort(int array[], int count)
{
int max = array[0];
int min = array[0];
for (int i = 1; i < count; i++)
{
if (array[i] > max)
{
max = array[i];
}

if (array[i] < min)
{
min = array[i];
}
}

int bucketLen = max - min + 1;
int* bucket = (int*)malloc(sizeof(int) * bucketLen);
bucket[0] = 0;
for (int i = 0; i < bucketLen; i++)
{
bucket[i] = 0;
}

int index = 0;
for (int i = 0; i < count; i++)
{
index = array[i] - min;
bucket[index] += 1;
}

int start = 0;
for (int i = 0; i < bucketLen; i++)
{
for (int j = start; j < start + bucket[i]; j++)
{
array[j] = min + i;
}
start += bucket[i];
}
free(bucket);
}

17种编程语言实现排序算法-桶排序_排序算法_17

16. C++

开发工具:​​下载Visual Studio​​

void sort(int array[], int count)
{
int max = array[0];
int min = array[0];
for (int i = 1; i < count; i++)
{
if (array[i] > max)
{
max = array[i];
}

if (array[i] < min)
{
min = array[i];
}
}

int bucketLen = max - min + 1;
int *bucket = new int[bucketLen];
for (int i = 0; i < bucketLen; i++)
{
bucket[i] = 0;
}

int index = 0;
for (int i = 0; i < count; i++)
{
index = array[i] - min;
bucket[index] += 1;
}

int start = 0;
for (int i = 0; i < bucketLen; i++)
{
for (int j = start; j < start + bucket[i]; j++)
{
array[j] = min + i;
}
start += bucket[i];
}
delete[] bucket;
}

17种编程语言实现排序算法-桶排序_Math_18

17. C#

开发工具:​​下载Visual Studio​​

static void Sort(int[] array, int len, int bucketsize = 4)
{
int max = GetMaxValue(array, len);
int min = GetMinValue(array, len);
List<List<int>> buckets = new List<List<int>>();
int bucketnum = (max - min) / bucketsize + 1;
for (int i = 0; i < bucketnum; i++)
{
buckets.Add(new List<int>());
}
for (int i = 0; i < len; i++)
{
int bucketIndex = (array[i] - min) / bucketsize;
buckets[bucketIndex].Add(array[i]);
}
int index = 0;
for (int i = 0; i < buckets.Count; i++)
{
buckets[i].Sort();
for (int j = 0; j < buckets[i].Count; j++)
{
array[index++] = buckets[i][j];
}
}
}
static int GetMaxValue(int[] array, int len)
{
int max = array[0];
for (int i = 1; i < len; i++)
{
if (array[i]>max)
{
max = array[i];
}
}
return max;
}

static int GetMinValue(int[] array, int len)
{
int min = array[0];
for (int i = 1; i < len; i++)
{
if (array[i] <min)
{
min = array[i];
}
}
return min;
}

17种编程语言实现排序算法-桶排序_i++_19

关于

厦门大学计算机专业|华为八年高级工程师
专注《零基础学编程系列》
包含:Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
公众号:蓝不蓝编程


标签:index,17,编程语言,int,brr,++,var,array,排序
From: https://blog.51cto.com/hspbc/6025588

相关文章

  • 17种编程语言实现排序算法-计数排序
    开源地址​​https://gitee.com/lblbc/simple-works/tree/master/sort/​​覆盖语言:C、C++、C#、Java、Kotlin、Dart、Go、JavaScript(JS)、TypeScript(TS)、ArkTS、swift、......
  • 17种编程语言+10种排序算法
    开源地址​​https://gitee.com/lblbc/simple-works/tree/master/sort​​覆盖语言:C、C++、C#、Java、Kotlin、Dart、Go、JavaScript(JS)、TypeScript(TS)、ArkTS、swift、......
  • 17种编程语言实现排序算法-堆排序
    开源地址​​https://gitee.com/lblbc/simple-works/tree/master/sort/​​覆盖语言:C、C++、C#、Java、Kotlin、Dart、Go、JavaScript(JS)、TypeScript(TS)、ArkTS、swift、......
  • 17种编程语言实现排序算法-插入排序
    开源地址​​https://gitee.com/lblbc/simple-works/tree/master/sort/​​覆盖语言:C、C++、C#、Java、Kotlin、Dart、Go、JavaScript(JS)、TypeScript(TS)、ArkTS、swift、......
  • 17种编程语言实现排序算法-合并排序
    开源地址​​https://gitee.com/lblbc/simple-works/tree/master/sort/​​覆盖语言:C、C++、C#、Java、Kotlin、Dart、Go、JavaScript(JS)、TypeScript(TS)、ArkTS、swift、......
  • 7.17 SQL Server INTERSECT(交集)
    SQLServerINTERSECT(交集)目录SQLServerINTERSECT(交集)简介示例简介SQLServerINTERSECT组合了两个或多个查询的结果,并返回多个查询相同的行。语法:query_1INTERSEC......
  • Arrays.sort()降序排序
    默认的Java.util包中的Arrays.sort(),可以实现对java中的基本数据类型(byte、char、short、int、long、float、double、boolean)的数组进行升序排序。但如果要实现降序排列,则......
  • 17种编程语言实现排序算法-选择排序
    开源地址​​https://gitee.com/lblbc/simple-works/tree/master/sort/​​覆盖语言:C、C++、C#、Java、Kotlin、Dart、Go、JavaScript(JS)、TypeScript(TS)、ArkTS、swift......
  • 17种编程语言实现排序算法-冒泡排序
    开源地址​​https://gitee.com/lblbc/simple-works/tree/master/sort​​覆盖语言:C、C++、C#、Java、Kotlin、Dart、Go、JavaScript(JS)、TypeScript(TS)、ArkTS、swift、......
  • 17种编程语言实现排序算法-快速排序
    开源地址​​https://gitee.com/lblbc/simple-works/tree/master/sort/​​覆盖语言:C、C++、C#、Java、Kotlin、Dart、Go、JavaScript(JS)、TypeScript(TS)、ArkTS、swift......