Skip to content

STL Algorithms

How stl algorithm work?

  • we either have to pass the iterators to them, or start and end pointers, and sometimes the container to the algorithm, which is a template function.

Some basic algorithms

Writing algorithms

  • Writing generic algorithms:
template <class T, class iter>s
void bubble_sort(iter begin, iter end, bool (&compare)(T, T)) {
  while(begin != end){
    end--;
    bool flag = true;
    for(iter j=begin; j<end;j++){
      if (!compare(*j, *(j + 1))) {
        flag = false;
        T temp = *j;
        *j = *(j + 1);
        *(j + 1) = temp;
      }
    }
    if(flag){
      return;
    }
  }
}
  • using functors, overloading
template <class iterator, class T, class comp>
iterator search(iterator begin, iterator end, T key, comp cmp) {
  while (begin != end) {
    if (cmp(*begin, key)) {
      return begin;
    }
    begin++;
  }
  return end;
}
  • a generic sort
template <class X> void sort(X a[],int n){
    X t;
    for(int i=0;i<n;i++){
        for(int j=0;j<n-i-1;j++){
            if(a[j]>a[j+1]){
                //swap
                X temp;
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        }
    }
}