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

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

时间:2023-01-29 11:37:17浏览次数:68  
标签:tmp 17 编程语言 int 堆排序 length var adjustHeap array


开源地址

​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种编程语言实现排序算法-堆排序_Java_02

1. 安卓Java版

开发工具:​​下载Android Studio​​

private void sort(int[] array) {
for (int i = array.length / 2 - 1; i >= 0; i--) {
adjustHeap(array, i, array.length);
}
for (int j = array.length - 1; j > 0; j--) {
int temp = array[0];
array[0] = array[j];
array[j] = temp;
adjustHeap(array, 0, j);
}
}

private void adjustHeap(int[] array, int i, int length) {
int tmp = array[i];
for (int j = i * 2 + 1; j < length; j = j * 2 + 1) {
if (j + 1 < length && array[j] < array[j + 1]) {
j++;
}
if (array[j] > tmp) {
array[i] = array[j];
i = j;
} else {
break;
}
}
array[i] = tmp;
}

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

2. 安卓Kotlin版

开发工具:​​下载Android Studio​​

private fun sort(array: IntArray) {
for (i in array.size / 2 - 1 downTo 0) {
adjustHeap(array, i, array.size)
}
for (j in array.size - 1 downTo 1) {
val temp = array[0]
array[0] = array[j]
array[j] = temp
adjustHeap(array, 0, j)
}
}

private fun adjustHeap(array: IntArray, _index: Int, length: Int) {
var index = _index
val tmp = array[index]
var j = index * 2 + 1
while (j < length) {
if (j + 1 < length && array[j] < array[j + 1]) {
j++
}
if (array[j] > tmp) {
array[index] = array[j]
index = j
} else {
break
}
j = j * 2 + 1
}
array[index] = tmp
}

17种编程语言实现排序算法-堆排序_开发工具_04

3. NodeJS

开发工具:​​下载Visual Studio Code​​

function sort() {
let array = [2, 1, 5, 4, 3]
for (var i = Math.floor(array.length / 2) - 1; i >= 0; i--) {
adjustHeap(array, i, array.length)
}
for (var j = array.length - 1; j > 0; j--) {
var temp = array[0]
array[0] = array[j]
array[j] = temp
adjustHeap(array, 0, j)
}
return convertToStr(array)
}
function adjustHeap(array, i, length) {
var tmp = array[i]
for (var j = i * 2 + 1; j < length; j = j * 2 + 1) {
if (j + 1 < length && array[j] < array[j + 1]) {
j++
}
if (array[j] > tmp) {
array[i] = array[j]
i = j
} else {
break
}
}
array[i] = tmp
}

17种编程语言实现排序算法-堆排序_排序算法_05

4. Php

开发工具:​​下载Visual Studio Code​​

public function sort()
{
$array = [2, 1, 5, 4, 3];
$len = count($array);
for ($i = floor($len / 2) - 1; $i >= 0; $i--) {
$this->adjustHeap($array, $i, $len);
}
for ($j = $len - 1; $j > 0; $j--) {
$this->swap($array, 0, $j);
$this->adjustHeap($array, 0, $j);
}
return var_dump($array);
}

private function adjustHeap(&$array, $i, $length)
{
$tmp = $array[$i];
for ($k = 2 * $i + 1; $k < $length; $k = 2 * $k + 1) {
if ($k + 1 < $length && $array[$k] < $array[$k + 1]) {
$k++;
}
if ($tmp < $array[$k]) {
$array[$i] = $array[$k];
$i = $k;
} else {
break;
}

}
$array[$i] = $tmp;
}

17种编程语言实现排序算法-堆排序_算法_06

5. Python

开发工具:​​下载PyCharm​​

array = [2, 1, 5, 4, 3]


def heap_sort(n, i):
largest = i
left = 2 * i + 1
right = 2 * i + 2

if left < n and array[i] < array[left]:
largest = left

if right < n and array[largest] < array[right]:
largest = right

if largest != i:
array[i], array[largest] = array[largest], array[i] # 交换

heap_sort(n, largest)


def sort():
n = len(array)

for i in range(n, -1, -1):
heap_sort(n, i)

for i in range(n - 1, 0, -1):
array[i], array[0] = array[0], array[i]
heap_sort(i, 0)

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

6. Swift(SwiftUI版)

开发工具:XCode(mac电脑自带)

var array = [2, 1, 5, 4, 3]
func sort() {
var i = array.count / 2 - 1

while i >= 0
{
adjustHeap(_startIndex:i, length:array.count);
i -= 1
}

i = array.count - 1
while i > 0
{
let temp = array[0];
array[0] = array[i];
array[i] = temp;
adjustHeap(_startIndex: 0, length:i);
i -= 1
}
}

func adjustHeap( _startIndex:Int, length:Int) {
var startIndex = _startIndex
let tmp = array[startIndex];

var j = startIndex * 2 + 1

while j < length
{
if (j + 1 < length && array[j] < array[j + 1]) {
j += 1;
}
if (array[j] > tmp) {
array[startIndex] = array[j];
startIndex = j;
} else {
break;
}
j = j * 2 + 1
}

array[startIndex] = tmp;
}

