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

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

时间:2023-01-19 22:34:51浏览次数:59  
标签:sortMe 17 编程语言 int high while low array 排序

开源地址

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

image

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

image

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 | 小程序 | 鸿蒙
公众号:蓝不蓝编程

标签:sortMe,17,编程语言,int,high,while,low,array,排序
From: https://blog.51cto.com/hspbc/6020177

相关文章

  • 了解冒泡排序,并写出一个函数进行排序,拍成升序
    冒泡排序:它的基本思想是对所有相邻记录的关键字值进行比效,如果是逆顺(a[j]>a[j+1]),则将其交换,最终达到有序化。 当有n个数时,会进行n-1趟冒泡排序,第一趟排n-1次,第二趟排n-2次......
  • CF1768B 题解
    首先我们思考什么数是不用排序的。显然,序列中一个由\(1\)开始,每次递增\(1\)的子序列是不用排序的。为什么呢?因为我们把其他数按从大到小排好后这个子序列刚好构成排......
  • CodeForces 1783F Double Sort II
    洛谷传送门CF传送门考虑只有一个排列怎么做。有一个结论是答案为\(n\-\)置换环个数,即每个环都会选择一个点不操作,其他点都操作。接下来考虑两个排列,显然当\(x\)在......
  • 新版本S1730系列交换机配置telnet
    当前新版本交换机为了安全新增了相关安全特性,对应的配置telnet的步骤参考如下第一步:开启telnet服务<Huawei>system-view [Huawei]telnet server enable  第二......
  • 快速排序and归并排序
    1.快速排序快速排序的思想分治确定轴值(分界点),可以是q[l]、q[l+r>>1](建议用这个)、q[r]根据轴值划分递归左右子划分快排结束即已经是合并完的情况,所以已经完成子......
  • FMC子卡设计资料原理图:FMC177-基于AD9361的双收双发射频FMC子卡
    FMC177-基于AD9361的双收双发射频FMC子卡     一、板卡介绍       FMC177射频模块分别包含两个接收通道与发射通道,其频率可覆盖达到70MHz......
  • CF1768C 题解
    考虑构造。首先第一轮构造我们把第一次出现的数放到\(p\)里面,第二次出现的放到\(q\)里面。如果有数第三次出现呢?那么显然无解。那么现在\(p\)和\(q\)中都填了一......
  • 常见排序算法之快速排序
    文章目录​​1、概述​​​​2、代码实现​​​​3、测试案例​​1、概述快速排序(Quicksort)是对冒泡排序的一种改进。快速排序由C.A.R.Hoare在1960年提出。它的基本思想......
  • 常见排序算法之基数排序
    文章目录​​1、概述​​​​2、测试代码​​​​3、测试小案例​​1、概述基数排序(radixsort)属于“分配式排序”(distributionsort),又称“桶子法”(bucketsort)或binsort,顾......
  • 常见排序算法之归并排序
    文章目录​​1、概述​​​​2、测试代码​​​​3、小案例​​1、概述归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(DivideandConquer)......