Subversion Repositories programming

Rev

Rev 82 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 82 Rev 83
Line 14... Line 14...
14
void swap (int *left, int *right);
14
void swap (int *left, int *right);
15
void printarray (int array[], unsigned long size);
15
void printarray (int array[], unsigned long size);
16
void randomfill (int array[], unsigned long size);
16
void randomfill (int array[], unsigned long size);
17
int partition (int array[], unsigned long left, unsigned long right);
17
int partition (int array[], unsigned long left, unsigned long right);
18
void copyarray (int src[], int dst[], unsigned long size);
18
void copyarray (int src[], int dst[], unsigned long size);
-
 
19
void copyarray_partial (int src[], int dst[], unsigned long src_left,
-
 
20
        unsigned long src_right);
-
 
21
int* create_array (unsigned long size);
-
 
22
int** create_matrix (unsigned long rows, unsigned long cols);
-
 
23
void free_matrix (int **matrix, unsigned long rows, unsigned long cols);
19
int kth_smallest_method1 (int src_array[], unsigned long size, unsigned long position);
24
int kth_smallest_method1 (int src_array[], unsigned long size, unsigned long position);
20
int kth_smallest_method2 (int src_array[], unsigned long size, unsigned long position);
25
int kth_smallest_method2 (int src_array[], unsigned long size, unsigned long position);
-
 
26
int kth_smallest_method3 (int src_array[], unsigned long size, unsigned long position);
-
 
27
int kth_smallest_method4 (int src_array[], unsigned long size, unsigned long position);
21
 
28
 
22
void startclock (void);
29
void startclock (void);
23
void stopclock (void);
30
void stopclock (void);
24
void printtimetaken (char *funcname);
31
void printtimetaken (char *funcname);
25
 
32
 
Line 34... Line 41...
34
 
41
 
35
    unsigned long size, kth_pos;
42
    unsigned long size, kth_pos;
36
    int* array;
43
    int* array;
37
    int i;
44
    int i;
38
    
45
    
39
    for (size=50; size<=3000; size+=50)
46
    for (size=1000000; size<=10000000; size+=1000000)
40
    {
47
    {
41
        if ( (array = (int*) malloc (size*sizeof(int))) == NULL)
-
 
42
        {
-
 
43
            printf ("Ran out of memory at size=%lu\n", size);
-
 
44
            exit (1);
48
        array = create_array (size);
45
        }
-
 
46
 
49
 
47
        /* fill the array with random values */
50
        /* fill the array with random values */
48
        randomfill (array, size);
51
        randomfill (array, size);
49
 
52
 
50
        for (i=0; i<5; i++)
53
        for (i=0; i<5; i++)
Line 81... Line 84...
81
            startclock();
84
            startclock();
82
            printf ("%luth smallest (method3): %d -- ", kth_pos, 
85
            printf ("%luth smallest (method3): %d -- ", kth_pos, 
83
                    kth_smallest_method3 (array, size, kth_pos));
86
                    kth_smallest_method3 (array, size, kth_pos));
84
            stopclock();
87
            stopclock();
85
            printtimetaken("Method3");
88
            printtimetaken("Method3");
-
 
89
            
-
 
90
            /* run method4 */
-
 
91
            startclock();
-
 
92
            printf ("%luth smallest (method4): %d -- ", kth_pos, 
-
 
93
                    kth_smallest_method4 (array, size, kth_pos));
-
 
94
            stopclock();
-
 
95
            printtimetaken("Method4");
86
 
96
            
87
            printf ("\n\n");
97
            printf ("\n\n");
88
        }
98
        }
89
        
99
        
90
        free (array);
-
 
91
        array = NULL;
100
        free (array); array = NULL;
92
    }
101
    }
93
 
102
 
94
    return 0;
103
    return 0;
95
}
104
}
96
 
105
 
