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