Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
134827 | why | 双关键字排序 | C++ | 编译错误 | 0 | 0 MS | 0 KB | 1345 | 2024-03-02 21:11:47 |
#include<bits/stdc++.h> using namespace std; //-----------------------快排-------------------/ int HoareSort(int* a, int begain, int end) { int key = begain; int left = begain, right = end; while (left < right) { //右边找比key小的值 while (left < right && a[right] >= a[key]) right--; //左边找比key大的值 while (left < right && a[left] <= a[key]) left++; swap(&a[right], &a[left]); } int meeti = left; swap(&a[meeti], &a[key]); return meeti; } void QuickSort(int* a, int begain, int end) { //递归返回条件如果该区间只有一个值或没有值则返回 if (begain >= end) return; int meeti = HoareSort(a, begain, end); QuickSort(a, begain, meeti - 1); QuickSort(a, meeti + 1, end); } int main(){ int n; cin>>n; def quick_sort(arr): if len(arr) <= 1: return arr pivot = arr[0] left = [x for x in arr[1:] if x[0] < pivot[0] or (x[0] == pivot[0] and x[1] < pivot[1])] right = [x for x in arr[1:] if x[0] > pivot[0] or (x[0] == pivot[0] and x[1] >= pivot[1])] return quick_sort(left) + [pivot] + quick_sort(right) //示例输入 pairs = [(3, 2), (1, 4), (2, 3), (1, 2), (3, 1)] //调用快速排序函数进行排序 sorted_pairs = quick_sort(pairs) //输出排序结果 print(sorted_pairs) return 0; }