데이터 구조의 정렬 실현

3555 단어 데이터 구조J#
더 읽 기
1. 정렬 삽입
 
void InsertSort(RecType R[],int n){
    int i,j,k;
    RecType temp;
    for (i=1; i=0&&temp.key 
 

 2、

 

void BubbleSort(RecType R[],int n){
    int i,j,k;
    RecType temp;
    for (i=0; ii; j--) {
            if(R[j].key 
 

 

3、

 

void SelectSort(RecType R[],int n){
    int i,j,k;
    RecType temp;
    for (i=0; i 
 

 4、

 


void ShellSort(RecType R[],int n){
    int i,j,d,k;
    RecType temp;
    d=n/2;
    while (d>0) {
        for (i=d; i=0&&R[j].key>R[j+d].key) {
                temp=R[j];
                R[j]=R[j+d];
                R[j+d]=temp;
                j=j-d;
            }
        }
   
       printf("  d=%d:    ",d);
       for (k=0; k 
 

 

5、

 

void QuickSort(RecType R[],int s,int t){
    int i=s,j=t,k;
    RecType temp;
    if(si&&R[j].key>temp.key)
                   j--;
            if(i   
 

6、

 



void Sift(RecType R[],int low,int high){

    int i=low,j=2*i;
    RecType temp=R[i];
    while(j<=high){
        if(j=1;i--)
        Sift(R,i,n);
    printf("   : ");DispHeap(R,1,n);printf("
"); for (i=n; i>=2; i--) { printf(" %d %d, %d
",R[i].key,R[1].key,R[1].key); temp=R[1]; R[1]=R[i]; R[i]=temp; Sift(R,1,i-1); printf(" :"); DispHeap(R,1,i-1); printf("
"); } }

 
7. 병합 정렬
 
 
void Merge(RecType *R,int low,int m,int high)
{//         R[low..m) R[m+1..high]           R[low..high]
    int i=low,j=m+1,p=0; //    
    RecType *R1; //R1     
    R1=(RecType *)malloc((high-low+1)*sizeof(RecType));
    if(!R1)return; //      
    while(i<=m&&j<=high) //              R1[p] 
        R1[p++]=(R[i].key<=R[j].key)?R[i++]:R[j++];
    while(i<=m) //  1      ,        R1 
        R1[p++]=R[i++];
    while(j<=high) //  2      ,        R1 
        R1[p++]=R[j++];
    for(p=0,i=low;i<=high;p++,i++)
        R[i]=R1[p];//           R[low..high]
}

void MergeSort(RecType R[],int low,int high)
{//     R[low..high]        
    int mid;
    if(low   

좋은 웹페이지 즐겨찾기