# Template qsort

• 04-06-2005
micha_mondeli
Template qsort
Hi guys i'm wondering if someone could help me correct what i'm doing wrong ...my qsort code won't compile..Thanks

Code:

```template <typename T> void qsort(T *a, int l, int r) { // sort a[l..r]         if (l < r) {                 int i=l-1, j=r;                 const T &v = a[r];                 for (;;) { // partition: < v | >= v                         while (a[++i] < v);                         while (v < a[--j]) if (j <= l) break; // (*)                         if (i >= j) break;                         swap(a[i], a[j]); // exchange misplaced elems.                 }                 swap(a[i], a[r]);                 qsort(a, l, i-1); // recursively sort part 1                 qsort(a, i+1, r); // recursively sort part 2         } } template <typename T> void swap(T &a, T &b){ T temp = a; a = b; b = temp; } int main(){ int ia[6] = { 1, 3, 5, 2, 8, 0 }; double da[6] = { 1.5, 0.5, 3.4, 5.2 }; qsort(ia, 2, 4); // T=int, sort elements ia[2..4] qsort(da, 0, 5); // T=double, sort elements da[0..5] }```
THe code compiles but does not build
errors:

qsorttemplate.obj : error LNK2005: _main already defined in Qsort.obj
Debug/Qsort.exe : fatal error LNK1169: one or more multiply defined symbols found
• 04-06-2005
sean
Post the errors

edit: and post the full code. Are those lines of code at the bottom inside your main function, or what?
• 04-06-2005
samGwilliam
Sorry to hijack the thread, but I still don't understand the qsort algorithm. Anyone care to explain it to me?
• 04-06-2005
sean
• 04-06-2005
Prelude
It compiles and links cleanly for me. What compiler are you using?
• 04-06-2005
micha_mondeli
Visual c++ 6.0
• 04-06-2005
micha_mondeli
Actually hold up its working now! Strange..Thanks
• 04-06-2005
sean
Works fine under Dev-C++ 4.9.9.1