logo

C qsort () vs. c ++ lajittelu ()

Standard C library provides qsort function that can be used for sorting an array. Following is the prototype of qsort() function.
// Sort an array of any type. The parameters are base // address of array size of array and pointer to // comparator function void qsort (void* base size_t num size_t size int (*comparator)(const void* const void*));
It requires a pointer to the array the number of elements in the array the size of each element and a comparator function. We have discussed qsort comparator in detail tässä . C ++ -standardikirjasto tarjoaa samanlaisen funktion lajittelun (), joka on peräisin STL: stä. Olemme keskustelleet C ++ -lajista tässä . Following are prototypes of C++ sort() function.
// To sort in default or ascending order. template void sort(T first T last); // To sort according to the order specified // by comp. template void sort(T first T last Compare comp); 
The order of equal elements is not guaranteed to be preserved. C++ provides std::stable_sort that can be used to preserve order. Vertailu Qsortiin ja lajitteluun () 1. toteutustiedot: Kuten nimestä ehdottaa, QSORT -toiminto käyttää Quicksort -algoritmia annetun taulukon lajittelemiseen, vaikka C -standardi ei vaadi sitä pikaSortin toteuttamiseen. C ++ -lajin toiminto käyttää introsorttia, joka on hybridi -algoritmi. Eri toteutukset käyttävät erilaisia ​​algoritmeja. Esimerkiksi GNU-standardi C ++ -kirjasto käyttää 3-osaista hybridi-lajittelualgoritmia: introsort suoritetaan ensin (Introsort itse on Quicksortin ja kasan lajittelun hybridi), jota seuraa tuloksen lisäys. 2. monimutkaisuus: C -standardi ei puhu sen monimutkaisuudesta QSORT. Uusi C ++ 11 -standardi edellyttää, että lajittelu on O (NLOG (N)) pahimmassa tapauksessa. Aikaisemmat C ++: n versiot, kuten C ++ 03, sallivat mahdollisen pahimman tapauksen O (n^2). Ainoastaan ​​keskimääräinen monimutkaisuus vaadittiin O (n log n). 3. Käyntiaika: STL’s sort ran faster than C’s qsort because C++’s templates generate optimized code for a particular data type and a particular comparison function. STL’s sort runs 20% to 50% faster than the hand-coded quicksort and 250% to 1000% faster than the C qsort library function. C might be the fastest language but qsort is very slow. When we tried to sort one million integers on C++14 Time taken by C qsort() was 0.247883 sec and time taken by C++ sort() was only 0.086125 sec CPP
// C++ program to demonstrate performance of // C qsort and C++ sort() algorithm #include    using namespace std; // Number of elements to be sorted #define N 1000000 // A comparator function used by qsort int compare(const void * a const void * b) {  return ( *(int*)a - *(int*)b ); } // Driver program to test above functions int main() {  int arr[N] dupArr[N];  // seed for random input  srand(time(NULL));  // to measure time taken by qsort and sort  clock_t begin end;  double time_spent;  // generate random input  for (int i = 0; i < N; i++)  dupArr[i] = arr[i] = rand()%100000;  begin = clock();  qsort(arr N sizeof(int) compare);  end = clock();  // calculate time taken by C qsort function  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;  cout << 'Time taken by C qsort() - '  << time_spent << endl;  time_spent = 0.0;  begin = clock();  sort(dupArr dupArr + N);  end = clock();  // calculate time taken by C++ sort  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;  cout << 'Time taken by C++ sort() - '  << time_spent << endl;  return 0; } 
Output :
Time taken by C qsort() - 0.247883 Time taken by C++ sort() - 0.086125 
C++ sort() is blazingly faster than qsort() on equivalent data due to inlining. sort() on a container of integers will be compiled to use std::less::operator() by default which will be inlined and sort() will be comparing the integers directly. On the other hand qsort() will be making an indirect call through a function pointer for every comparison which compilers fails to optimize. 4. Joustavuus: STL: n lajittelu toimii kaikille tietotyypeille ja erilaisille tietosäiliöille, kuten C -taulukoille C ++ -vektorit C ++ Deques jne. Ja muilla säilöillä, jotka käyttäjä voi kirjoittaa. Tällaista joustavuutta on melko vaikea saavuttaa C. 5. Turvallisuus: QSORT: ään verrattuna mallitettu lajittelu on tyyppisempi, koska se ei vaadi tietokohteiden pääsyä vaarallisten tyhjiöosoittimien kautta, kuten QSORT tekee. Viitteet: http://theory.stanford.edu/~amitp/rants/c++-vs-c/ https://en.wikipedia.org/wiki/sort_(c%2B%2B)