Hi, I was thinking of making a library of different types of sorts(as I had discovered that I could make my own libraries),but I ran into a problem with the
bubble sort. If I write bubble sort directly in the code, it runs in about 1.59secs
but if it is written as a function it runs in 1.98secs. Could someone help me keep the runtime down? (How does the qsort() keep runtime down?)
This is my code:
Thank you.Code:#include<algorithm> #include<cstdio> #include<iostream> #ifndef SORT_H #define SORT_H void bsort(int L,int *ptr) { bool changed=0; int D_C=L-1; for(int x=0; x<L; x++) { for(int y=0; y<D_C; y++) { if(ptr[y]>ptr[y+1]) { int temp = ptr[y+1]; ptr[y+1] = ptr[y]; ptr[y] = temp; changed=1; } } if(!changed) break; D_C--; } } #endif



LinkBack URL
About LinkBacks



and it doesn't check already sorted elements (D_C--). I'm more concerned with why converting the bubble sort into a function makes the runtime so much longer. If it were impremented directly on an array (of 10000elements produced by rand()), it works 1.25 times faster. Why does it go so slow? Can I change this? (Yes I want to use bubble sort. I know other sorts but some problems are easier using bubble sort).