Subversion Repositories programming

Rev

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

Rev 83 Rev 84
Line 377... Line 377...
377
}
377
}
378
 
378
 
379
int kth_smallest_method4 (int src_array[], unsigned long size, unsigned long position)
379
int kth_smallest_method4 (int src_array[], unsigned long size, unsigned long position)
380
{
380
{
381
    int *array;
381
    int *array;
382
    int **matrix;
-
 
383
    int *medianarray;
382
    int *medianarray;
384
    int *temparray = NULL;
383
    int *temparray = NULL;
385
    int group_size = 3;
384
    int group_size = 3;
386
    int i, left=0, right=group_size-1;
385
    int i, left=0, right=group_size-1;
387
    int num_groups = size/group_size;
386
    int num_groups = size/group_size;
Line 400... Line 399...
400
        free (array); array = NULL;
399
        free (array); array = NULL;
401
 
400
 
402
        return retval;
401
        return retval;
403
    }
402
    }
404
    
403
    
405
    /* create the matrix as described in class */
404
    /* get the array of medians */
-
 
405
    medianarray = create_array (num_groups);
406
    matrix = create_matrix (num_groups, group_size);
406
    temparray = create_array (group_size);
407
 
407
    
408
    for (i=0; i<num_groups; i++)
408
    for (i=0; i<num_groups; i++)
409
    {
409
    {
410
        copyarray_partial (array, matrix[i], left, right);
410
        copyarray_partial (array, temparray, left, right);
411
        mergesort (matrix[i], group_size);
411
        mergesort (temparray, group_size);
-
 
412
        medianarray[i] = temparray[group_size/2];
412
        left = right + 1;
413
        left = right + 1;
413
        right = left + group_size - 1;
414
        right = left + group_size - 1;
414
    }
415
    }
415
 
416
 
416
    /* get the array of medians */
-
 
417
    medianarray = create_array (num_groups);
417
    free (temparray); temparray = NULL;
418
 
418
    
419
    for (i=0; i<num_groups; i++)
-
 
420
        medianarray[i] = matrix[i][group_size/2];
-
 
421
 
-
 
422
    /* find the median of medians */
419
    /* find the median of medians */
423
    retval = kth_smallest_method4 (medianarray, num_groups, num_groups/2);
420
    retval = kth_smallest_method4 (medianarray, num_groups, num_groups/2);
424
 
421
 
425
    /* move the mm to the 0th position in the array (for partition) */
422
    /* move the mm to the 0th position in the array (for partition) */
426
    for (i=0; i<size; i++)
423
    for (i=0; i<size; i++)
Line 436... Line 433...
436
    pivotpos = partition (array, 0, size-1);
433
    pivotpos = partition (array, 0, size-1);
437
 
434
 
438
    if (position == pivotpos)
435
    if (position == pivotpos)
439
    {
436
    {
440
        free (array); array = NULL;
437
        free (array); array = NULL;
441
        free_matrix (matrix, num_groups, group_size);
-
 
442
        free (medianarray); medianarray = NULL;
438
        free (medianarray); medianarray = NULL;
443
 
439
 
444
        return retval;
440
        return retval;
445
    }
441
    }
446
    else if (position < pivotpos)
442
    else if (position < pivotpos)
Line 448... Line 444...
448
        temparray = create_array (pivotpos);
444
        temparray = create_array (pivotpos);
449
        copyarray_partial (array, temparray, 0, pivotpos-1);
445
        copyarray_partial (array, temparray, 0, pivotpos-1);
450
        retval = kth_smallest_method4 (temparray, pivotpos, position);
446
        retval = kth_smallest_method4 (temparray, pivotpos, position);
451
 
447
 
452
        free (array); array = NULL;
448
        free (array); array = NULL;
453
        free_matrix (matrix, num_groups, group_size);
-
 
454
        free (medianarray); medianarray = NULL;
449
        free (medianarray); medianarray = NULL;
455
        free (temparray); temparray = NULL;
450
        free (temparray); temparray = NULL;
456
 
451
 
457
        return retval;
452
        return retval;
458
    }
453
    }
Line 461... Line 456...
461
        temparray = create_array (size-(pivotpos+1));
456
        temparray = create_array (size-(pivotpos+1));
462
        copyarray_partial (array, temparray, pivotpos+1, size-1);
457
        copyarray_partial (array, temparray, pivotpos+1, size-1);
463
        retval = kth_smallest_method4 (temparray, size-(pivotpos+1), position-(pivotpos+1));
458
        retval = kth_smallest_method4 (temparray, size-(pivotpos+1), position-(pivotpos+1));
464
 
459
 
465
        free (array); array = NULL;
460
        free (array); array = NULL;
466
        free_matrix (matrix, num_groups, group_size);
-
 
467
        free (medianarray); medianarray = NULL;
461
        free (medianarray); medianarray = NULL;
468
        free (temparray); temparray = NULL;
462
        free (temparray); temparray = NULL;
469
 
463
 
470
        return retval;
464
        return retval;
471
    }
465
    }
472
    
466
    
473
    /* shouldn't need any of this */
467
    /* shouldn't need any of this */
474
    free_matrix (matrix, num_groups, group_size);
-
 
475
    free (array); array = NULL;
468
    free (array); array = NULL;
476
    free (medianarray); medianarray = NULL;
469
    free (medianarray); medianarray = NULL;
477
    if (temparray != NULL)
470
    if (temparray != NULL)
478
    {
471
    {
479
        free (temparray); temparray = NULL;
472
        free (temparray); temparray = NULL;