![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 751
Rep Power: 3
![]() |
bl00dninja, maybe you missed it, but there's supposed to be some paperwork involved in this thread... :p
|
|
|
|
|
|
#12 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
yeah, i declined to do all that...just lost interest and moved on.
:p
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#13 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 751
Rep Power: 3
![]() |
you lazy bum... :p
If I get some free time sometime soon (like, 3 days worth of free time), I might try to get my head around smoothsort... all I've ever seen for it was Dijkstra's paper about how it works, which had some Pascal samples I think. I didn't read the document much, just scanned it really quickly to see how long it was... but I want to go back and implement it, even if only for the bragging rights... |
|
|
|
|
|
#14 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Jim, I've probably seen that same reference, but didn't delve into it, either. What do you think about opening it up in the Algorithms forum where it might be taken seriously by the few percent of the members who are looking for technical enlightenment?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#15 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 3
![]() |
Selection sort is faster than bubble sort when the cost of data movement exceeds the cost of comparisons:
void jsw_selection ( int a[], int n )
{
while ( --n > 0 ) {
int i, max = n;
for ( i = 0; i < n; i++ ) {
if ( a[i] >= a[max] )
max = i;
}
if ( max != n ) {
int save = a[max];
for ( i = max; i < n; i++ )
a[i] = a[i + 1];
a[n] = save;
}
}
}void jsw_insertion ( int a[], int n )
{
int i;
for ( i = 1; i < n; i++ ) {
int j, save = a[i];
for ( j = i; j >= 1 && a[j - 1] > save; j-- )
a[j] = a[j - 1];
a[j] = save;
}
}void jsw_insertion ( struct jsw_list *list )
{
struct jsw_node result = {0};
struct jsw_node *i, *step, *next;
for ( step = list->head; step != NULL; step = next ) {
next = step->next;
for ( i = &result; i->next != NULL; i = i->next ) {
if ( i->next->data > step->data )
break;
}
step->next = i->next;
i->next = step;
}
list->head = result.next;
}void jsw_shellsort ( int a[], int n )
{
int i, h = 1;
while ( h <= n 9 )
h = 3 * h + 1;
while ( h > 0 ) {
for ( i = h; i < n; i++ ) {
int j, save = a[i];
for ( j = i; j >= h && a[j - h] > save; j -= h )
a[j] = a[j - h];
a[j] = save;
}
h = 3;
}
}/* Median of three, obviously */
int jsw_partition ( int a[], int first, int last )
{
int pivot, mid = ( first + last ) / 2;
/* Largest of two */
pivot = a[first] > a[mid] ? first : mid;
/* Smallest of remaining */
if ( a[pivot] > a[last] )
pivot = last;
jsw_swap ( &a[pivot], &a[first] );
pivot = first;
while ( first < last ) {
if ( a[first] <= a[last] )
jsw_swap ( &a[pivot++], &a[first] );
++first;
}
jsw_swap ( &a[pivot], &a[last] );
return pivot;
}
void jsw_quicksort_r ( int a[], int first, int last )
{
/* Use another method on almost-sorted array */
if ( last - first > THRESHOLD ) {
int pivot;
/* Avoid work if the subarray is sorted */
if ( is_sorted ( a, first, last ) )
return;
pivot = jsw_partition ( a, first, last );
jsw_quicksort_r ( a, first, pivot - 1 );
jsw_quicksort_r ( a, pivot + 1, last );
}
}
void jsw_quicksort ( int a[], int n )
{
jsw_quicksort_r ( a, 0, n - 1 );
/* Finalize the sort */
jsw_insertion ( a, n );
}void jsw_introsort_r ( int a[], int first, int last, int depth )
{
while ( last - first > THRESHOLD ) {
if ( depth == 0 )
jsw_heapsort ( &a[first], last - first + 1 );
else {
int pivot;
if ( is_sorted ( a, first, last ) )
return;
pivot = jsw_partition ( a, first, last );
jsw_introsort_r ( a, pivot + 1, last, depth - 1 );
last = pivot - 1;
}
}
}
void jsw_introsort ( int a[], int n )
{
jsw_introsort_r ( a, 0, n - 1, (int)( 2 * log ( n ) ) );
jsw_insertion ( a, n );
}/*
Remove a node from a list and
push it onto a tail pointer
*/
#define split_push(head,tail,item) do { \
struct jsw_node *save = item->next; \
item->next = NULL; \
if ( tail == NULL ) \
head = tail = item; \
else { \
tail->next = item; \
tail = tail->next; \
} \
item = save; \
}while ( 0 );
struct jsw_node *jsw_merge ( struct jsw_node *a,
struct jsw_node *q[2] )
{
int dir = q[0]->data > q[1]->data;
struct jsw_node *save = a = q[dir];
/* Intended assignment */
while ( q[dir] = q[dir]->next ) {
dir = q[0]->data > q[1]->data;
if ( q[dir]->data < a->data
&& a->data < q[!dir]->data )
{
dir = !dir;
}
a->next = q[dir];
a = a->next;
}
dir = q[0] == NULL;
while ( q[dir] != NULL ) {
a->next = q[dir];
q[dir] = q[dir]->next;
a = a->next;
}
return save;
}
void jsw_mergesort ( struct jsw_list *list )
{
struct jsw_node *q[2] = {0};
struct jsw_node *a = list->head;
for ( ; ; ) {
struct jsw_node *end[2] = {0};
int half = 0;
split_push ( q[0], end[0], a );
while ( a != NULL ) {
if ( a->data < end[half]->data )
half = !half;
split_push ( q[half], end[half], a );
}
if ( q[0] == NULL || q[1] == NULL )
break;
a = jsw_merge ( a, q );
}
list->head = q[q[0] == NULL];
}#define RANGE ( ( 1U << CHAR_BIT ) + 1 )
#define digit(x,i) ( (x) >> ( (i) * CHAR_BIT ) & UCHAR_MAX )
void jsw_radix_pass ( int a[], int aux[], int n, int radix )
{
int i;
int count[RANGE] = {0};
for ( i = 0; i < n; i++ )
++count[digit ( a[i], radix ) + 1];
for ( i = 1; i < RANGE; i++ )
count[i] += count[i - 1];
for ( i = 0; i < n; i++ )
aux[count[digit ( a[i], radix )]++] = a[i];
for ( i = 0; i < n; i++ )
a[i] = aux[i];
}
void jsw_radixsort ( int a[], int n )
{
int i;
int *aux = malloc ( n * sizeof *aux );
for ( i = 0; i < sizeof *a; i++ )
jsw_radix_pass ( a, aux, n, i );
free ( aux );
}/* Extract ith MSD byte */
#define digit(x,i) ( (x)[i] )
#define bin(x) ( first + count[x] )
void jsw_radix_pass ( char *a[], char *aux[],
int count[], int first, int last, int radix )
{
int i;
for ( i = first; i < last; i++ )
++count[digit ( a[i], radix ) + 1];
for ( i = 1; i < UCHAR_MAX; i++ )
count[i] += count[i - 1];
for ( i = first; i < last; i++ )
aux[count[digit ( a[i], radix )]++] = a[i];
for ( i = first; i < last; i++ )
a[i] = aux[i - first];
}
void jsw_radixsort_r ( char *a[], char *aux[],
int first, int last, int radix )
{
if ( radix <= UCHAR_MAX && last - first > THRESHOLD ) {
int count[UCHAR_MAX + 1] = {0};
int i;
jsw_radix_pass ( a, aux, count, first, last, radix );
for ( i = 0; i < UCHAR_MAX - 1; i++ ) {
int left = bin ( i );
int right = bin ( i + 1 ) - 1;
jsw_radixsort_r ( a, aux, left, right, radix + 1 );
}
}
}
void jsw_radixsort ( char *a[], int n )
{
char **aux = malloc ( ( n + 1 ) * sizeof *aux );
jsw_radixsort_r ( a, aux, 0, n, 0 );
jsw_insertion ( a, n );
free ( aux );
} I can come up with more. I know far more than 25 algorithms for sorting. ![]()
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#16 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You've departed from the enlightened path of inanity!
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#17 |
|
Programmer
Join Date: May 2006
Location: The US duhhhhh!
Posts: 42
Rep Power: 0
![]() |
One question though, what the heck would we use this for?
__________________
Work Hard... Play Harder! |
|
|
|
|
|
#18 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 751
Rep Power: 3
![]() |
That's a good idea. If I have any problems with it, expect a post there... and if I get it figured out, I'll see what I can do to summarize it. Now, just gotta get the current project to meet the deadline and hopefully classes starting up in a week won't keep me too busy :o
|
|
|
|
|
|
#19 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 3
![]() |
>One question though, what the heck would we use this for?
For learning, of course. Studying sorting algorithms can give you a huge number of potential tools in algorithm design because they're basically the most researched topic in computer science, and the tricks used in them can be used elsewhere as well. As an example, take the divide and conquer trick from quicksort and see how many obvious places it can be applied. Just because you don't use an algorithm doesn't mean you didn't learn anything from it.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#20 |
|
Professional Programmer
|
Congrats and thank you good folks. This is the first PFO thread I've bookmarked.
__________________
Amateurs built the ark Professionals built the Titanic |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sort and swap | brad sue | C | 1 | Mar 25th, 2006 6:33 AM |
| interpolation search on a million | EverLearning | C | 24 | Jun 15th, 2005 2:42 PM |
| threaded merge sort help | AusTex | C | 1 | May 1st, 2005 4:58 PM |