zoukankan      html  css  js  c++  java
  • 基本排序算法(C)

    /* Note:Your choice is C IDE */
    #include "stdio.h"
    void main()
    {
        int i,j,temp,d,a[10];
        printf("请输入数组的元素:\n");
        for(i=0; i<10; i++)
            scanf("%d",&a[i]);
        /*冒泡排序*/
        for(i=9; i>=1; i--)
            for(j=0; j<=i-1; j++)
                if(a[j+1]<a[j])
                {
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
        printf("冒泡排序后:\n");
        for(i=0; i<10; i++)
            printf("%8d",a[i]);
        printf("\n");
        /*插入排序*/
        for(i=1; i<10; i++)
        {
            temp=a[i];
            j=i-1;
            while(j>=0&&a[j]>temp)
            {
                a[j+1]=a[j];
                j--;
            }
            a[j+1]=temp;
        }
        printf("插入排序后:\n");
        for(i=0; i<10; i++)
            printf("%8d",a[i]);
        printf("\n");
        /*选择排序*/
        /*for(i=0;i<10;i++)
        {
            int min;
            min=a[i];
            for(j=i+1;j<10;j++)
            {
                if(a[j]<min)
                {
                    min=a[j];
                    temp=a[j];
                    a[j]=a[i];
                    a[i]=temp;
    
    
                }
            }
        }*/
        for(i=0; i<10; i++)
        {
            int k;
            k=i;
            for(j=i+1; j<10; j++)
            {
                if(a[j]<a[k])
                {
                    k=j;
                }
            }
         if(i!=k){
           temp=a[i];
           a[i] = a[k];
           a[k]=temp;
        }
    } printf(
    "选择排序后:\n"); for(i=0; i<10; i++) printf("%8d",a[i]); printf("\n"); /*希尔排序*/ for(d=10/2; d>=1; d=d/2) { for(i=d; i<10; i++) { temp=a[i]; j=i-d; while(j>=0&&a[j]>temp) { a[j+d]=a[j]; j=j-d; } a[j+d]=temp; } } printf("希尔排序后:\n"); for(i=0; i<10; i++) printf("%8d",a[i]); printf("\n"); } /*堆排序*/ #include "stdio.h" #include "string.h" void HeapAdjust(int R[],int s,int m) { int t,j; t=R[s]; for(j=2*s+1; j<=m; j=j*2+1) { if(j<m&&R[j]<R[j+1]) ++j; if(!(t<R[j])) break; R[s]=R[j]; s=j; } R[s]=t; } void HeapSort(int R[],int n) { int i,temp; for(i=n/2-1; i>=0; --i) //建立初始堆,初始堆的时候要依次调整第n/2-1到第0个节点。 HeapAdjust(R,i,n-1); for(i=n-1; i>0; i--) { temp=R[0]; R[0]=R[i]; R[i]=temp; HeapAdjust(R,0,i-1);//重建堆的时候只要调整第0个节点就可以了。 } } void main() { int i; int R[]= {3,12,4,1,9,8,6,5,23,9,90,87,65,456,2,95}; HeapSort(R,16); for(i=0; i<16; i++) printf("%d\t",R[i]); }
  • 相关阅读:
    With在sql server 2005中的用法
    相互关联子查询在项目中的用法
    存储过程中@@Identity全局变量
    sql server 2008 5120错误
    如何启用 FILESTREAM
    表变量在存储过程或sql server中的运用
    Union ,Union ALL的用法
    数据移植(利用Ado.Net功能实现)
    Foreign Key ,NO action,Cascade的用法
    EXISTS 在SQL 中的用法
  • 原文地址:https://www.cnblogs.com/zhanjindong/p/2834718.html
Copyright © 2011-2022 走看看