請你能幫我從一本書中理解下面的代碼嗎?
我想知道為什么“ swap(words, start, current); ”不是下面代碼中for回圈的一部分?
“for 回圈 - 根據所選單詞檢查單詞”的最終效果應該是將所有小于所選單詞的單詞放在所有大于或等于它的單詞之前。
但是,如果在每次 I 迭代后不交換“開始”和“當前”,我不明白比較是如何完成的,因為 IF 陳述句中的“*words[i]”將始終與“*words[start”進行比較]" 始終等于 index = 0 (條件在回圈內迭代,這意味著總是針對 0 索引進行比較)
// 參考 "*words[i] < *words[start]")
Ps 我最初的假設是交換行“swap(words, start, current);” 應該是 for 回圈的一部分,下面你可以看到它不是回圈的一部分,而是在 for 回圈之外。
void sort(Words& words, size_t start, size_t end)
{
// start index must be less than end index for 2 or more elements
if (!(start < end))
return;
// Choose middle address to partition set
swap(words, start, (start end) / 2);
// Check words against chosen word
size_t current {start};
for (size_t i {start 1}; i <= end; i )
{
if (*words[i] < *words[start])
swap(words, current, i);
}
swap(words, start, current);
if (current > start) sort(words, start, current - 1);
if (end > current 1) sort(words, current 1, end);
}
下面還添加了為交換函式定義的代碼(以防您認為它相關)
#include <iostream>
#include <iomanip>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
using Words = std::vector<std::shared_ptr<std::string>>;
void swap(Words& words, size_t first, size_t second);
void sort(Words& words);
void sort(Words& words, size_t start, size_t end);
void extract_words(Words& words, std::string_view text, std::string_view separators); void show_words(const Words& words);
size_t max_word_length(const Words& words);
int main()
{
Words words;
std::string text;
const auto separators{" ,.!?\"\n"};
std::cout << "Enter a string terminated by *:" << std::endl; getline(std::cin, text, '*');
extract_words(words, text, separators);
if (words.empty())
{
std::cout << "No words in text." << std::endl;
return 0;
}
sort(words);
show_words(words);
}
void extract_words(Words& words, std::string_view text, std::string_view separators)
{
size_t start {text.find_first_not_of(separators)};
size_t end {};
while (start != std::string_view::npos)
{
end = text.find_first_of(separators, start 1);
if (end == std::string_view::npos)
end = text.length();
words.push_back(std::make_shared<std::string>(text.substr(start, end - start)));
}
}
void swap(Words& words, size_t first, size_t second)
{
auto temp{words[first]};
words[first] = words[second];
words[second] = temp;
}
This just swaps the addresses in words at indexes first and second.
uj5u.com熱心網友回復:
發生的事情是陣列的中間值被選為樞軸值并將“移開”到陣列的開頭:
swap(words, start, (start end) / 2);
然后回圈處理從start 1到end包含的所有值,以便一旦完成,從start到current包含的所有值都小于主元值。注意回圈來自start 1所以我們永遠不會改變樞軸值。
size_t current {start};
for (size_t i {start 1}; i <= end; i )
{
if (*words[i] < *words[start])
swap(words, current, i);
}
但是,此時樞軸位于錯誤的位置。要使遞回呼叫起作用,at 的值words[current]必須包含樞軸值。所以它需要從我們放置它的地方交換 ( words[start]) 到current
swap(words, start, current);
考慮一下如果對一個簡單的小陣列進行排序會發生什么可能會很有用
[3,2,1]
您選擇中間值并將其移動到開始...
[2,3,1]
您移動所有小于樞軸的值...
[2,1,3]
^
|
current
您將樞軸移回原位
[1,2,3]
您對當前 [1] 之前和當前 [3] 之后的部分進行排序,因為它們是單個元素,因此不涉及任何更改。
請注意,如果您沒有將主元移動到正確的位置,則對子陣列進行排序將不會產生正確的答案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/330394.html
標籤:C
下一篇:用前處理器替換C 類/靜態方法?
