Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

CTDL & GT - sort

#include <stdio.h>

#include <stdlib.h>

typedef struct data

{

        int ma;

        char ht[10];

        } data;

void doicho(int *a,int *b)

{

     int tg;

     tg = *a;

     *a=*b;

     *b=tg;

     }

void selection_sort (int a[],int n)

{

     int temp ,i,j;

     for(i=0;i<n-1;i++)

     {

                   temp=i;

                   for(j=i+1;j<n;j++)  

                                       if (a[j]<a[temp]) temp =j;

                   if (temp!=i) doicho(&a[i],&a[temp]);

                     }

     }

void insertion_sort(int a[],int n)

{

     int pos,i,temp;

     for(i=1;i<n;i++)

                     {

                                temp=a[i];

                                pos = i-1;

                                while ((pos>=0)&& (a[pos]>temp))

                                      {

                                                  a[pos+1] =a[pos];

                                                  pos--;

                                                  }

                                      a[pos+1]=temp;

                                }

     }

void bubblesort (int a[],int n)

{

     int i,j;

     for(i=0;i<n-1;i++)

                     for(j=n-1;j>i;j--)

                     if (a[j]<a[j-1]) doicho (&a[j],&a[j-1]);

     }

void   ShellSort (int a[], int N, int h[], int k)

{int step,i,j;

int x,len;

for (step=0;step<k;step++)

{len=h[step];

for (i=len;i<N; i++)

{x=a[i];

j=i-len;

while ((x<a[j])&&(j>-1))

{a[j+len]=a[j];

j=j-len;

}

a[j+len]=x;

}

}

}

void QuickSort (int a[], int l, int r)

{int i,j,x;

x=a[(l+r)/2];

i=l;  j=r;

do

{while (a[i]<x)i++;

while (a[j]>x)j--;

if (i<=j)

{doicho(&a[i],&a[j]);

i++;

j--;

}

} while (i<j);

if (l<j)    QuickSort(a,l,j);

if (i<r)    QuickSort(a,i,r);

}

void Shift(int a [ ],int l, int r)

{int  x, i, j, cont;

i = l; j = 2*i+1;

    cont =1;  

x = a[i ];

while ((j<=r) and (cont))

{if (j <r)                      

    if (a[j]>a[j+1])    

j=j+1;

if (a[j]>x)

cont = 0;

else

{doicho(&a[i],&a[j]);

i = j;             

j=2*i+1;

      }

      }

}

void  CreateHeap(int a[ ],int N)

{

int   i;

i = N/2;

while (i > 0)

{i--;

Shift(a,i,N-1);

}

}

void  Heapsort(int a[],int N)

{

int   r;

r = N-1;

while (r > 0)

{

doicho(&a[0],&a[r]);

r--;

Shift(a,0,r);

}

}

void input (data *a)

{

     char temp[5];

     fflush(stdin);

     printf("

Nhap Ma: "); gets(temp); a->ma=atoi(temp); 

     fflush(stdin);

     printf("

Nhap ho te: "); gets(a->ht);

     }

void output(data a)

{

     printf("

Ma: %3d \t HoTen: %10s ",a.ma,a.ht);

     }

int main(int argc, char *argv[])

{

    int a[]={7,4,3,6,8,5,9,1,2};

    int h[]={5,1,3};

    //ShellSort(a,9,h,3);

    //QuickSort(a,0,9);

    //insertion_sort(a,9);

    //selection_sort(a,9)

    /*CreateHeap(a,9);

    Heapsort(a,9);*/

    data *b;

    b=(data*)malloc(5*sizeof(data));

    int i;

    for(i=0;i<5;i++)

    {

           input(&b[i]);         

                    }

    //for(int i=0;i<9;i++)

    //printf("%2d",a[i]);

    for(i=0;i<5;i++)

    output(b[i]);

    printf("

");

    system("PAUSE");

    return 0;

}

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: