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

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

时间:2023-01-29 11:37:04浏览次数:68  
标签:sort insertIndex 17 编程语言 int 插入排序 var array insertVal


开源地址

​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 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;
}
}

17种编程语言实现排序算法-插入排序_开发工具_03

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
}
}

17种编程语言实现排序算法-插入排序_Visual_04

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
}
}

17种编程语言实现排序算法-插入排序_i++_05

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);
}

17种编程语言实现排序算法-插入排序_开发工具_06

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

17种编程语言实现排序算法-插入排序_算法_07

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;
}

}

17种编程语言实现排序算法-插入排序_Visual_08

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
}
}

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

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
}
},

17种编程语言实现排序算法-插入排序_算法_10

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
}

},

17种编程语言实现排序算法-插入排序_Visual_11

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
}

17种编程语言实现排序算法-插入排序_算法_12

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
}
}

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

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;
}
}

17种编程语言实现排序算法-插入排序_算法_14

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
}
}

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

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;
}
}

17种编程语言实现排序算法-插入排序_开发工具_16

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;
}
}

17种编程语言实现排序算法-插入排序_i++_17

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种编程语言实现排序算法-插入排序_i++_18

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;
}
}

17种编程语言实现排序算法-插入排序_排序算法_19

关于

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


标签:sort,insertIndex,17,编程语言,int,插入排序,var,array,insertVal
From: https://blog.51cto.com/hspbc/6025596

相关文章

  • 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......
  • 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......
  • 插入排序
    插入排序,在待排序列表中将第一个看作一个有序列表,循环n-1次,将每次的值插入已经排好序的列表中。 代码实现#-*-coding=utf-8-*-#@Author:Wchime......
  • 17、Ubuntu安装docker
    1、先卸载旧版,如果没有的话,就不用执行了,直接第二步。apt-getremovedockerdocker-enginedocker.iocontainerdrunc2、在终端输入aptupdateapt-getinstallca-......
  • macos:安装java 17.0.6(android studio报错:Unable to locate a Java Runtime.)
    一,报错信息:androidstudio的报错信息:Theoperationcouldn’tbecompleted.UnabletolocateaJavaRuntime.Pleasevisithttp://www.java.comforinformation......
  • 华硕ROG玩家国度枪神2 plus屏幕校准调色 CMN1747
    我这个版本的枪神2plus的屏幕色域很广,131%sRGB,虽然色彩丰富,但是看起来感觉很费眼,有些动画看多了还头晕,必须得校准显示器+调色  这是老版的屏幕,奇美CMN1747,屏库上面名......
  • 运维新手一定不能错过的17 个技巧
    1、查找当前目录下所有以.tar结尾的文件然后移动到指定目录:find.-name“*.tar”-execmv{}./backup/;❝注解:find–name主要用于查找某个文件名字,-exec、xargs可以......