开源地址
https://gitee.com/lblbc/simple-works/tree/master/sort/
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);
}
}
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)
}
}
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
}
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);
}
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
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)
}
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);
},
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
}
}
}
}
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);
},
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);
}
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)
}
}
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);
}
}
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)
}
}
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);
}
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. 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);
}
关于
厦门大学计算机专业|华为八年高级工程师
专注《零基础学编程系列》 http://lblbc.cn/blog
包含:Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
公众号:蓝不蓝编程