Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



Practical C Programming, 3rd Edition    (ISBN: 1565923065)


 

 List Price: $34.95
 Our Price: $24.47
 Used Price: $7.73

 Release Date: August, 1997
 Manufacturer: O'Reilly & Associates (Paperback)
 Sales Rank: 53,221

 Author: Steve Oualline









More Info

 Bi-directional Sweep Sort 2004-06-07 21:05:13
 
Category: source:c:general
Description: Based on the sort algorithm in bubblesort.c which is as simple as it gets in the sorting business. Added basic improvements to make a bi-directional sweep sort. Added check to short-circuit sort operation as soon as the program detects that the array is sorted. Limit sweeping of values to the unsorted middle of the array. Also added some debugging messages so one can follow the progress of a sort.
Platform: all
Author: mwberryman
Viewed: 4223
Rating: 4/5 (8 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
bisweepsort.c
/* bisweepsort.c by mwberryman on metalshell.com
 *
 * Based on the sort algorithm in bubblesort.c which is
 * as simple as it gets in the sorting business.
 *
 * Added basic improvements to make a bi-directional sweep
 * sort.  Added check to short-circuit sort operation as soon
 * as the program detects that the array is sorted.  Limit
 * sweeping of values to the unsorted middle of the array.
 *
 * Also added some debugging messages so one can follow the
 * progress of a sort.
 *
 * http://www.metalshell.com/
 *
 */

#include <stdlib.h>
#include <stdio.h>

#define ARRAY_SIZE 20

void print_array(int *array) {
  int x;
  for(x = 0; x < ARRAY_SIZE; x++) {
    if(x != ARRAY_SIZE-1)
      fprintf(stdout, "%d, ", array[x]);
    else
      fprintf(stdout, "%d\n", array[x]);
  }
}

int main() {
  int iarray[ARRAY_SIZE];
  int x, y, holder;

  // Seed rand()

  srand((unsigned int)time(NULL));

  for(x = 0; x < ARRAY_SIZE; x++)
    iarray[x] = (int)(rand() % 100);

  fprintf(stdout, "Before Sort\n---------------\n");
  print_array(iarray);
  printf("\n");


  // Bubble sort method with bi-directional sweeping.

  //

  // Check after each sweep if any more sweeps need

  // to be done.  If any sweep results in no movement

  // of values then the array is sorted so short_circuit

  // the sort operation.

  //

  // After each sweep reduce the upper or lower limit

  // of array which must be swept.



  int ul=ARRAY_SIZE;
  int ll=0;
  int sorted=1;
  for(x = ll; x < ARRAY_SIZE; x++) {

    /* Sweep largest value to end */

    printf("x=%d\n",x);
    sorted=1;
    for(y = ll; y < ul-1; y++) {
      printf("y=%d ",y);
      if(iarray[y] > iarray[y+1]) {
        holder = iarray[y+1];
        iarray[y+1] = iarray[y];
        iarray[y] = holder;
        sorted=0;
      }
    }
    printf("\n");
    print_array(iarray);

    /* short circuit the process if array is now sorted */
    if (sorted) { printf("Found sorted A\n"); break; }

    /* Reduce upper limit on array since the larger values have
       been swept to the end. */
    ul--;

   /* Bubble smallest value to beginning */

    sorted=1;
    for(y = ul-1; y > ll; y--) {
      printf("y=%d ",y);
      if(iarray[y] < iarray[y-1]) {
        holder = iarray[y-1];
        iarray[y-1] = iarray[y];
        iarray[y] = holder;
        sorted=0;
      }
    }
    printf("\n");
    print_array(iarray);

    /* short circuit the process if array is now sorted */
    if (sorted) { printf("Found sorted B\n"); break; }

    /* Increase lower limit on array since the smaller values have
       been swept to the beginning. */
    ll++;
  }

  fprintf(stdout, "\nAfter Sort\n---------------\n");
  print_array(iarray);

}
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?