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

17种编程语言实现排序算法-快速排序开源地址https://gitee.com/lblbc/simple-works/tree/master/sort/1. 安卓Java版priv

大家好,欢迎来到IT知识分享网。

开源地址

https://gitee.com/lblbc/simple-works/tree/master/sort/

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

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

1. 安卓Java

private static void sort(int[] array) { sortMe(array, 0, array.length - 1); } private static void sortMe(int[] array, int low, int high) { if (low >= high) { return; } int pivot = array[low]; int l = low; int r = high; int tmp; while (l < r) { while (l < r && array[r] >= pivot) { r--; } while (l < r && array[l] <= pivot) { l++; } if (l < r) { tmp = array[l]; array[l] = array[r]; array[r] = tmp; } } array[low] = array[l]; array[l] = pivot; if (low < l) { sortMe(array, low, l - 1); } if (r < high) { sortMe(array, r + 1, high); } } 
17种编程语言实现排序算法-快速排序

2. 安卓Kotlin版

private fun sort(array: IntArray) { sortMe(array, 0, array.size - 1) } private fun sortMe(array: IntArray, low: Int, high: Int) { if (low >= high) { return } val pivot = array[low] var l = low var r = high var tmp: Int while (l < r) { while (l < r && array[r] >= pivot) { r-- } while (l < r && array[l] <= pivot) { l++ } if (l < r) { tmp = array[l] array[l] = array[r] array[r] = tmp } } array[low] = array[l] array[l] = pivot if (low < l) { sortMe(array, low, l - 1) } if (r < high) { sortMe(array, r + 1, high) } } 
17种编程语言实现排序算法-快速排序

3. NodeJS

function sort() { sortMe(0, array.length - 1); return convertToStr(array); } function sortMe(slow, fast) { let base = array[slow]; array[slow] = 0; let left = slow; let right = fast; while (left < right) { if (array[left] === 0) { if (array[right] < base) { array[left] = array[right]; array[right] = 0; left = left + 1; } else { right = right - 1; } } else if (array[right] === 0) { if (array[left] >= base) { array[right] = array[left]; array[left] = 0; right = right - 1; } else { left = left + 1; } } } array[left] = base; if ((left - 1) - slow > 0) { sortMe(slow, left - 1); } if (fast - (right + 1) > 0) { sortMe(right + 1, fast); } return } 
17种编程语言实现排序算法-快速排序

4. Php

public function sort() { $array = [2, 1, 5, 4, 3]; $result = $this->sortMe($array); return var_dump($result); } public function sortMe($arr) { $length = count($arr); if (!is_array($arr) || $length <= 1) { return $arr; } $baseValue = $arr[0]; $leftArr = array(); $rightArr = array(); for ($i = 1; $i < $length; $i++) { if ($arr[$i] < $baseValue) { $leftArr[] = $arr[$i]; } else { $rightArr[] = $arr[$i]; } } $leftArr = $this->sortMe($leftArr); $rightArr = $this->sortMe($rightArr); return array_merge($leftArr, array($baseValue), $rightArr); } 
17种编程语言实现排序算法-快速排序

5. Python

array = [2, 1, 5, 4, 3] def sort(array, low, high): if low >= high: return array i = low j = high pivot = array[low] while i < j: while i < j and array[j] > pivot: j -= 1 array[i] = array[j] while i < j and array[i] < pivot: i += 1 array[j] = array[i] array[j] = pivot sort(array, low, j - 1) sort(array, j + 1, high) return 
17种编程语言实现排序算法-快速排序

6. Swift(SwiftUI版)

var array = [2, 1, 5, 4, 3] func sort() { sortMe(array: &array, left: 0, right: array.count - 1) } func sortMe(array: inout [Int], left: Int, right: Int) { if right - left <= 0 { return } var flagIndex = left let flagValue = array[left] for index in stride(from: left + 1, to: right + 1, by: 1) { let value = array[index] if value < flagValue { array[flagIndex] = value flagIndex += 1 array[index] = array[flagIndex] array[flagIndex] = flagValue } } sortMe(array: &array, left: left, right: flagIndex - 1) sortMe(array: &array, left: flagIndex + 1, right: right) } 
17种编程语言实现排序算法-快速排序

7. uni-app

