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

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

时间:2023-01-29 11:38:14浏览次数:64  
标签:counting 17 编程语言 min int max ++ 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种编程语言实现排序算法-计数排序_i++_02

1. 安卓Java版

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

private static void sort(int[] array) {
if (array.length == 0) return;
int min = array[0];
int max = min;
for (int num : array) {
if (num > max) {
max = num;
} else if (num < min) {
min = num;
}
}
int[] counting = new int[max - min + 1];
for (int i = 0; i < array.length; i++) {
counting[array[i] - min] += 1;
}
int index = -1;
for (int i = 0; i < counting.length; i++) {
for (int j = 0; j < counting[i]; j++) {
index++;
array[index] = i + min;
}
}
}

17种编程语言实现排序算法-计数排序_Visual_03

2. 安卓Kotlin版

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

private fun sort(array: IntArray) {
if (array.isEmpty()) return
var min = array[0]
var max = min
for (num in array) {
if (num > max) {
max = num
} else if (num < min) {
min = num
}
}
val counting = IntArray(max - min + 1)
for (i in array.indices) {
counting[array[i] - min] += 1
}
var index = -1
for (i in counting.indices) {
for (j in 0 until counting[i]) {
index++
array[index] = i + min
}
}
}

17种编程语言实现排序算法-计数排序_i++_04

3. NodeJS

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

function sort() {
let array = [2, 1, 5, 4, 3]
var min = array[0]
var max = min
for (var i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i]
} else if (array[i] < min) {
min = array[i]
}
}

var counting = Array(max - min + 1).fill(0)
for (var i = 0; i < array.length; i++) {
counting[array[i] - min] += 1
}
var index = -1
for (var i = 0; i < counting.length; i++) {
for (var j = 0; j < counting[i]; j++) {
index++
array[index] = i + min
}
}
}

17种编程语言实现排序算法-计数排序_算法_05

4. Php

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

public function sort()
{
$array = [2, 1, 5, 4, 3];
$bucket = [];
$result = [];
$min = $array[0];
$max = $array[0];
foreach ($array as $item) {
if ($item < $min) {
$min = $item;
}
if ($item > $max) {
$max = $item;
}
}
foreach ($array as $v) {
if (!isset($bucket[$v])) {
$bucket[($v)] = 0;
}
$bucket[$v]++;
}
for ($i = $min; $i <= $max; $i++) {
while (isset($bucket[$i]) && $bucket[$i] > 0) {
array_push($result, $i);
$bucket[$i]--;
}
}
return var_dump($result);
}

17种编程语言实现排序算法-计数排序_Visual_06

5. Python

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

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


def sort():
max_num = max(array)
count = [0 for i in range(max_num + 1)]
for val in array:
count[val] += 1
array.clear()
for index, val in enumerate(count):
for i in range(val):
array.append(index)

17种编程语言实现排序算法-计数排序_i++_07

6. Swift(SwiftUI版)

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

var array = [2, 1, 5, 4, 3]
func sort() {
var min = array[0]
var max = min
for i in 0...array.count-1 {
if (array[i] > max) {
max = array[i]
} else if (array[i] < min) {
min = array[i]
}
}

var counting:[Int] = [Int](repeating: 0, count: max - min + 1)

for i in 0...array.count-1 {
counting[array[i] - min] += 1
}
var index = -1
for i in 0...counting.count-1 {
for _ in 0...counting[i]-1 {
index += 1
array[index] = i + min
}
}
}

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

7. uni-app

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

sort() {
let array = [2, 1, 5, 4, 3]
var min = array[0]
var max = min
for (var i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i]
} else if (array[i] < min) {
min = array[i]
}
}

var counting = Array(max - min + 1).fill(0)
for (var i = 0; i < array.length; i++) {
counting[array[i] - min] += 1
}
var index = -1
for (var i = 0; i < counting.length; i++) {
for (var j = 0; j < counting[i]; j++) {
index++
array[index] = i + min
}
}
this.array = array
this.arrayStr = this.convertToStr(array)
},

17种编程语言实现排序算法-计数排序_i++_09

8. vue

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

sort() {
let array = [2, 1, 5, 4, 3]
var min = array[0]
var max = min
for (var i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i]
} else if (array[i] < min) {
min = array[i]
}
}

var counting = Array(max - min + 1).fill(0)
for (var i = 0; i < array.length; i++) {
counting[array[i] - min] += 1
}
var index = -1
for (var i = 0; i < counting.length; i++) {
for (var j = 0; j < counting[i]; j++) {
index++
array[index] = i + min
}
}
this.array = array
this.arrayStr = this.convertToStr(array)
},

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

9. 微信小程序

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

sort() {
let array = [2, 1, 5, 4, 3]
var min = array[0]
var max = min
for (var i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i]
} else if (array[i] < min) {
min = array[i]
}
}