17种编程语言实现排序算法-堆排序_排序算法_08

7. uni-app

开发工具:​​下载HBuilderX​​

sort() {
let array = [2, 1, 5, 4, 3]
for (var i = Math.floor(array.length / 2) - 1; i >= 0; i--) {
this.adjustHeap(array, i, array.length)
}
for (var j = array.length - 1; j > 0; j--) {
var temp = array[0]
array[0] = array[j]
array[j] = temp
this.adjustHeap(array, 0, j)
}
this.array = array
this.arrayStr = this.convertToStr(array)
},
adjustHeap(array: number[], i: number, length: number) {
var tmp = array[i]
for (var j = i * 2 + 1; j < length; j = j * 2 + 1) {
if (j + 1 < length && array[j] < array[j + 1]) {
j++
}
if (array[j] > tmp) {
array[i] = array[j]
i = j
} else {
break
}
}
array[i] = tmp
}

17种编程语言实现排序算法-堆排序_Math_09

8. vue

开发工具:​​下载Visual Studio Code​​

sort() {
let array = [2, 1, 5, 4, 3]
for (var i = Math.floor(array.length / 2) - 1; i >= 0; i--) {
this.adjustHeap(array, i, array.length)
}
for (var j = array.length - 1; j > 0; j--) {
var temp = array[0]
array[0] = array[j]
array[j] = temp
this.adjustHeap(array, 0, j)
}
this.array = array
this.arrayStr = this.convertToStr(array)
},
adjustHeap(array: number[], i: number, length: number) {
var tmp = array[i]
for (var j = i * 2 + 1; j < length; j = j * 2 + 1) {
if (j + 1 < length && array[j] < array[j + 1]) {
j++
}
if (array[j] > tmp) {
array[i] = array[j]
i = j
} else {
break
}
}
array[i] = tmp
}

17种编程语言实现排序算法-堆排序_Math_10

9. 微信小程序

开发工具:​​下载微信开发者工具​​

sort() {
let array = [2, 1, 5, 4, 3]
for (var i = Math.floor(array.length / 2) - 1; i >= 0; i--) {
this.adjustHeap(array, i, array.length)
}
for (var j = array.length - 1; j > 0; j--) {
var temp = array[0]
array[0] = array[j]
array[j] = temp
this.adjustHeap(array, 0, j)
}
this.setData({
array: array,
arrayStr: this.convertToStr(array),
})
},
adjustHeap(array: number[], i: number, length: number) {
var tmp = array[i]
for (var j = i * 2 + 1; j < length; j = j * 2 + 1) {
if (j + 1 < length && array[j] < array[j + 1]) {
j++
}
if (array[j] > tmp) {
array[i] = array[j]
i = j
} else {
break
}
}
array[i] = tmp
}

17种编程语言实现排序算法-堆排序_排序算法_11

10. 鸿蒙(ArkTS)

开发工具:​​下载DevEco Studio​

sort() {
let array = [2, 1, 5, 4, 3]
for (var i = Math.floor(array.length / 2) - 1; i >= 0; i--) {
this.adjustHeap(array, i, array.length)
}
for (var j = array.length - 1; j > 0; j--) {
var temp = array[0]
array[0] = array[j]
array[j] = temp
this.adjustHeap(array, 0, j)
}
this.array = array
}
adjustHeap(array: number[], i: number, length: number) {
var tmp = array[i]
for (var j = i * 2 + 1; j < length; j = j * 2 + 1) {
if (j + 1 < length && array[j] < array[j + 1]) {
j++
}
if (array[j] > tmp) {
array[i] = array[j]
i = j
} else {
break
}
}
array[i] = tmp
}

17种编程语言实现排序算法-堆排序_开发工具_12

11. Go语言

开发工具:​​下载Visual Studio Code​​

var array = []int{2, 1, 5, 4, 3}

func adjustHeap(currentIndex int, maxLength int) {
var noLeafValue = array[currentIndex]
for j := 2*currentIndex + 1; j <= maxLength; j = currentIndex*2 + 1 {
if j < maxLength && array[j] < array[j+1] {
j++
}
if noLeafValue >= array[j] {
break
}
array[currentIndex] = array[j]
currentIndex = j
}
array[currentIndex] = noLeafValue
}

func createHeap(length int) {
for i := length / 2; i >= 0; i-- {
adjustHeap(i, length-1)
}
}

func sort(array []int) {
var length = len(array)
createHeap(length)
for i := length - 1; i > 0; i-- {
array[0], array[i] = array[i], array[0]
adjustHeap(0, i-1)
}
}

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

12. Java

开发工具:​​下载IntelliJ IDEA​​

