First quicksort algorithm The program is separated in main.cpp, quicksort.cpp, partition.cpp and swap.cpp. There is also a header file to each .cpp file. The idea is from this site.

As you can see, I tried to be very modular. I think instead of having swap.cpp I could've simply used std::swap, right?

But the more important thing is, that I'd like to know how I can improve this quicksort algorithm or how to improve my program in general (not only the algorithm itself).

My three .hpp files are looking like this:

#ifndef __qs__name__
#define __qs__name__

#include &lt;vector&gt;

//protype

#endif 


partition.cpp:

#include "partition.hpp"
#include "swap.hpp"

int partition(std::vector&lt;int&gt;&amp; v, int ileft, int iright, int ipivot)
{
  int i = ileft, j = iright;

  while (i &lt;= j)
  {
    while (v[i] &lt; ipivot)
      i++;

    while (v[j] &gt; ipivot)
      j--;

    if (i &lt;= j)
    {
      swap(v, i, j); 
      i++; 
      j--;
    }
  } 

  return j;
}


quicksort.cpp:

#include "quicksort.hpp"
#include "partition.hpp"

void quicksort(std::vector&lt;int&gt;&amp; v, int ileft, int iright)
{
  int p, ipivot = v[iright]; // v[(ileft + iright) / 2];

  p = partition(v, ileft, iright, ipivot);

  if(ileft &lt; p) 
    quicksort(v, ileft, p);

  if((p + 1) &lt; iright) 
    quicksort(v, p + 1, iright);
}


swap.cpp:

#include "swap.hpp"

void swap(std::vector&lt;int&gt;&amp; v, int i, int j) 
{
  int temp = v[i];

  v[i] = v[j];
  v[j] = temp;
}


main.cpp:

#include &lt;vector&gt;

#include "partition.hpp"
#include "quicksort.hpp"
#include "swap.hpp"


int main()
{
  std::vector&lt;int&gt; v;
  v.push_back(7);
  v.push_back(10);
  v.push_back(11);
  v.push_back(1); 
  /*v.push_back(-4543);
  v.push_back(88);
  v.push_back(5);
  v.push_back(3);
  v.push_back(31);
  v.push_back(34);
  v.push_back(7);*/


  for (int i = 0; i &lt; v.size(); i++)
    std::cout &lt;&lt; v[i] &lt;&lt; std::endl;

  quicksort(v, 0, v.size()-1);
  std::cout &lt;&lt; "\n";

  for (int i = 0; i &lt; v.size(); i++)
    std::cout &lt;&lt; v[i] &lt;&lt; std::endl;

  return 0;
}
Any identifier with two underscores is reserved for the implementation. Using such an identifier yields undefined behaviour. Change your include guard. Something along QUICKSORT_GAMDSCHIEE_H is perfectly fine.



The user interface of quicksort should only take a std::vector. That way one cannot use size() by accident:

void quicksort(std::vector&lt;int&gt; &amp; v) {
    quicksort_impl(v, 0, v.size() - 1);
}


Make it hard to use your code in a wrong way.



You can return immediately in both partition and quicksort if ileft == iright and spare yourself some other if.




  I think instead of having swap.cpp I could've simply used std::swap, right?


Exactly, or at least re-use it:

void swap(std::vector&lt;int&gt; &amp; v, int i, int j) {
    std::swap(v[i], v[j]);
}




Instead of int we should use std::vector&lt;int&gt;::size_type (which will be size_t usually). 



There's no reason to limit your quicksort to std::vector&lt;int&gt; it works fine for any std::vector&lt;T&gt; as long as T provides operator&lt;.



To your "faster quicksort algorithm" question: you can find a std::vector where quicksort will take \$\mathcal O(n^2)\$ instead of \$\mathcal O(n \log n)\$. It's average time complexity is \$\mathcal O(n \log n)\$. Mergesort on the other hand is always \$\mathcal \Theta(n \log n)\$ time complexity.

Usually std::sort will be fast enough for all your use cases.