Skip to content

Instantly share code, notes, and snippets.

@dhruvbird
Created April 11, 2012 13:24
Show Gist options
  • Save dhruvbird/2359271 to your computer and use it in GitHub Desktop.
Save dhruvbird/2359271 to your computer and use it in GitHub Desktop.
Comparing merge sort implementations for doubly linked lists
/* Compiling: Create a file named list_sort.c in the src/ folder and
* run:
*
* gcc list_sort.c lcthw/list.o lcthw/list_algos.o -I . -O2
*
* Time: 14.025s
*
*/
#include "lcthw/list.h"
#include "lcthw/list_algos.h"
int
int_cmp(void *lhs, void *rhs) {
return (int)lhs - (int)rhs;
}
int
main() {
List *l = List_create();
int i;
for (i = 0; i < 5000000; ++i) {
List_push(l, (void*)i);
}
List_merge_sort(l, int_cmp);
return 0;
}
/* Compiling: Create a file named list_sort.cpp in the src/ folder and
* run:
*
* g++ list_sort.cpp -o a.out.cpp -O2
*
* Time: 0.647s
*
*/
#include <list>
#include <iostream>
using namespace std;
int
main() {
list<int> *pl = new list<int>;
list<int> &l = *pl;
int i;
for (i = 0; i < 5000000; ++i) {
l.push_back(i);
}
l.sort();
// Add cout to prevent compiler from optimizing everything out.
cout<<"smallest element: "<<l.front()<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment