Permalink
Browse files

move shared functionality into its own file

  • Loading branch information...
1 parent bf9845f commit 99058f7df3cd79868c4080b75892703fd17aa2c0 @JoshCheek JoshCheek committed Sep 12, 2010
Showing with 45 additions and 85 deletions.
  1. +1 −40 sort-efficiency/insertion.c
  2. +5 −45 sort-efficiency/merge.c
  3. +39 −0 sort-efficiency/shared.c
@@ -1,30 +1,4 @@
-#include <stdio.h>
-#include <stdarg.h>
-
-#define bool int
-#define true 1
-#define false 0
-
-void print_ary( int ary[] , int size ) {
- int i;
- printf( "[ " );
- for( i=0; i<size ; ++i )
- printf( "%d " , ary[i] );
- printf( "]\n");
-}
-
-bool is_sorted( int ary[] , int size ) {
- while(--size)
- if( ary[size] < ary[size-1] )
- return false;
- return true;
-}
-
-void populate( int ary[] , int size ) {
- int i;
- for( i=0 ; i<size ; ++i ) ary[i]=rand();
-}
-
+#include "shared.c"
void sort( int ary[] , int size ) {
int wall; // wall separates sorted left from unsorted right
@@ -42,16 +16,3 @@ void sort( int ary[] , int size ) {
}
-// pass in array size, and number of times to test
-int main( int argc , char const *argv[] ) {
- int i=0 , size=atoi(argv[1]) , ary[size] , times_to_test=atoi(argv[2]);
- while( i++ < times_to_test ) {
- populate(ary,size);
- sort(ary,size);
- // if( !is_sorted(ary,size) ) {
- // printf( "failure: not sorted\n" );
- // print_ary(ary,size);
- // }
- }
- return 0;
-}
@@ -1,37 +1,12 @@
-#include <stdio.h>
-#include <stdarg.h>
-
-#define bool int
-#define true 1
-#define false 0
-
-void print_ary( int ary[] , int size ) {
- int i;
- printf( "[ " );
- for( i=0; i<size ; ++i )
- printf( "%d " , ary[i] );
- printf( "]\n");
-}
-
-bool is_sorted( int ary[] , int size ) {
- while(--size)
- if( ary[size] < ary[size-1] )
- return false;
- return true;
-}
-
-void populate( int ary[] , int size ) {
- int i;
- for( i=0 ; i<size ; ++i ) ary[i]=rand();
-}
-
-
+#include "shared.c"
void sort( int ary[] , int size ) {
// arrays with size <= 1 are already sorted
if( size <= 1 ) return;
- int middle=size/2 , ary1size=middle , ary2size=size-middle , ary1[ary1size] , ary2[ary2size] , i , ary1i , ary2i;
+ int middle=size/2 , i , // whole ary vars
+ ary1size=middle , ary1[ary1size] , ary1i , // left half vars
+ ary2size=size-middle , ary2[ary2size] , ary2i ; // right half vars
// split the array in half
for( i=0 ; i<middle ; ++i ) ary1[i] = ary[i];
@@ -41,7 +16,7 @@ void sort( int ary[] , int size ) {
sort( ary1 , ary1size );
sort( ary2 , ary2size );
- // merge the halves either half is completely merged
+ // merge the halves until either half is completely merged
for( i=ary1i=ary2i=0 ; i<size ; ++i )
if( ary1i == ary1size || ary2i == ary2size )
break;
@@ -52,18 +27,3 @@ void sort( int ary[] , int size ) {
if( ary1i != ary1size) for( ; i<size ; ++i ) ary[i] = ary1[ary1i++];
if( ary2i != ary2size) for( ; i<size ; ++i ) ary[i] = ary2[ary2i++];
}
-
-
-// pass in array size, and number of times to test
-int main( int argc , char const *argv[] ) {
- int i=0 , size=atoi(argv[1]) , ary[size] , times_to_test=atoi(argv[2]);
- while( i++ < times_to_test ) {
- populate(ary,size);
- sort(ary,size);
- if( !is_sorted(ary,size) ) {
- printf( "failure: not sorted\n" );
- print_ary(ary,size);
- }
- }
- return 0;
-}
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <stdarg.h>
+
+#define bool int
+#define true 1
+#define false 0
+
+void print_ary( int ary[] , int size ) {
+ int i;
+ printf( "[ " );
+ for( i=0 ; i<size ; ++i ) printf( "%d " , ary[i] );
+ printf( "]\n");
+}
+
+bool is_sorted( int ary[] , int size ) {
+ while(--size) if( ary[size] < ary[size-1] ) return false;
+ return true;
+}
+
+void populate( int ary[] , int size ) {
+ int i;
+ for( i=0 ; i<size ; ++i ) ary[i]=rand(); // pseudo random number
+}
+
+void sort( int* , int );
+
+// pass in array size, and number of times to test
+int main( int argc , char const *argv[] ) {
+ int i=0 , size=atoi(argv[1]) , ary[size] , times_to_test=atoi(argv[2]);
+ while( i++ < times_to_test ) {
+ populate(ary,size);
+ sort(ary,size);
+ // if( !is_sorted(ary,size) ) {
+ // printf( "failure: not sorted\n" );
+ // print_ary(ary,size);
+ // }
+ }
+ return 0;
+}

0 comments on commit 99058f7

Please sign in to comment.