C++ STL : Insertion Sort (Sorting)

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


void insertionSort(std::vector<int>&a){

typedef std::vector<int>::iterator it;
for(auto i=1;i<a.size();i++){
it end_sorted =a.begin()+i;
    it insertion_point=std::upper_bound(a.begin(),end_sorted,a[i]);
    std::rotate(insertion_point,end_sorted,end_sorted+1);
}
}
void print(std::vector<int>a){
for(int i=0;i<a.size();i++){
std::cout<<a[i]<<" ";
}

}
int main(){
std::vector<int>arr={2,1,5,3,7,5,4,6};
insertionSort(arr);
print(arr);
}
Detailed Question and Suggestions
std::rotate
auto specifier

You may also like:
What is "namespace" and why do we use it?
C++: Implementation of Heapsort
C++: Merge_Sort using Vector
C++:Recursive Merge Sort
How do I learn data structures and algorithms from scratch?

Popular Posts

Top 10 Web Sites Every Programmer Should Visit Daily

How do I improve my logical thinking and problem solving skills?

C++: STL Iterators