冒泡排序, 選擇排序, 希爾排序,插入排序,快速排序

結果
在這裏插入圖片描述

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

typedef struct scoreTable{
    int id;
    char name[30];
    int politics ;
    int chinese ;
    int foreignLanguages ;
    int math ;
    int physics ;
    int chemistry ;
    int Biology ;
    int total_score ;
}scoreTable;

typedef struct Data{
    scoreTable * elem ;
    int length ;
}Data;

void CreateData(Data * L , int len , scoreTable *data)
{
    L->elem = malloc(sizeof(scoreTable)* len) ;
    L->length = len ;
    L->elem = data ;
}

void Print(Data L)
{
    int i ;
    printf("%-10s\t%-10s\t%-10s\t%-10s\t%-10s\n" , "准考證號" , "姓名" , "政治" , "語文", "總分");
    for(i = 0 ; i < L.length ; i++)
    {
        L.elem[i].total_score = L.elem[i].politics + L.elem[i].chinese + L.elem[i].foreignLanguages + L.elem[i].math
        + L.elem[i].physics + L.elem[i].chemistry + L.elem[i].Biology ;

        printf("%-10d\t%-10s\t%-10d\t%-10d\t%-10d\n" , L.elem[i].id , L.elem[i].name , L.elem[i].politics , L.elem[i].chinese , L.elem[i].total_score);
    }
}
int Find(Data L ,int id)
{
    int flag = 0 ;
    int i ;
    for( i= 0 ; i < L.length ; i++)
    {
        if(id == L.elem[i].id)
        {
            flag = 1 ;
            break ;
        }
    }
    return flag ;
}
Data BubbleSort(Data L)
{
    int i , j;
    scoreTable p ;
    for(i = 0 ; i < L.length -1; i++ )
    {
        for(j = 0 ; j < L.length-i-1 ; j++)
        {
            if(L.elem[j].id > L.elem[j+1].id)
            {
                p = L.elem[j];
                L.elem[j] = L.elem[j+1];
                L.elem[j+1] = p ;
            }
        }
    }
    return L ;
}

Data Selection_sort(Data L)
{
    int i , j , min;
    scoreTable p ;
    for(i = 0 ; i < L.length - 1 ; i++)
    {
        min = i ;
        for(j = i + 1 ; j < L.length ; j++)
        {
            if(L.elem[j].id < L.elem[min].id)
            {
               min = j ;
            }
            p = L.elem[i];
            L.elem[i] = L.elem[min];
            L.elem[i] = p ;
        }
    }
    return L ;
}

Data Quick_sort(Data L , int left , int right)
{
    if(left >= right)
    {
        return L;
    }
    int i = left ;
    int j = right ;
    scoreTable key = L.elem[left] ;
    while(i < j)
    {
        while(i < j && key.id <=  L.elem[j].id)
        {
            j-- ;
        }
        L.elem[i] = L.elem[j] ;
        while(i < j && key.id >= L.elem[i].id)
        {
            i++ ;
        }
        L.elem[j] = L.elem[i] ;
    }
    L.elem[i] = key ;
    Quick_sort(L , left , i -1) ;
    Quick_sort(L , i + 1 , right);
    return L ;
}


Data insertion_sort(Data L )
{
    int i , j ;
    scoreTable p ;
    for(i = 1 ; i < L.length ; i++)
    {
        p = L.elem[i] ;
        j = i - 1 ;
        while(j>= 0 && L.elem[j].id > p.id)
        {
            L.elem[j+1] = L.elem[j];
            j-- ;
        }
        if(j != i -1)
        {
            L.elem[j+1] = p ;
        }
    }
    return L ;
}

Data ShellSort(Data L)
{
    int i , j ,d ;
    scoreTable p ;
    for(d = L.length / 2 ; d >= 1 ; d /= 2)
    {
        for(i = d + 1 ; i < L.length ; i++)
        {
            p = L.elem[i] ;
            j = i - d ;
            while(j >= 0 && p.id < L.elem[j].id)
            {
                L.elem[j + d] = L.elem[j] ;
                j = j - d ;
            }
            L.elem[j + d] = p ;
        }
    }
    return L ;
}



int main()
{
    Data root ;
    scoreTable data[] = {
    {179328 , "何芳芳" , 85 , 89 , 98 , 100 , 93 , 80 , 47},
    {179325 , "陳紅" , 85 , 86 , 88 , 100 , 92 ,90 , 45},
    {179326 , "陸華" , 78 , 75 , 90 , 80 , 95 , 88 , 37},
    {179327 , "張平"  , 82 , 80 , 78 , 98 , 84 , 96 , 40},
    {179324 , "趙小怡" , 76 , 85 , 94 , 57 , 77 , 69 , 44}
    };

    CreateData(&root , 5 , data) ;

    Print(root);

    printf("冒泡排序\n");
    Print(BubbleSort(root));

    printf("選擇排序\n");
    Print(Selection_sort(root));

    printf("插入排序\n");
    Print(insertion_sort(root));

    printf("快速排序\n");
    Print(Quick_sort(root , 0 ,root.length));

    printf("希爾排序\n");
    Print(ShellSort(root));

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章