给定一个长度为n的数组,从小到大输出每个数。
func quickSort(arr []int) []int { return _quickSort(arr, 0, len(arr)-1) } func _quickSort(arr []int, left, right int) []int { if left < right { partitionIndex := partition(arr, left, right) _quickSort(arr, left, partitionIndex-1) _quickSort(arr, partitionIndex+1, right) } return arr } func partition(arr []int, left, right int) int { pivot := left index := pivot + 1 for i := index; i <= right; i++ { if arr[pivot] > arr[i] { arr[i], arr[index] = arr[index], arr[i] index++ } } arr[pivot], arr[index-1] = arr[index-1], arr[pivot] return index - 1 }
#include <iostream> using namespace std; int qiefen(int* a, int low, int high) { int mid = a[low]; while (low < high) { while (low < high && a[high] >= mid) { high--; } a[low] = a[high]; while (low < high && a[low] <= mid) { low++; } a[high] = a[low]; } a[low] = mid; return low; } void kuaipai(int* a, int low, int high) { if (low < high) { int mid = qiefen(a, low, high); kuaipai(a, low, mid - 1); kuaipai(a, mid + 1, high); } } int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } kuaipai(a, 0, n - 1); for (int i = 0; i < n; i++) { cout << a[i] << " " ; } } // 64 位输出请用 printf(\"%lld\")
n = int(input()) alist = [int(x) for x in input().split(' ')] # 不开辟新的空间,使用双指针引用 def fast_sort(alist, first, last): left = first right = last # 停止标志 if left >= right: return mid = alist[first] while left < right: while (left < right) and (alist[right] >= mid): right -= 1 alist[left]= alist[right] while (left < right) and (alist[left] < mid): left += 1 alist[right] = alist[left] alist[left] = mid fast_sort(alist,first, left-1) fast_sort(alist,left+1, last) fast_sort(alist,0,n-1) c = ' ' print(c.join([str(i) for i in alist]))