@@ -13,31 +13,32 @@ and implementations.
1313#include <math.h>
1414#include <string.h>
1515#include <assert.h>
16+ #include <limits.h>
1617#include "sorting_algs.h"
1718
1819
1920
2021
2122
2223
23- typedef void (* sort_func )(int [], int n );
24+ typedef void (* sort_func )(int [], size_t n );
2425
25- void mergesort_wrapper (int a [], int n )
26+ void mergesort_wrapper (int a [], size_t n )
2627{
2728 mergesort (a , 0 , n - 1 );
2829}
2930
30- void qsort_wrapper (int a [], int n )
31+ void qsort_wrapper (int a [], size_t n )
3132{
3233 quicksort (a , 0 , n - 1 );
3334}
3435
35- void iter_qsort_wrapper (int a [], int n )
36+ void iter_qsort_wrapper (int a [], size_t n )
3637{
3738 iter_quicksort (a , 0 , n - 1 );
3839}
3940
40- void qsort_insertion_wrapper (int a [], int n )
41+ void qsort_insertion_wrapper (int a [], size_t n )
4142{
4243 quick_insertionsort (a , 0 , n - 1 );
4344}
@@ -55,22 +56,26 @@ int cmp_int_lt(const void* a, const void* b)
5556 return 0 ;
5657}
5758
58- void qsort_lib_wrapper (int a [], int n )
59+ void qsort_lib_wrapper (int a [], size_t n )
5960{
6061 qsort (a , n , sizeof (int ), cmp_int_lt );
6162}
6263
64+ void qsort_my_wrapper (int a [], size_t n )
65+ {
66+ generic_qsort (a , n , sizeof (int ), cmp_int_lt );
67+ }
6368
64- typedef struct
69+ typedef struct sort_alg
6570{
6671 sort_func func ;
6772 char name [40 ];
68- } test_struct ;
73+ } sort_alg ;
6974
7075
7176
72-
73- test_struct tests [] =
77+ /*
78+ sort_alg algorithms [] =
7479{
7580 { iterative_merge, "itermerge" },
7681 { heapsort, "hsort" },
@@ -82,9 +87,29 @@ test_struct tests[] =
8287 { qsort_insertion_wrapper, "qinssort" }
8388};
8489
90+ */
91+
92+ sort_alg algorithms [] =
93+ {
94+ { qsort_wrapper , "qsort" },
95+ { qsort_lib_wrapper , "qsortlib" },
96+ { qsort_my_wrapper , "genericqsort" },
97+ { iter_qsort_wrapper , "iterqsort" },
98+ { qsort_insertion_wrapper , "qinssort" }
99+ };
85100
86- #define NUM_ALGS (sizeof(tests)/sizeof(test_struct))
87- #define NUM_N 13
101+
102+ size_t n2_sizes [] = { 10 , 50 , 500 , 1000 , 5000 , 10000 , 50000 };
103+
104+
105+ size_t n_sizes_small [] = { 10000 , 50000 , 100000 + 3 , 500000 - 5 , 1000000 };
106+ size_t n_sizes_med [] = { 10000 , 50000 , 100000 + 3 , 500000 - 5 , 1000000 , 5000000 , 10000000 , 50000000 };
107+ size_t n_sizes_giant [] = { 500000 - 5 , 1000000 , 5000000 , 10000000 , 50000000 , 100000000 , 500000000 };
108+
109+ size_t n_sizes [] = { 500000 , 1000000 , 5000000 , 10000000 , 50000000 , 100000000 , 500000000 };
110+
111+ #define NUM_ALGS (sizeof(algorithms)/sizeof(sort_alg))
112+ #define NUM_N (sizeof(n_sizes)/sizeof(size_t))
88113
89114
90115
@@ -93,38 +118,40 @@ test_struct tests[] =
93118
94119int main ()
95120{
96- int n_sizes [] = {10 , 50 , 500 , 1000 , 5000 , 10000 , 50000 , 100000 + 3 , 500000 - 5 , 1000000 , 5000000 , 10000000 , 50000000 };
97- int num_n = (sizeof (n_sizes )/sizeof (int ));
98- //num_n = 6;
99- int start_n = 5 ;
121+ int start_n = 0 ;
122+ int num_n = NUM_N ;
100123
101124 double table_results [NUM_ALGS ][NUM_N ];
102125 clock_t before ;
103126
104127 int * test_array , * temp_array ;
128+
129+ printf ("%u %d\n" , (unsigned )sizeof (int ), INT_MAX );
130+
131+ size_t i , j , k ;
105132
106- for (int i = start_n ; i < num_n ; ++ i ) {
133+ for (i = start_n ; i < num_n ; ++ i ) {
107134 srand (time (NULL ));
108135 test_array = (int * )malloc (n_sizes [i ] * sizeof (int ));
109136 temp_array = (int * )malloc (n_sizes [i ] * sizeof (int ));
110137
111- for (int j = 0 ; j < n_sizes [i ]; ++ j ) {
112- temp_array [j ] = test_array [j ] = rand () % ( 4 * n_sizes [ i ]) ;
138+ for (j = 0 ; j < n_sizes [i ]; ++ j ) {
139+ temp_array [j ] = test_array [j ] = rand ();
113140 }
114141
115- printf ("N = %d \n" , n_sizes [i ]);
142+ printf ("N = %lu \n" , n_sizes [i ]);
116143
117144 //loop through selected algorithms it test_struct
118- for (int j = 0 ; j < NUM_ALGS ; ++ j ) {
145+ for (j = 0 ; j < NUM_ALGS ; ++ j ) {
119146 before = clock ();
120- tests [j ].func (temp_array , n_sizes [i ]);
147+ algorithms [j ].func (temp_array , n_sizes [i ]);
121148 table_results [j ][i ] = ((double )(clock () - before ))/CLOCKS_PER_SEC ;
122149
123150 //check results
124151#ifdef DEBUG
125- for (int k = 1 ; k < n_sizes [i ]; ++ k ) {
152+ for (k = 1 ; k < n_sizes [i ]; ++ k ) {
126153 if (temp_array [k - 1 ] > temp_array [k ]) {
127- printf ("\n\n%s faled \n" , tests [j ].name );
154+ printf ("\n\n%s failed \n" , algorithms [j ].name );
128155 assert (temp_array [k - 1 ] <= temp_array [k ]);
129156 }
130157 }
@@ -142,16 +169,16 @@ int main()
142169 printf ("%50s\n" , "Sort time in milliseconds" );
143170 printf ("%10s" , "N" );
144171 int len ;
145- for (int j = 0 ; j < NUM_ALGS ; ++ j ) {
146- len = strlen (tests [j ].name );
147- printf ("%*s" , ((len > 10 ) ? len + 2 : 10 ), tests [j ].name );
172+ for (j = 0 ; j < NUM_ALGS ; ++ j ) {
173+ len = strlen (algorithms [j ].name );
174+ printf ("%*s" , ((len > 10 ) ? len + 2 : 10 ), algorithms [j ].name );
148175 }
149176 putchar ('\n' );
150177
151- for (int i = start_n ; i < num_n ; i ++ ) {
152- printf ("%10d " , n_sizes [i ]);
153- for (int j = 0 ; j < NUM_ALGS ; j ++ ) {
154- len = strlen (tests [j ].name );
178+ for (i = start_n ; i < num_n ; i ++ ) {
179+ printf ("%10lu " , n_sizes [i ]);
180+ for (j = 0 ; j < NUM_ALGS ; j ++ ) {
181+ len = strlen (algorithms [j ].name );
155182 printf ("%*.0f" , ((len > 10 ) ? len + 2 : 10 ), table_results [j ][i ]* 1000 );
156183 }
157184 putchar ('\n' );
@@ -161,13 +188,13 @@ int main()
161188 FILE * file = fopen ("output.dat" , "w" );
162189
163190 fprintf (file , "%*sN" , 11 , " " );
164- for (int i = start_n ; i < num_n ; ++ i )
165- fprintf (file , "%12d " , n_sizes [i ]);
191+ for (i = start_n ; i < num_n ; ++ i )
192+ fprintf (file , "%12lu " , n_sizes [i ]);
166193 fputc ('\n' , file );
167194
168- for (int i = 0 ; i < NUM_ALGS ; ++ i ) {
169- fprintf (file , "%*s" , 12 , tests [i ].name );
170- for (int j = start_n ; j < num_n ; ++ j )
195+ for (i = 0 ; i < NUM_ALGS ; ++ i ) {
196+ fprintf (file , "%*s" , 12 , algorithms [i ].name );
197+ for (j = start_n ; j < num_n ; ++ j )
171198 fprintf (file , "%*.0f" , 12 , table_results [i ][j ]* 1000 );
172199 fputc ('\n' , file );
173200 }
0 commit comments