c++1178:成绩排序不用结构体
#include <string> #include <algorithm> using namespace std; void QuickSort(string name[], float score[], int left, int right) { // int pivot = score[right]; int pivot = score[rand() % (right - left) + left]; int tmp = left - 1; for (int j = left; j < right; j++) { if (score[j] < pivot) { tmp++; swap(score[j], score[tmp]); swap(name[j], name[tmp]); } } tmp++; swap(score[right], score[tmp]); swap(name[right], name[tmp]); if (left < tmp - 1) QuickSort(name, score, left, tmp - 1); if (tmp + 1 < right) QuickSort(name, score, tmp + 1, right); }
string name[] = {"a", "b", "c", "d", "e"}; float score[] = {1, 2, 40, 3, 9}; QuickSort(name, score, 0, 4);
[此贴子已经被作者于2022-8-13 00:47编辑过]