Subversion Repositories programming

Rev

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

Rev 84 Rev 85
Line 17... Line 17...
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,
19
void copyarray_partial (int src[], int dst[], unsigned long src_left,
20
        unsigned long src_right);
20
        unsigned long src_right);
21
int* create_array (unsigned long size);
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);
-
 
-
 
22
 
24
int kth_smallest_method1 (int src_array[], unsigned long size, unsigned long position);
23
int kth_smallest_method1 (int src_array[], unsigned long size, unsigned long position);
25
int kth_smallest_method2 (int src_array[], unsigned long size, unsigned long position);
24
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);
25
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);
26
int kth_smallest_method4 (int src_array[], unsigned long size, unsigned long position);
28
 
27
 
Line 379... Line 378...
379
int kth_smallest_method4 (int src_array[], unsigned long size, unsigned long position)
378
int kth_smallest_method4 (int src_array[], unsigned long size, unsigned long position)
380
{
379
{
381
    int *array;
380
    int *array;
382
    int *medianarray;
381
    int *medianarray;
383
    int *temparray = NULL;
382
    int *temparray = NULL;
384
    int group_size = 3;
383
    int group_size = 11;
385
    int i, left=0, right=group_size-1;
384
    int i, left=0, right=group_size-1;
386
    int num_groups = size/group_size;
385
    int num_groups = size/group_size;
387
    int retval;
386
    int retval;
388
    int pivotpos;
387
    int pivotpos;
389
 
388
 
Line 431... Line 430...
431
 
430
 
432
    /* find pivotpos using partition() */
431
    /* find pivotpos using partition() */
433
    pivotpos = partition (array, 0, size-1);
432
    pivotpos = partition (array, 0, size-1);
434
 
433
 
435
    if (position == pivotpos)
434
    if (position == pivotpos)
436
    {
-
 
437
        free (array); array = NULL;
-
 
438
        free (medianarray); medianarray = NULL;
-
 
439
 
-
 
440
        return retval;
435
    { /* no-op */ }
441
    }
-
 
442
    else if (position < pivotpos)
436
    else if (position < pivotpos)
443
    {
437
    {
444
        temparray = create_array (pivotpos);
438
        temparray = create_array (pivotpos);
445
        copyarray_partial (array, temparray, 0, pivotpos-1);
439
        copyarray_partial (array, temparray, 0, pivotpos-1);
446
        retval = kth_smallest_method4 (temparray, pivotpos, position);
440
        retval = kth_smallest_method4 (temparray, pivotpos, position);
447
 
-
 
448
        free (array); array = NULL;
-
 
449
        free (medianarray); medianarray = NULL;
-
 
450
        free (temparray); temparray = NULL;
-
 
451
 
-
 
452
        return retval;
-
 
453
    }
441
    }
454
    else
442
    else
455
    {
443
    {
456
        temparray = create_array (size-(pivotpos+1));
444
        temparray = create_array (size-(pivotpos+1));
457
        copyarray_partial (array, temparray, pivotpos+1, size-1);
445
        copyarray_partial (array, temparray, pivotpos+1, size-1);
458
        retval = kth_smallest_method4 (temparray, size-(pivotpos+1), position-(pivotpos+1));
446
        retval = kth_smallest_method4 (temparray, size-(pivotpos+1), position-(pivotpos+1));
459
 
-
 
460
        free (array); array = NULL;
-
 
461
        free (medianarray); medianarray = NULL;
-
 
462
        free (temparray); temparray = NULL;
-
 
463
 
-
 
464
        return retval;
-
 
465
    }
447
    }
466
    
448
    
467
    /* shouldn't need any of this */
-
 
468
    free (array); array = NULL;
449
    free (array); array = NULL;
469
    free (medianarray); medianarray = NULL;
450
    free (medianarray); medianarray = NULL;
470
    if (temparray != NULL)
451
    if (temparray != NULL)
471
    {
452
    {
472
        free (temparray); temparray = NULL;
453
        free (temparray); temparray = NULL;
Line 487... Line 468...
487
    }
468
    }
488
 
469
 
489
    return array;
470
    return array;
490
}
471
}
491
 
472
 
492
int** create_matrix (unsigned long rows, unsigned long cols)
-
 
493
{
-
 
494
    int **array;
-
 
495
    int i;
-
 
496
    
-
 
497
    if ( (array = (int**) malloc (rows*sizeof(int*))) == NULL)
-
 
498
    {
-
 
499
        printf ("Out of memory creating matrix of size=%lux%lu\n", rows, cols);
-
 
500
        printf ("Exiting...");
-
 
501
        exit (1);
-
 
502
    }
-
 
503
 
-
 
504
    for (i=0; i<rows; i++)
-
 
505
    {
-
 
506
        if ( (array[i] = (int*) malloc (cols*sizeof(int))) == NULL)
-
 
507
        {
-
 
508
            printf ("Out of memory creating matrix of size=%lux%lu\n", rows, cols);
-
 
509
            printf ("Exiting...");
-
 
510
            exit (1);
-
 
511
        }
-
 
512
    }
-
 
513
 
-
 
514
    return array;
-
 
515
}
-
 
516
 
-
 
517
void free_matrix (int **matrix, unsigned long rows, unsigned long cols)
-
 
518
{
-
 
519
    int i;
-
 
520
 
-
 
521
    for (i=0; i<rows; i++)
-
 
522
    {
-
 
523
        free (matrix[i]);
-
 
524
        matrix[i] = NULL;
-
 
525
    }
-
 
526
 
-
 
527
    free (matrix);
-
 
528
    matrix = NULL;
-
 
529
}
-
 
530
 
-