Line 128... Line 137...
128
{
137
{
129
    unsigned long ai, bi, ci, i;
138
    unsigned long ai, bi, ci, i;
130
    int *C;
139
    int *C;
131
    unsigned long csize = asize+bsize;
140
    unsigned long csize = asize+bsize;
132
 
141
 
133
    if ( (C = (int*) malloc (csize * sizeof(int))) == NULL)
-
 
134
    {
-
 
135
        printf ("Out of memory... Exiting\n");
-
 
136
        exit (1);
142
    C = create_array (csize);
137
    }
-
 
138
   
143
 
139
    ai = 0;
144
    ai = 0;
140
    bi = 0;
145
    bi = 0;
141
    ci = 0;
146
    ci = 0;
142
   
147
   
143
    while ((ai < asize) && (bi < bsize)) 
148
    while ((ai < asize) && (bi < bsize)) 
Line 277... Line 282...
277
        unsigned long position)
282
        unsigned long position)
278
{
283
{
279
    int *array;
284
    int *array;
280
    int retval;
285
    int retval;
281
 
286
 
282
    if ( (array = (int*) malloc (size*sizeof(int))) == NULL)
-
 
283
    {
-
 
284
        printf ("Out of memory at size=%lu\n", size);
287
    array = create_array (size);
285
        exit(1);
-
 
286
    }
-
 
287
 
-
 
288
    copyarray (src_array, array, size);
288
    copyarray (src_array, array, size);
289
    
289
    
290
    mergesort (array, size);
290
    mergesort (array, size);
291
 
291
 
292
    retval = array[position];
292
    retval = array[position];
293
    free (array);
293
    free (array); array = NULL;
294
    array = NULL;
294
    
295
    return retval;
295
    return retval;
296
}
296
}
297
 
297
 
298
/* Find the kth smallest element in the array (starting from zero)
298
/* Find the kth smallest element in the array (starting from zero)
299
 *
299
 *
Line 306... Line 306...
306
    int retval;
306
    int retval;
307
    unsigned long left = 0;
307
    unsigned long left = 0;
308
    unsigned long right = size-1;
308
    unsigned long right = size-1;
309
    unsigned long pivotpos;
309
    unsigned long pivotpos;
310
    
310
    
311
    if ( (array = (int*) malloc (size*sizeof(int))) == NULL)
-
 
312
    {
-
 
313
        printf ("Out of memory at size=%lu\n", size);
-
 
314
        exit (1);
311
    array = create_array (size);
315
    }
-
 
316
    
-
 
317
    copyarray (src_array, array, size);
312
    copyarray (src_array, array, size);
318
 
313
 
319
    do
314
    do
320
    {
315
    {
321
        swap (&array[position], &array[left]);
316
        swap (&array[position], &array[left]);
Line 327... Line 322...
327
            right = pivotpos - 1;
322
            right = pivotpos - 1;
328
    }
323
    }
329
    while (pivotpos != position);
324
    while (pivotpos != position);
330
 
325
 
331
    retval = array[position];
326
    retval = array[position];
332
    free (array);
327
    free (array); array = NULL;
333
    array = NULL;
328
    
334
    return retval;
329
    return retval;
335
}
330
}
336
 
331
 
337
int kth_smallest_method3 (int src_array[], unsigned long size, unsigned long position)
332
int kth_smallest_method3 (int src_array[], unsigned long size, unsigned long position)
338
{
333
{
Line 341... Line 336...
341
    int retval;
336
    int retval;
342
    unsigned long left = 0;
337
    unsigned long left = 0;
343
    unsigned long right = size-1;
338
    unsigned long right = size-1;
344
    unsigned long pivotpos, newpos;
339
    unsigned long pivotpos, newpos;
345
 
340
 
346
    if ( (array = (int*) malloc (size*sizeof(int))) == NULL)
-
 
347
    {
-
 
348
        printf ("Out of memory at size=%lu\n", size);
-
 
349
        exit (1);
341
    array = create_array (size);
350
    }
-
 
351
 
-
 
352
    copyarray (src_array, array, size);
342
    copyarray (src_array, array, size);
353
 
343
 
354
    swap (&array[position], &array[left]);
344
    swap (&array[position], &array[left]);
355
    pivotpos = partition (array, left, right);
345
    pivotpos = partition (array, left, right);
356
 
346
 
357
    /* base case */
