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