简体   繁体   English

使用迭代器和向量的插入排序实现

[英]Insertion sort implementation using iterators and vectors

I'm trying to implement insertion sort algorithm using iterators and it doesn't seem to work as I thought... Do you have any ideas of how to implement it?我正在尝试使用迭代器实现插入排序算法,但它似乎并没有像我想象的那样工作......你对如何实现它有任何想法吗?

Also, I can't use code like this one: https://www.geeksforgeeks.org/insertion-sort-using-c-stl/ because I'm intending to make an animation and it will get more complicated.另外,我不能使用这样的代码: https://www.geeksforgeeks.org/insertion-sort-using-c-stl/因为我打算制作一个 animation,它会变得更加复杂。

This is my source code so far:到目前为止,这是我的源代码:

#include <iostream>
#include <vector>
#include <algorithm>



int main()
{
    
    std::vector<int> seq = { 5, 4, 3, 2, 1 };
    
    
    std::vector<int>::iterator itj;
    std::vector<int>::iterator leftmost;
    
    // insertion sort
    for (std::vector<int>::iterator iti = seq.begin() + 1; iti != seq.end(); iti = std::next(iti))
    {
        itj = std::prev(iti);
        leftmost = iti;
        
        while (std::distance(seq.begin(), itj) >= 0 && *itj > *leftmost)
        {   
            std::next(itj) = itj;
            itj = prev(itj);
        }
        std::next(itj) = leftmost;
    }
    
    // printing 
    for (std::vector<int>::iterator iti = seq.begin(); iti != seq.end(); iti = std::next(iti))
    {
        std::cout << *iti << " ";
    }
    

}

Here's a very elegant implementation of insertion sort using iterators lifted directly from the reference page on rotate :这是一个非常优雅的插入排序实现,它使用直接从rotate上的参考页面提升的迭代器:

// insertion sort
for (auto i = v.begin(); i != v.end(); ++i) {
    std::rotate(std::upper_bound(v.begin(), i, *i), i, i+1);
}

All you have to do is understand how std::rotate works, and this becomes easy to understand.您所要做的就是了解std::rotate是如何工作的,这很容易理解。 ( rotate is anyway a really powerful algorithm that you should be comfortable with). (无论如何, rotate是一个非常强大的算法,你应该感到舒服)。

Here is animplementation taken from SGI STL 1 :这是取自 SGI STL 1实现

template<class Random_it, class T>
void unguarded_linear_insert(Random_it last, T val) {
    auto next = last;
    --next;
    while (val < *next) {
        *last = *next;
        last = next;
        --next;
    }
    *last = val;
}

template<class Random_it>
void linear_insert(Random_it first, Random_it last) {
    auto val = *last;
    if (val < *first) {
        std::copy_backward(first, last, last + 1);
        *first = val;
    }
    else
        unguarded_linear_insert(last, val);
}

template<class Random_it>
void insertion_sort(Random_it first, Random_it last) {
    if (first == last)
        return;
    for (auto i = first + 1; i != last; ++i)
        linear_insert(first, i);
}

Note how the val < *first condition and std::copy_backward are used to simplify the loop inside unguarded_linear_insert : only one condition, namely val < *next can be checked in that loop.请注意如何使用val < *first条件和std::copy_backward来简化unguarded_linear_insert内的循环:在该循环中只能检查一个条件,即val < *next

1 The same implementation can be found in libstdc++. 1在 libstdc++ 中可以找到相同的实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM