1. 程式人生 > >王岐9.11-分治:快排;迴圈賽日程表-西工大演算法

王岐9.11-分治:快排;迴圈賽日程表-西工大演算法

快排:

#include <stdio.h>
#include <stdlib.h>

void qs (int l, int r);

int A[10] = {0, 2, 5, 7, 5, 9, 3, 0, 8, 4};

int main()
{
    qs (1, 9);
    int i;
    for (i = 1; i <= 9; i++)
    {
        printf ("%d ", A[i]);
    }
    return 0;
}

void qs (int l, int r)
{
    int i = l, j = r;
    if (l >= r)
    {
        return ;
    }
    else
    {
        A[0] = A[i];
        while (i != j)
        {
            while (i < j && A[j] < A[0]) j--;
            if (i < j) A[i++] = A[j];
            while (i < j && A[i] > A[0]) i++;
            if (i < j) A[j--] = A[i];
        }
        A[i] = A[0];
        qs (l, i - 1);
        qs (i + 1, r);
    }
}

迴圈賽日程表:

#include <stdio.h>
#include <stdlib.h>

int A[40][40];

void getTable (int start, int end);

int main()
{
    int n, i, j;
    scanf ("%d", &n);
    getTable (1, n);
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            printf ("%d ", A[i][j]);
        }
        printf ("\n");
    }
    return 0;
}

void getTable (int start, int end)
{
    int len;
    int i, j;
    if (end - start == 1)
    {
        A[start][1] =  start;
        A[start][2] = end;
        A[end][1] = end;
        A[end][2] = start;
        return;
    }
    len = (end - start + 1) / 2;
    getTable (start, start + len - 1);
    getTable (start + len, end);
    for (i = 0; i < len; i++)
    {
        for (j = 0; j < len; j++)
        {
            A[start + i + len][j + len + 1] = A[start + i][1 + j];
            A[start + i][j + len + 1] =A[start + i + len][1 + j];
        }
    }
}