Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 14th, 2006, 2:54 AM   #11
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
bl00dninja, maybe you missed it, but there's supposed to be some paperwork involved in this thread... :p
Jimbo is offline   Reply With Quote
Old Sep 15th, 2006, 3:01 AM   #12
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Old Sep 15th, 2006, 3:11 AM   #13
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
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...
Jimbo is offline   Reply With Quote
Old Sep 15th, 2006, 8:18 AM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Sep 15th, 2006, 8:52 AM   #15
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 Narue is on a distinguished road
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;
    }
  }
}
Insertion sort is faster than bubble sort on arrays due to optimized data movement:
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;
  }
}
But the real benefit doesn't show up until you sort a data structure that doesn't have such a high shifting cost, like a linked list:
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;
}
Shell sort is faster than bubble sort because it uses a divide and conquer approach that has been proven to be efficient many times over the decades:
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;
  }
}
Here's another quicksort with optimizations that most people generally ignore:
/* 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 );
}
Here's introsort, which is faster than bubble sort when quicksort is and doesn't have quicksort's problems:
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 );
}
A natural merge sort is superior to a regular merge sort because it takes advantage of existing order in the sequence. However, natural merge sort on arrays sucks because you spend so much time maintaining the queues. A linked list is much better:
/* 
  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];
}
LSD radix sort is super fast because it relies on a bucket dropping technique. This can easily get into O(N) speeds, which is vastly superior to bubble sort in the average case. Keep in mind that bubble sort is very fast when the sequence is already sorted:
#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 );
}
MSD radix sort is better than LSD radix sort because it sorts the most significant parts first, thus reaching a sorted state much faster:
/* 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 );
}
Do I win the thread? 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.
Narue is offline   Reply With Quote
Old Sep 15th, 2006, 9:20 AM   #16
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Sep 15th, 2006, 12:47 PM   #17
RobEasy
Programmer
 
RobEasy's Avatar
 
Join Date: May 2006
Location: The US duhhhhh!
Posts: 42
Rep Power: 0 RobEasy is on a distinguished road
One question though, what the heck would we use this for?
__________________
Work Hard... Play Harder!
RobEasy is offline   Reply With Quote
Old Sep 15th, 2006, 1:26 PM   #18
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
Quote:
Originally Posted by DaWei View Post
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?
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
Jimbo is offline   Reply With Quote
Old Sep 15th, 2006, 3:14 PM   #19
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 Narue is on a distinguished road
>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.
Narue is offline   Reply With Quote
Old Sep 15th, 2006, 6:58 PM   #20
peace_of_mind
Professional Programmer
 
peace_of_mind's Avatar
 
Join Date: Sep 2004
Location: Hell if I know most of the time
Posts: 439
Rep Power: 4 peace_of_mind is on a distinguished road
Send a message via MSN to peace_of_mind Send a message via Yahoo to peace_of_mind
Congrats and thank you good folks. This is the first PFO thread I've bookmarked.
__________________
Amateurs built the ark
Professionals built the Titanic

peace_of_mind is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:46 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC