From 626c4952cda70a3ad71592bc8813957ea366ce31 Mon Sep 17 00:00:00 2001 From: huihut Date: Tue, 6 Nov 2018 22:13:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A1=E6=95=B0=E6=8E=92=E5=BA=8F=E4=B8=AD?= =?UTF-8?q?=E8=AE=A1=E6=95=B0=E5=AE=B9=E5=99=A8=E9=87=8D=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E4=B8=BA=20vecCount?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Algorithm/CountSort.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Algorithm/CountSort.cpp b/Algorithm/CountSort.cpp index a3666a9..159e811 100644 --- a/Algorithm/CountSort.cpp +++ b/Algorithm/CountSort.cpp @@ -32,19 +32,19 @@ void CountSort(vector& vecRaw, vector& vecObj) // 使用 vecRaw 的最大值 + 1 作为计数容器 countVec 的大小 int vecCountLength = (*max_element(begin(vecRaw), end(vecRaw))) + 1; - vector countVec(vecCountLength, 0); + vector vecCount(vecCountLength, 0); // 统计每个键值出现的次数 for (int i = 0; i < vecRaw.size(); i++) - countVec[vecRaw[i]]++; + vecCount[vecRaw[i]]++; // 后面的键值出现的位置为前面所有键值出现的次数之和 for (int i = 1; i < vecCountLength; i++) - countVec[i] += countVec[i - 1]; + vecCount[i] += vecCount[i - 1]; // 将键值放到目标位置 for (int i = vecRaw.size(); i > 0; i--) // 此处逆序是为了保持相同键值的稳定性 - vecObj[--countVec[vecRaw[i - 1]]] = vecRaw[i - 1]; + vecObj[--vecCount[vecRaw[i - 1]]] = vecRaw[i - 1]; } int main() @@ -57,6 +57,6 @@ int main() for (int i = 0; i < vecObj.size(); ++i) cout << vecObj[i] << " "; cout << endl; - + return 0; } \ No newline at end of file