sort() { let array = this.array; this.sortMe(array, 0, array.length - 1); this.array = array this.arrayStr = this.convertToStr(array) }, sortMe(array: number[], low: number, high: number) { if (low >= high) { return; } var index = array[low]; var i = low; var j = high; while (i < j) { while (i < j && array[j] >= index) { j--; } if (i < j) { array[i] = array[j]; i++; } while (i < j && array[i] < index) { i++; } if (i < j) { array[j] = array[i]; j--; } } array[i] = index; this.sortMe(array, low, i - 1); this.sortMe(array, i + 1, high); }, 
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 = this.data.array; this.sortMe(array, 0, array.length - 1); this.setData({ array: array, arrayStr: this.convertToStr(array), }) }, sortMe(array: number[], low: number, high: number) { if (low >= high) { return; } var index = array[low]; var i = low; var j = high; while (i < j) { while (i < j && array[j] >= index) { j--; } if (i < j) { array[i] = array[j]; i++; } while (i < j && array[i] < index) { i++; } if (i < j) { array[j] = array[i]; j--; } } array[i] = index; this.sortMe(array, low, i - 1); this.sortMe(array, i + 1, high); }, 
17种编程语言实现排序算法-快速排序

10. 鸿蒙(ArkTS)

 sort() { let array = this.array; this.sortMe(array, 0, array.length - 1); this.array = array } sortMe(array: number[], low: number, high: number) { if (low >= high) { return; } var index = array[low]; var i = low; var j = high; while (i < j) { while (i < j && array[j] >= index) { j--; } if (i < j) { array[i] = array[j]; i++; } while (i < j && array[i] < index) { i++; } if (i < j) { array[j] = array[i]; j--; } } array[i] = index; this.sortMe(array, low, i - 1); this.sortMe(array, i + 1, high); } 
17种编程语言实现排序算法-快速排序

11. Go语言

func sort(left int, right int, array *[5]int) { l := left r := right pivot := array[(left+right)/2] tmp := 0 for l < r { for array[l] < pivot { l++ } for array[r] > pivot { r-- } if l >= r { break } tmp = array[l] array[l] = array[r] array[r] = tmp if array[l] == pivot { r-- } if array[r] == pivot { l++ } } if l == r { l++ r-- } if left < r { sort(left, r, array) } if right > l { sort(l, right, array) } } 
17种编程语言实现排序算法-快速排序

12. Java

private static void sort(int[] array) { sortMe(array, 0, array.length - 1); } public static void sortMe(int[] array, int low, int high) { if (low >= high) { return; } int pivot = array[low]; int l = low; int r = high; int tmp; while (l < r) { while (l < r && array[r] >= pivot) { r--; } while (l < r && array[l] <= pivot) { l++; } if (l < r) { tmp = array[l]; array[l] = array[r]; array[r] = tmp; } } array[low] = array[l]; array[l] = pivot; if (low < l) { sortMe(array, low, l - 1); } if (r < high) { sortMe(array, r + 1, high); } } 
17种编程语言实现排序算法-快速排序

13. Kotlin

private fun sort(array: IntArray) { sortMe(array, 0, array.size - 1) } fun sortMe(array: IntArray, low: Int, high: Int) { if (low >= high) { return } val pivot = array[low] var l = low var r = high var tmp: Int while (l < r) { while (l < r && array[r] >= pivot) { r-- } while (l < r && array[l] <= pivot) { l++ } if (l < r) { tmp = array[l] array[l] = array[r] array[r] = tmp } } array[low] = array[l] array[l] = pivot if (low < l) { sortMe(array, low, l - 1) } if (r < high) { sortMe(array, r + 1, high) } } 
17种编程语言实现排序算法-快速排序

14. Flutter

15. C语言

 void sortMe(int* array, int low, int high) { if (low >= high) { return; } int index = array[low]; int i = low; int j = high; while (i < j) { while (i < j && array[j] >= index) { j--; } if (i < j) { array[i] = array[j]; i++; } while (i < j && array[i] < index) { i++; } if (i < j) { array[j] = array[i]; j--; } } array[i] = index; sortMe(array, low, i - 1); sortMe(array, i + 1, high); } void sort(int* array, int count) { sortMe(array, 0, count - 1); } 
17种编程语言实现排序算法-快速排序

16. C++

 void sortMe(int* array, int low, int high) { if (low >= high) { return; } int index = array[low]; int i = low; int j = high; while (i < j) { while (i < j && array[j] >= index) { j--; } if (i < j) { array[i] = array[j]; i++; } while (i < j && array[i] < index) { i++; } if (i < j) { array[j] = array[i]; j--; } } array[i] = index; sortMe(array, low, i - 1); sortMe(array, i + 1, high); } void sort(int* array, int count) { sortMe(array, 0, count - 1); } 
17种编程语言实现排序算法-快速排序

17. C#

private static void Sort(int[] array) { SortMe(array, 0, array.Length - 1); } private static void SortMe(int[] array, int low, int high) { if (low >= high) { return; } int index = array[low]; int i = low; int j = high; while (i < j) { while (i < j && array[j] >= index) { j--; } if (i < j) { array[i] = array[j]; i++; } while (i < j && array[i] < index) { i++; } if (i < j) { array[j] = array[i]; j--; } } array[i] = index; SortMe(array, low, i - 1); SortMe(array, i + 1, high); } 
17种编程语言实现排序算法-快速排序

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/50432.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信