开源地址
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++)、鸿蒙
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;
}
}
}
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
}
}
}
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
}
}
}
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);
}
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)
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
}
}
}
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)
},
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)
},
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
}
}
},
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
}
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++
}
}
}
}
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;
}
}
}
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
}
}
}
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;
}
}
}
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);
}
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. 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;
}
}
}
关于
厦门大学计算机专业|华为八年高级工程师
专注《零基础学编程系列》 http://lblbc.cn/blog
包含:Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
公众号:蓝不蓝编程