private static void sort(int[] array) {
for (int i = array.length / 2 - 1; i >= 0; i--) {
adjustHeap(array, i, array.length);
}
for (int j = array.length - 1; j > 0; j--) {
int temp = array[0];
array[0] = array[j];
array[j] = temp;
adjustHeap(array, 0, j);
}
}

private static void adjustHeap(int[] array, int i, int length) {
int tmp = array[i];
for (int j = i * 2 + 1; j < length; j = j * 2 + 1) {
if (j + 1 < length && array[j] < array[j + 1]) {
j++;
}
if (array[j] > tmp) {
array[i] = array[j];
i = j;
} else {
break;
}
}
array[i] = tmp;
}

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

13. Kotlin

开发工具:​​下载IntelliJ IDEA​​

private fun sort(array: IntArray) {
for (i in array.size / 2 - 1 downTo 0) {
adjustHeap(array, i, array.size)
}
for (j in array.size - 1 downTo 1) {
val temp = array[0]
array[0] = array[j]
array[j] = temp
adjustHeap(array, 0, j)
}
}

private fun adjustHeap(array: IntArray, _index: Int, length: Int) {
var index = _index
val tmp = array[index]
var j = index * 2 + 1
while (j < length) {
if (j + 1 < length && array[j] < array[j + 1]) {
j++
}
if (array[j] > tmp) {
array[index] = array[j]
index = j
} else {
break
}
j = j * 2 + 1
}
array[index] = tmp
}

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

14. Flutter

开发工具:​​下载IntelliJ IDEA​​

void sort(List<int> array) {
var startIndex = (array.length / 2 - 1).toInt();
for (int i = startIndex; i >= 0; i--) {
adjustHeap(array, i, array.length);
}
for (int j = array.length - 1; j > 0; j--) {
int temp = array[0];
array[0] = array[j];
array[j] = temp;
adjustHeap(array, 0, j);
}
}

void adjustHeap(List<int> array, int startIndex, int length) {
int tmp = array[startIndex];
for (int j = startIndex * 2 + 1; j < length; j = j * 2 + 1) {
if (j + 1 < length && array[j] < array[j + 1]) {
j++;
}
if (array[j] > tmp) {
array[startIndex] = array[j];
startIndex = j;
} else {
break;
}
}
array[startIndex] = tmp;
}

17种编程语言实现排序算法-堆排序_算法_16

15. C语言

开发工具:​​下载Visual Studio​​

void adjustHeap(int* array, int i, int length)
{
int tmp = array[i];
for (int j = i * 2 + 1; j < length; j = j * 2 + 1)
{
if (j + 1 < length && array[j] < array[j + 1])
{
j++;
}
if (array[j] > tmp)
{
array[i] = array[j];
i = j;
}
else
{
break;
}
}
array[i] = tmp;
}

void sort(int* array, int count)
{
for (int i = count / 2 - 1; i >= 0; i--)
{
adjustHeap(array, i, count);
}
for (int j = count - 1; j > 0; j--)
{
int temp = array[0];
array[0] = array[j];
array[j] = temp;
adjustHeap(array, 0, j);
}
}

17种编程语言实现排序算法-堆排序_Java_17

16. C++

开发工具:​​下载Visual Studio​​

void adjustHeap(int* array, int i, int length)
{
int tmp = array[i];
for (int j = i * 2 + 1; j < length; j = j * 2 + 1)
{
if (j + 1 < length && array[j] < array[j + 1])
{
j++;
}
if (array[j] > tmp)
{
array[i] = array[j];
i = j;
}
else
{
break;
}
}
array[i] = tmp;
}

void sort(int* array, int count)
{
for (int i = count / 2 - 1; i >= 0; i--)
{
adjustHeap(array, i, count);
}
for (int j = count - 1; j > 0; j--)
{
int temp = array[0];
array[0] = array[j];
array[j] = temp;
adjustHeap(array, 0, j);
}
}

17种编程语言实现排序算法-堆排序_开发工具_18

17. C#

开发工具:​​下载Visual Studio​​

private static void Sort(int[] array)
{
for (int i = array.Length / 2 - 1; i >= 0; i--)
{
AdjustHeap(array, i, array.Length);
}
for (int j = array.Length - 1; j > 0; j--)
{
int temp = array[0];
array[0] = array[j];
array[j] = temp;
AdjustHeap(array, 0, j);
}
}

private static void AdjustHeap(int[] array, int i, int length)
{
int tmp = array[i];
for (int j = i * 2 + 1; j < length; j = j * 2 + 1)
{
if (j + 1 < length && array[j] < array[j + 1])
{
j++;
}
if (array[j] > tmp)
{
array[i] = array[j];
i = j;
}
else
{
break;
}
}
array[i] = tmp;
}

17种编程语言实现排序算法-堆排序_Java_19

关于

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


标签:tmp,17,编程语言,int,堆排序,length,var,adjustHeap,array
From: https://blog.51cto.com/hspbc/6025595

相关文章

  • 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、......
  • 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......
  • 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可以......