347
    /* base case */
358
    if (pivotpos == position)
348
    if (pivotpos == position)
359
    {
349
    {
360
        retval = array[position];
350
        retval = array[position];
361
        free (array);
-
 
362
        array = NULL;
351
        free (array); array = NULL;
-
 
352
 
363
        return retval;
353
        return retval;
364
    }
354
    }
365
    
355
    
366
    /* throw away left side */
356
    /* throw away left side */
367
    if (pivotpos < position)
357
    if (pivotpos < position)
Line 373... Line 363...
373
    {
363
    {
374
        right = pivotpos - 1;
364
        right = pivotpos - 1;
375
        newpos = position;
365
        newpos = position;
376
    }
366
    }
377
 
367
 
378
    if ( (temparray = (int*) malloc (size*sizeof(int))) == NULL)
368
    temparray = create_array (right-left+1);
-
 
369
    copyarray_partial (array, temparray, left, right);
-
 
370
 
-
 
371
    retval = kth_smallest_method3 (temparray, right-left+1, newpos);
-
 
372
 
-
 
373
    free (temparray); temparray = NULL;
-
 
374
    free (array); array = NULL;
-
 
375
    
-
 
376
    return retval;
-
 
377
}
-
 
378
 
-
 
379
int kth_smallest_method4 (int src_array[], unsigned long size, unsigned long position)
-
 
380
{
-
 
381
    int *array;
-
 
382
    int **matrix;
-
 
383
    int *medianarray;
-
 
384
    int *temparray = NULL;
-
 
385
    int group_size = 3;
-
 
386
    int i, left=0, right=group_size-1;
-
 
387
    int num_groups = size/group_size;
-
 
388
    int retval;
-
 
389
    int pivotpos;
-
 
390
 
-
 
391
    array = create_array (size);
-
 
392
    copyarray (src_array, array, size);
-
 
393
 
-
 
394
    /* base case */
-
 
395
    if (size <= group_size)
379
    {
396
    {
380
        printf ("Out of memory at size=%lu\n", size);
397
        mergesort (array, size);
-
 
398
        retval = array[position];
-
 
399
 
-
 
400
        free (array); array = NULL;
-
 
401
 
381
        exit (1);
402
        return retval;
382
    }
403
    }
-
 
404
    
-
 
405
    /* create the matrix as described in class */
-
 
406
    matrix = create_matrix (num_groups, group_size);
383
 
407
 
-
 
408
    for (i=0; i<num_groups; i++)
-
 
409
    {
384
    copyarray_partial (array, temparray, left, right);
410
        copyarray_partial (array, matrix[i], left, right);
-
 
411
        mergesort (matrix[i], group_size);
-
 
412
        left = right + 1;
-
 
413
        right = left + group_size - 1;
-
 
414
    }
385
 
415
 
-
 
416
    /* get the array of medians */
-
 
417
    medianarray = create_array (num_groups);
-
 
418
 
-
 
419
    for (i=0; i<num_groups; i++)
-
 
420
        medianarray[i] = matrix[i][group_size/2];
-
 
421
 
-
 
422
    /* find the median of medians */
386
    retval = kth_smallest_method3 (temparray, right-left+1, newpos);
423
    retval = kth_smallest_method4 (medianarray, num_groups, num_groups/2);
387
 
424
 
-
 
425
    /* move the mm to the 0th position in the array (for partition) */
-
 
426
    for (i=0; i<size; i++)
-
 
427
    {
-
 
428
        if (array[i] == retval)
-
 
429
        {
-
 
430
            swap (&array[i], &array[0]);
388
    free (temparray);
431
            break;
-
 
432
        }
-
 
433
    }
-
 
434
 
-
 
435
    /* find pivotpos using partition() */
-
 
436
    pivotpos = partition (array, 0, size-1);
-
 
437
 
-
 
438
    if (position == pivotpos)
-
 
439
    {
389
    temparray = NULL;
440
        free (array); array = NULL;
-
 
441
        free_matrix (matrix, num_groups, group_size);
-
 
442
        free (medianarray); medianarray = NULL;
-
 
443
 
-
 
444
        return retval;
-
 
445
    }
-
 
446
    else if (position < pivotpos)
-
 
447
    {
-
 
448
        temparray = create_array (pivotpos);
-
 
449
        copyarray_partial (array, temparray, 0, pivotpos-1);
-
 
450
        retval = kth_smallest_method4 (temparray, pivotpos, position);
-
 
451
 
390
    free (array);
452
        free (array); array = NULL;
-
 
453
        free_matrix (matrix, num_groups, group_size);
-
 
454
        free (medianarray); medianarray = NULL;
-
 
455
        free (temparray); temparray = NULL;
-
 
456
 
-
 
457
        return retval;
-
 
458
    }
-
 
459
    else
-
 
460
    {
-
 
461
        temparray = create_array (size-(pivotpos+1));
-
 
462
        copyarray_partial (array, temparray, pivotpos+1, size-1);
-
 
463
        retval = kth_smallest_method4 (temparray, size-(pivotpos+1), position-(pivotpos+1));
-
 
464
 
391
    array = NULL;
465
        free (array); array = NULL;
-
 
466
        free_matrix (matrix, num_groups, group_size);
-
 
467
        free (medianarray); medianarray = NULL;
-
 
468
        free (temparray); temparray = NULL;
-
 
469
 
-
 
470
        return retval;
-
 
471
    }
392
    
472
    
-
 
473
    /* shouldn't need any of this */
-
 
474
    free_matrix (matrix, num_groups, group_size);
-
 
475
    free (array); array = NULL;
-
 
476
    free (medianarray); medianarray = NULL;
-
 
477
    if (temparray != NULL)
-
 
478
    {
-
 
479
        free (temparray); temparray = NULL;
-
 
480
    }
-
 
481
 
393
    return retval;
482
    return retval;
394
}
483
}
395
 
