开源地址
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) {
for (int i = 0; i < array.length - 1; i++) {
int minIndex = i;
int min = array[i];
for (int j = i + 1; j < array.length; j++) {
if (min > array[j]) {
min = array[j];
minIndex = j;
}
}
if (minIndex != i) {
array[minIndex] = array[i];
array[i] = min;
}
}
}
image
2. 安卓Kotlin版
开发工具:下载Android Studio
private fun sort(array: IntArray) {
for (i in 0 until array.size - 1) {
var minIndex: Int = i
var min = array[i]
for (j in i + 1 until array.size) {
if (min > array[j]) {
min = array[j]
minIndex = j
}
}
if (minIndex != i) {
array[minIndex] = array[i]
array[i] = min
}
}
}
image
3. NodeJS
开发工具:下载Visual Studio Code
array = [2, 1, 5, 4, 3]
function sort() {
var len = array.length
for (let i = 0; i < len - 1; i++) {
for (let j = i; j < len; j++) {
if (array[j] < array[i]) {
let tmp = array[i]
array[i] = array[j]
array[j] = tmp
}
}
}
}
image
4. Php
开发工具:下载Visual Studio Code
public function sortMe(&$array)
{
$t = count($array);
for ($i = 0; $i < $t - 1; $i++) {
$k = $i;
for ($j = $i + 1; $j < $t; $j++) {
if ($array[$j] < $array[$k]) {
$k = $j;
}
}
if ($k != $i) {
$tmp = $array[$i];
$array[$i] = $array[$k];
$array[$k] = $tmp;
}
}
}
image
5. Python
开发工具:下载PyCharm
array = [2, 1, 5, 4, 3]
def sort():
length = len(array)
if length <= 1:
return array
for i in range(length):
min_num_index = i
for j in range(i + 1, length):
if array[j] < array[min_num_index]:
min_num_index = j
array[min_num_index], array[i] = array[i], array[min_num_index]
return array
image
6. Swift(SwiftUI版)
开发工具:XCode(mac电脑自带)
var array = [2, 1, 5, 4, 3]
func sort() {
for i in 0..<array.count {
var minValueIndex = i
for j in (minValueIndex + 1)..<array.count {
if array[minValueIndex] > array[j] {
minValueIndex = j
}
}
let tmp = array[i]
array[i] = array[minValueIndex]
array[minValueIndex] = tmp
}
}
image
7. uni-app
开发工具:下载HBuilderX
sort() {
let array = [2, 1, 5, 4, 3]
var len = array.length
for (let i = 0; i < len - 1; i++) {
for (let j = i; j < len; j++) {
if (array[j] < array[i]) {
let tmp = array[i]
array[i] = array[j]
array[j] = tmp
}
}
}
},
image
8. vue
开发工具:下载Visual Studio Code
sort() {
let array = [2, 1, 5, 4, 3]
var len = array.length
for (let i = 0; i < len - 1; i++) {
for (let j = i; j < len; j++) {
if (array[j] < array[i]) {
let tmp = array[i]
array[i] = array[j]
array[j] = tmp
}
}
}
},
image
9. 微信小程序
开发工具:下载微信开发者工具
sort() {
let array = [2, 1, 5, 4, 3]
var len = array.length
for (let i = 0; i < len - 1; i++) {
for (let j = i; j < len; j++) {
if (array[j] < array[i]) {
let tmp = array[i]
array[i] = array[j]
array[j] = tmp
}
}
}
}
image
10. 鸿蒙(ArkTS)
开发工具:下载DevEco Studio
sort() {
let array = [2, 1, 5, 4, 3]
var len = array.length
for (let i = 0; i < len - 1; i++) {
for (let j = i; j < len; j++) {
if (array[j] < array[i]) {
let tmp = array[i]
array[i] = array[j]
array[j] = tmp
}
}
}
this.array = array;
}
image
11. Go语言
开发工具:下载Visual Studio Code
func sort(arr *[5]int) {
for i := 0; i < len(arr)-1; i++ {
max := arr[i]
maxIndex := i
for j := i + 1; j < len(arr); j++ {
if max < arr[j] {
max = arr[j]
maxIndex = j
}
}
if maxIndex != i {
arr[i], arr[maxIndex] = arr[maxIndex], arr[i]
}
}
}
func main() {
array := [5]int{2, 1, 5, 4, 3}
sort(&array)
}
image
12. Java
开发工具:下载IntelliJ IDEA
private static void sort(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
int minIndex = i;
int min = array[i];
for (int j = i + 1; j < array.length; j++) {
if (min > array[j]) {
min = array[j];
minIndex = j;
}
}
if (minIndex != i) {
array[minIndex] = array[i];
array[i] = min;
}
}
}
image
13. Kotlin
开发工具:下载IntelliJ IDEA
private fun sort(array: IntArray) {
for (i in 0 until array.size - 1) {
var minIndex = i
var min = array[i]
for (j in i + 1 until array.size) {
if (min > array[j]) {
min = array[j]
minIndex = j
}
}
if (minIndex != i) {
array[minIndex] = array[i]
array[i] = min
}
}
}
image
14. Flutter
开发工具:下载IntelliJ IDEA
sort(List<int> list) {
if (list.isEmpty) {
return [];
}
for (var end = list.length - 1; end > 0; end--) {
int maxIndex = 0;
for (var begin = 1; begin <= end; begin++) {
if (list[maxIndex] <= list[begin]) {
maxIndex = begin;
}
}
int temp = list[maxIndex];
list[maxIndex] = list[end];
list[end] = temp;
}
}
image
15. C语言
开发工具:下载Visual Studio
void sort(int* array, int count)
{
for (int i = 0; i < count - 1; i++)
{
int minIndex = i;
int min = array[i];
for (int j = i + 1; j < count; j++)
{
if (min > array[j])
{
min = array[j];
minIndex = j;
}
}
if (minIndex != i)
{
array[minIndex] = array[i];
array[i] = min;
}
}
}
int main()
{
int array[5] = { 2, 1, 5, 4, 3 };
int count = sizeof(array) / sizeof(int);
sort(array, count);
return 0;
}
image
16. C++
开发工具:下载Visual Studio
void sort(int* array, int count)
{
for (int i = 0; i < count - 1; i++)
{
int minIndex = i;
int min = array[i];
for (int j = i + 1; j < count; j++)
{
if (min > array[j])
{
min = array[j];
minIndex = j;
}
}
if (minIndex != i)
{
array[minIndex] = array[i];
array[i] = min;
}
}
}
int main()
{
int array[5] = { 2, 1, 5, 4, 3 };
int count = sizeof(array) / sizeof(int);
sort(array,count);
return 0;
}
image
17. C#
开发工具:下载Visual Studio
private static void Sort(int[] array)
{
for (int i = 0; i < array.Length - 1; i++)
{
int minIndex = i;
int min = array[i];
for (int j = i + 1; j < array.Length; j++)
{
if (min > array[j])
{
min = array[j];
minIndex = j;
}
}
if (minIndex != i)
{
array[minIndex] = array[i];
array[i] = min;
}
}
}
image
关于
厦门大学计算机专业|华为八年高级工程师
专注《零基础学编程系列》 http://lblbc.cn/blog
包含:Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
公众号:蓝不蓝编程
网友评论