둘째 주 SHH 데이터 구조 - [프로젝트 3 - 복잡 도 체험 (1)]

3342 단어
프로그램 (1):
<span style="font-size:14px;color:#000000;">#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAXNUM 100000
void selectsort(int a[], int n)
{
        int i, j, k, tmp;
        for(i = 0; i < n-1; i++)
        {
                k = i;
                for(j = i+1; j < n; j++)
                {
                        if(a[j] < a[k])
                                k = j;
                }
                if(k != j)
                {
                        tmp = a[i];
                        a[i] = a[k];
                        a[k] = tmp;
                }
        }
}

int main()
{
    int x[MAXNUM];
    int n = 0;
    double t1,t2;
    FILE *fp;
    fp = fopen("numbers.txt", "r");
    while(fscanf(fp, "%d", &x[n])!=EOF)
        n++;
    printf("   :%d,     ....", n);
    t1=time(0);
    selectsort(x, n);
    t2=time(0);
    printf("   %d  !", (int)(t2-t1));
    fclose(fp);
    return 0;
}</span>
<img src="http://img.blog.csdn.net/20150914164935627?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

프로그램 (2):
 
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAXNUM 100000
void quicksort(int data[],int first,int last)
{
    int i, j, t, base;
    if (first>last)
        return;
    base=data[first];
    i=first;
    j=last;
    while(i!=j)
    {
        while(data[j]>=base && i<j)
            j--;
        while(data[i]<=base && i<j)
            i++;
        /*     */
        if(i<j)
        {
            t=data[i];
            data[i]=data[j];
            data[j]=t;
        }
    }
    data[first]=data[i];
    data[i]=base;
    quicksort(data,first,i-1);
    quicksort(data,i+1,last);
}

int main()
{
    int x[MAXNUM];
    int n = 0;
    double t1,t2;
    FILE *fp;
    fp = fopen("numbers.txt", "r");
    while(fscanf(fp, "%d", &x[n])!=EOF)
        n++;
    printf("   :%d,     ....", n);
    t1=time(0);
    quicksort(x, 0, n-1);
    t2=time(0);
    printf("   %d  !", (int)(t2-t1));
    fclose(fp);
    return 0;
}
<img src="http://img.blog.csdn.net/20150914165135209?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<span style="font-size:18px;">  :</span>
<span style="font-size:18px;">                ,           ,            。
</span>

좋은 웹페이지 즐겨찾기