• 排序


    void Insert_Sort(RecType R[],int n)  
    {    int i,j;
          for(i=2;i<=n; i++)  //表示待插入元素的下标
          {    R[0]=R[i]; //设置监视哨保存待插入元素,腾出R[i]空间
                j=i-1;      //j指示当前空位置的前一个元素
                while(R[0].key<R[j].key)//搜索插入位置并后移腾出空
              {    R[j+1]=R[j]; 
                    j--; 
              }
              R[j+1]=R[0]; //插入元素
          }
    	  	printf("直接插入排序:
    ");
    	  for(int k=1;k<=n;k++)
    	  printf("%d
    ",R[k]);
    }
    //希尔排序算法如下:
    void ShellInsert ( RecType R[], int dk ,int n) 
    {
    for (int i=dk+1; i<=n; ++i )
    if ( R[i].key< R[i-dk].key) 
    {
            R[0] = R[i];            // 暂存在R[0]
            for(int j=i-dk;j>0&&(R[0].key<R[j].key);j-=dk)
                R[j+dk] =R[j];  // 记录后移,查找插入位置
    R[j+dk] = R[0];                // 插入
    } // if
    printf("希尔排序:
    ");
    for (int j=1; j<=n; ++j )
    printf("%d
    ",R[j]);
    } // ShellInsert
    void ShellSort (RecType R[], int dlta[], int t)
    {    // 增量为dlta[]的希尔排序	
    for (int k=0; k<t; ++t)
    ShellInsert(R, dlta[k],t);  //一趟增量为dlta[k]的插入排序
    } // ShellSort
    //冒泡排序算法如下:
    void bubble_sort(int n,RecType a[])
    {
    	for(int i=1;i<=n;i++){
    		for(int j=i+1;j<n;j++){
    			if(a[i].key>a[j].key){
    			int temp=a[i].key;
    			a[i].key=a[j].key;
    			a[j].key=temp;
    			}
    		}
    	}
    	printf("冒泡排序:
    ");
    for(int k=1;k<=n;k++)
    printf("%d
    ",a[k]);
    }
    //快速排序算法如下:
    int Partition (RecType R[], int low, int high) 
    {
    R[0] = R[low];  
    int pivotkey = R[low].key;  // 枢轴  
    while (low<high) 
    {
    while(low<high&& R[high].key>=pivotkey)
    -- high;      // 从右向左搜索
    R[low] = R[high];
    while (low<high && R[low].key<=pivotkey) 
            ++ low;      // 从左向右搜索
    R[high] = R[low];
    }
    R[low] = R[0];     return low;
    }// Partition         
    void QSort(RecType R[],  int s,  int  t ) 
    {          // 对记录序列R[s..t]进行快速排序  
    if (s < t) // 长度大于1
    {             
    int pivotloc = Partition(R, s, t); // 对 R[s..t] 进行一次划分
    QSort(R, s, pivotloc-1);
    QSort(R, pivotloc+1, t); 
    }
    printf("快速排序:
    ");
    printf("|");
     for(int k=s;k<=t;k++)
    	 printf("%d,",R[k]);
     printf("|");
     // QSort
    }
    //)简单选择排序的算法描述如下:
    void SelectSort (RecType R[], int n ) // 对记录序列R[1..n]作简单选择排序。
    {int i,j,k;
    for (i=1; i<n; ++i) // 选择第 i 小的记录,并交换到位
    { 
    for(k=i,j=i+1;j<=n;j++)
    {
    	if(R[j].key<R[k].key)
    		k=j;
    }
    if(i!=k)
    {
    	R[0]=R[k];
    	R[k]=R[i];
    	R[i]=R[0];
    }
    }
    printf("
    ");
    printf("简单选择排序:
    ");
    for(k=1;k<=n;k++)
    printf("%d
    ",R[k]);
    } // SelectSort
    //堆排序算法描述如下:
    
    void HeapAdjust(RecType R[], int s, int m)
    {   
    RecType rc = R[s];    // 暂存 R[s] 
    for (int j=2*s; j<=m; j*=2 ) // j 初值指向左孩子
    { 
        if ( j<m && R[j].key<R[j+1].key )  
    ++j;     
    if ( rc.key >= R[j].key )  
    break; 
    R[s] = R[j];   
    s = j;    
    }
    R[s] = rc;  
    } // HeapAdjust
    void HeapSort( RecType R[] ,int n) // 对顺序表 H 进行堆排序
    {  int i;
    for ( i=n/2;   i>0;   --i )
         HeapAdjust ( R, i, n);    // 建大顶堆
    for ( i=n; i>1; --i ) 
    {
         R[0]=R[1];
    	 R[1]=R[i];
    	 R[i]=R[0];
         HeapAdjust(R, 1, i-1);  // 对 H.r[1] 进行筛选
    }
    printf("堆排序:
    ");
    for(int k=1;k<=n;k++)
    printf("%d
    ",R[k]);
    } // HeapSort
    //归并排序算法描述如下:
    void Merge(RecType r1[],int low,int mid,int high,RecType r[])
    {
    	int i=low,j=mid+1,k=low;
    	while((i<=mid)&&(j<=high))
    	{
    		if(r1[i].key<=r1[j].key)
    		{
    			r[k]=r1[i];
    			++i;
    		}
    		else {
    			r[k]=r1[j];
    			++j;
    		}
    		++k;
    	}
    	while(i<=mid)
    		r[k++]=r1[i++];
    	while(j<=high)
    		r[k++]=r1[j++];
    }
    void Msort ( RecType SR[],RecType TR1[], int s, int t ) 
    {        // 将SR[s..t] 归并排序为 TR1[s..t]
    if (s==t) 
    TR1[s]=SR[s];
    else 
    {
    int m = (s+t)/2;
        Msort (SR, TR1, s, m); // 递归地将SR[s..m]归并为有序的TR1[s..m]
    Msort (SR, TR1, m+1, t);
        Merge (SR,s,m,t,TR1);
     }
     printf("
    ");
     printf("归并排序:
    ");
     printf("|");
     for(int k=s;k<=t;k++)
    	 printf("%d,",TR1[k]);
     printf("|");
    } // Msort
    

      






    #include <stdio.h> typedef int KeyType ; typedef struct dateType { KeyType key ; } RecType; void Insert_sort(RecType R[],int n) { int i,j ; for(i=2;i<=n;i++) { R[0]=R[i] ; j=i-1 ; while(R[0].key < R[j].key) { R[j+1]=R[j] ; j-- ; } R[j+1]=R[0] ; } printf("直接插入排序: "); for(int k=1;k<=n ;k++) printf("%d ",R[k]); } void shellInsert (RecType R[],int dk,int n) { for(int i=dk+1 ; i<=n ; ++i) if(R[i].key < R[i-dk].key) { R[0]=R[i] ; for(int j=i-dk; j>0&& (R[0].key < R[j].key) ;j-=dk) R[j+dk]=R[j]; R[j+dk]=R[0]; } printf("希尔排序: "); for(int j=1;j<=n;++j) printf("%d ",R[j]); } void shellsort(RecType R[],int dlta,int t) { for(int k=0; k<t; ++t) shellInsert (R,dlta[k] ,t); } void bubble_sort(int n,RecType a[]) { for(int i=1;i<=n;i++) { for(int j=i+1 ; j<n;j++) { if(a[i].key < a[j].key) { int temp=a[i].key ; a[i].key = a[j].key ; a[j].key = temp ; } } } printf("冒泡排序: "); for(int k=1;k<=n ; k++) printf("%d ",a[k]); } int partition(RecType R[],int low,int high) { R[0]=R[low] ; int pivotkey=R[low].key ; while(low<high) { while(low<high && R[high].key>=pivotkey) --high; R[low] =R[high] while(low<high && R[low].key<=pivotkey) ++low ; R[high]=R[low] ; } R[low] =R[0]; return low; } void qsort(RecType R[],int s,int t) { if(s<t) { int pivotloc= partition(R,s,t); qsort(R,s,pivotloc-1); qsort(R,pivotloc+1,t); } printf("快速排序: "); printf("|"); } void selectsort(RecType R[],int n) { int i,j,k ; for(i=1;i<n;++i) { for(k=i,j=i+1;j<=n ;j++) { if(R[j].key < R[k].key) k=j ; } if(i!=k) { R[0]=R[k]; R[k]=R[i]; R[i]=R[0]; } } printf(" "); printf("简单选择排序: "); for(int k=1;k<=n ; k++) printf("%d ",R[k]); } void heapadjust(RecType R[],int s,int m) { RecType rc=R[s] ; for(int j=2*s; j<=m; j*=2) { if(j<m && R[j].key < R[j+1].key) ++j ; if(rc.key>=R[j].key) break ; R[s]=R[j]; s=j ; } R[s]=rc ; } void heapsort(RecType R[],int n) { int i ; for(i=n/2; i>0 ; --i) heapadjust(R,i,n); for(i=n; i>1; --i) { R[0]=R[1] ; R[1]=R[i] ; R[i]=R[0] ; heapadjust(R,1,i-1); } printf("堆排序: "); for(int k=1;k<=n ; k++) printf("%d ",R[k]); } void merge(RecType r1[],int low,int mid,,int high,RecType r[]) { int i=low,j=mid+1,k=low ; while((i<=mid)&& (j<=high)) { if(r1[i].key<=r1[j].key) { r[k]=r1[i]; ++i; } else { r[k]=r1[j]; ++j; } ++k; } while(i<=mid) r[k++]=r1[i++]; while(j<=high) r[k++]=r1[j++]; } void msort(RecType SR[],RecType TR1[],int s,int t) { if(s==t) TR1[s]=SR[s]; else { int m=(s+t)/2; msort(SR,TR1,s,m); msort(SR,TR1,m+1,t); merge(SR,s,m,t,TR1); } printf(" "); printf("归并排序: "); printf("|"); for(int k=s;k<=t ; k++) printf("%d ",TR1[k]); printf("|"); } void main() { RecType R[20],R1[20]; int n,s,t,dk ; printf("请输入n ") ; scanf("%d",&n); printf("请输入n个数: ") ; for(int k=1;k<=n;k++) scanf("%d",&R[k]); Insert_sort(R,n) ; printf("请输入dk ") ; scanf("%d",&dk); shellInsert(R,dk,n); bubble_sort (n,R) ; printf("请输入s,t ") ; scanf("%d%d",&s,&t); qsort(R,s,t) ; selectsort(R,n); heapsort(R,n); msort(R,R1,s,t); }

      

  • 相关阅读:
    java生成UUID通用唯一识别码 (Universally Unique Identifier)
    使用ToolRunner运行Hadoop程序基本原理分析
    Hadoop入门经典:WordCount
    Hadoop配置文件
    【Nutch2.2.1基础教程之3】Nutch2.2.1配置文件
    8大排序算法图文讲解
    动态字典树_前缀相互查找(HDU_1671)
    DP_基本DP+排序(HDU_1421)
    斯特灵公式
    七种 qsort 排序方法
  • 原文地址:https://www.cnblogs.com/wc1903036673/p/3443850.html
Copyright © 2020-2023  润新知