Difference between r1.2 and the current
@@ -1,6 +1,6 @@
* http://isis.poly.edu/kulesh/stuff/src/klist/
{{{
#include <stdlib.h>
#include <time.h>
{{{
#include <unistd.h>
// #include <unistd.h>
#include <stdio.h> #include <stdlib.h>
#include <time.h>
@@ -19,17 +19,66 @@
int *c;
a = (int *)malloc(sizeof(int)*MAX);
for (i = 0; i < MAX; i++)
{
}
}}}
a = (int *)malloc(sizeof(int)*MAX);
clock_t stime, etime;
// clock_t stime, etime;
for (i = 0; i < MAX; i++)
{
a[i] = random()%MAX;
a[i] = rand()%MAX;
} stime = clock();
// stime = clock();
qsort( a, MAX, sizeof(int), fn_qsort_intcmp ); etime = clock();
printf("Time : %.3fs\n",(double)(etime - stime)/CLOCKS_PER_SEC);
// etime = clock();
// printf("Time : %.3fs\n",(double)(etime - stime)/CLOCKS_PER_SEC);
return 0; }
}}}
== qsort by template ==
{{{
// list::sort
#include <iostream>
#include <list>
#include <string>
#include <cctype>
using namespace std;
// comparison, not case sensitive.
bool compare_nocase (string first, string second)
{
unsigned int i=0;
while ( (i<first.length()) && (i<second.length()) )
{
if (tolower(first[i])<tolower(second[i])) return true;
else if (tolower(first[i])>tolower(second[i])) return false;
++i;
}
if (first.length()<second.length()) return true;
else return false;
}
int main ()
{
list<string> mylist;
list<string>::iterator it;
mylist.push_back ("one");
mylist.push_back ("two");
mylist.push_back ("Three");
mylist.sort();
cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
cout << " " << *it;
cout << endl;
mylist.sort(compare_nocase);
cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
}}}
// #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #define MAX 1000000 int fn_qsort_intcmp( const void *a, const void *b ) { return( *(int *)a - *(int *)b); } int main() { int *a; int i; int *c; a = (int *)malloc(sizeof(int)*MAX); // clock_t stime, etime; for (i = 0; i < MAX; i++) { a[i] = rand()%MAX; } // stime = clock(); qsort( a, MAX, sizeof(int), fn_qsort_intcmp ); // etime = clock(); // printf("Time : %.3fs\n",(double)(etime - stime)/CLOCKS_PER_SEC); return 0; }
qsort by template ¶
// list::sort #include <iostream> #include <list> #include <string> #include <cctype> using namespace std; // comparison, not case sensitive. bool compare_nocase (string first, string second) { unsigned int i=0; while ( (i<first.length()) && (i<second.length()) ) { if (tolower(first[i])<tolower(second[i])) return true; else if (tolower(first[i])>tolower(second[i])) return false; ++i; } if (first.length()<second.length()) return true; else return false; } int main () { list<string> mylist; list<string>::iterator it; mylist.push_back ("one"); mylist.push_back ("two"); mylist.push_back ("Three"); mylist.sort(); cout << "mylist contains:"; for (it=mylist.begin(); it!=mylist.end(); ++it) cout << " " << *it; cout << endl; mylist.sort(compare_nocase); cout << "mylist contains:"; for (it=mylist.begin(); it!=mylist.end(); ++it) cout << " " << *it; cout << endl; return 0; }