var counting = Array(max - min + 1).fill(0)
for (var i = 0; i < array.length; i++) {
counting[array[i] - min] += 1
}
var index = -1
for (var i = 0; i < counting.length; i++) {
for (var j = 0; j < counting[i]; j++) {
index++
array[index] = i + min
}
}
},

17种编程语言实现排序算法-计数排序_开发工具_11

10. 鸿蒙(ArkTS)

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

sort() {
let array = [2, 1, 5, 4, 3]
var min = array[0]
var max = min
for (var i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i]
} else if (array[i] < min) {
min = array[i]
}
}

var counting = Array(max - min + 1).fill(0)
for (var i = 0; i < array.length; i++) {
counting[array[i] - min] += 1
}
var index = -1
for (var i = 0; i < counting.length; i++) {
for (var j = 0; j < counting[i]; j++) {
index++
array[index] = i + min
}
}
this.array = array
}

17种编程语言实现排序算法-计数排序_Visual_12

11. Go语言

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

var array = []int{2, 1, 5, 4, 3}

func sort() {
var index int = 0
var tmpLen int = 0
for _, data := range array {
if tmpLen < data {
tmpLen = data
}
}
tmpLen++
tmpArray := make([][]int, tmpLen)
for _, data := range array {
tmpArray[data] = append(tmpArray[data], data)
}
for i := 0; i < tmpLen; i++ {
if tmpArray[i] != nil {
for j := 0; j < len(tmpArray[i]); j++ {
array[index] = tmpArray[i][j]
index++
}
}
}
}

17种编程语言实现排序算法-计数排序_i++_13

12. Java

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

private static void sort(int[] array) {
if (array.length == 0) return;
int min = array[0];
int max = min;
for (int num : array) {
if (num > max) {
max = num;
} else if (num < min) {
min = num;
}
}
int[] counting = new int[max - min + 1];
for (int i = 0; i < array.length; i++) {
counting[array[i] - min] += 1;
}
int index = -1;
for (int i = 0; i < counting.length; i++) {
for (int j = 0; j < counting[i]; j++) {
index++;
array[index] = i + min;
}
}
}

17种编程语言实现排序算法-计数排序_算法_14

13. Kotlin

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

private fun sort(array: IntArray) {
if (array.isEmpty()) return
var min = array[0]
var max = min
for (num in array) {
if (num > max) {
max = num
} else if (num < min) {
min = num
}
}
val counting = IntArray(max - min + 1)
for (i in array.indices) {
counting[array[i] - min] += 1
}
var index = -1
for (i in counting.indices) {
for (j in 0 until counting[i]) {
index++
array[index] = i + min
}
}
}

17种编程语言实现排序算法-计数排序_Visual_15

14. Flutter

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

void sort(List<int> array) {
int min = array[0];
int max = min;
for (int num in array) {
if (num > max) {
max = num;
} else if (num < min) {
min = num;
}
}
List<int> counting = List.filled(max - min + 1, 0);
for (int i = 0; i < array.length; i++) {
counting[array[i] - min] += 1;
}
int index = -1;
for (int i = 0; i < counting.length; i++) {
for (int j = 0; j < counting[i]; j++) {
index++;
array[index] = i + min;
}
}
}

17种编程语言实现排序算法-计数排序_i++_16

15. C语言

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

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

int* counting = (int*)malloc(sizeof(int) * countingLen);
memset(counting,0,sizeof(int)* countingLen);

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

17种编程语言实现排序算法-计数排序_开发工具_17

16. C++

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

void sort(int *array, int count)
{
int min = array[0];
int max = min;
for (int i = 1; i < count; i++)
{
if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
int countingLen = max - min + 1;
int *counting = new int[countingLen];
for (int i = 0; i < count; i++)
{
counting[array[i] - min] += 1;
}
int index = -1;
for (int i = 0; i < countingLen; i++)
{
for (int j = 0; j < counting[i]; j++)
{
index++;
array[index] = i + min;
}
}
delete[] counting;
}

17种编程语言实现排序算法-计数排序_i++_18

17. C#

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

private static void Sort(int[] array)
{
if (array.Length == 0) return;
int min = array[0];
int max = min;
foreach (int num in array)
{
if (num > max)
{
max = num;
}
else if (num < min)
{
min = num;
}
}
int[] counting = new int[max - min + 1];
for (int i = 0; i < array.Length; i++)
{
counting[array[i] - min] += 1;
}
int index = -1;
for (int i = 0; i < counting.Length; i++)
{
for (int j = 0; j < counting[i]; j++)
{
index++;
array[index] = i + min;
}
}
}

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

关于

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


标签:counting,17,编程语言,min,int,max,++,array,排序
From: https://blog.51cto.com/hspbc/6025591

相关文章

  • 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......
  • Js学习之 ----- 数组sort()排序
    数组的sort()方法会把数组中的元素转为字符串,然后根据字符串首位字符的Unicode码(或ASCII码)值来排序【默认从小到大】【ps:ASCII码是Unicode码的子集~】1、没有参数的情......