484
 
-
 
485
int* create_array (unsigned long size)
-
 
486
{
-
 
487
    int *array;
-
 
488
 
-
 
489
    if ( (array = (int*) malloc (size*sizeof(int))) == NULL)
-
 
490
    {
-
 
491
        printf ("Out of memory allocating array of size=%lu\n", size);
-
 
492
        printf ("Exiting...");
-
 
493
        exit (1);
-
 
494
    }
-
 
495
 
-
 
496
    return array;
-
 
497
}
-
 
498
 
-
 
499
int** create_matrix (unsigned long rows, unsigned long cols)
-
 
500
{
-
 
501
    int **array;
-
 
502
    int i;
-
 
503
    
-
 
504
    if ( (array = (int**) malloc (rows*sizeof(int*))) == NULL)
-
 
505
    {
-
 
506
        printf ("Out of memory creating matrix of size=%lux%lu\n", rows, cols);
-
 
507
        printf ("Exiting...");
-
 
508
        exit (1);
-
 
509
    }
-
 
510
 
-
 
511
    for (i=0; i<rows; i++)
-
 
512
    {
-
 
513
        if ( (array[i] = (int*) malloc (cols*sizeof(int))) == NULL)
-
 
514
        {
-
 
515
            printf ("Out of memory creating matrix of size=%lux%lu\n", rows, cols);
-
 
516
            printf ("Exiting...");
-
 
517
            exit (1);
-
 
518
        }
-
 
519
    }
-
 
520
 
-
 
521
    return array;
-
 
522
}
-
 
523
 
-
 
524
void free_matrix (int **matrix, unsigned long rows, unsigned long cols)
-
 
525
{
-
 
526
    int i;
-
 
527
 
-
 
528
    for (i=0; i<rows; i++)
-
 
529
    {
-
 
530
        free (matrix[i]);
-
 
531
        matrix[i] = NULL;
-
 
532
    }
-
 
533
 
-
 
534
    free (matrix);
-
 
535
    matrix = NULL;
-
 
536
}
-
 
537