pure code
1.0.0
這是一個項目,旨在是通用數據結構和算法的教科書實施。它提供演示實現和單元測試。
該代碼旨在乾淨和簡單,效率不是主要目標。我將根據計算複雜性來撰寫最有效的實現或提供不同版本的實現。
代碼示例:
template < class Iter >
void linearInsert (Iter first, Iter last)
{
auto insertionPoint = std::upper_bound (first, last, *last);
std::rotate (insertionPoint, last, last + 1 );
}
template < class Iter >
void insertionSort (Iter first, Iter last)
{
// [begin, p) sorted
// [p, end) to be processed
for ( auto p = first; p != last; ++p) {
linearInsert (first, p);
}
}如果您想練習編碼技能,只需下載項目並重寫相關算法並自己運行單元測試即可。
當您實施需要其他算法或數據結構的算法時,您可以簡單地使用標準庫中的現有實現或倉庫中的現有實現,並專注於主要思想,例如:
template < class Iter >
void quickSort (Iter first, Iter last)
{
if (last - first <= 1 ) {
return ;
}
auto partitionPoint = partition (first, last);
quickSort (first, partitionPoint);
quickSort (partitionPoint, last);
}在上面的示例中,如果輸入範圍的長度大於一個,則可以保證partition (在存儲庫中實現)返回兩個非空範圍,以確保我們確保以下兩個遞歸調用將在較小的維度上發生。
您歡迎您通過以下任意