17种编程语言实现排序算法-冒泡排序

时间:2023-01-18 22:59:46

开源地址

https://gitee.com/lblbc/simple-works/tree/master/sort/bubbleSort 17种编程语言实现排序算法-冒泡排序17种编程语言实现排序算法-冒泡排序

1. 安卓Java版

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

17种编程语言实现排序算法-冒泡排序

2. 安卓Kotlin版

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

17种编程语言实现排序算法-冒泡排序

3. NodeJS

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

17种编程语言实现排序算法-冒泡排序

4. Php

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

17种编程语言实现排序算法-冒泡排序

5. Python

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]

17种编程语言实现排序算法-冒泡排序

6. Swift(SwiftUI版)

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

17种编程语言实现排序算法-冒泡排序

7. uni-app

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

17种编程语言实现排序算法-冒泡排序

8. vue

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

17种编程语言实现排序算法-冒泡排序

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

17种编程语言实现排序算法-冒泡排序

10. 鸿蒙(ArkTS)


11. Go语言

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

17种编程语言实现排序算法-冒泡排序

12. Java

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

17种编程语言实现排序算法-冒泡排序

13. Kotlin

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

17种编程语言实现排序算法-冒泡排序

14. Flutter

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

17种编程语言实现排序算法-冒泡排序

15. C语言

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种编程语言实现排序算法-冒泡排序

16. C++

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种编程语言实现排序算法-冒泡排序

17. C#

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

17种编程语言实现排序算法-冒泡排序

关于

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