zoukankan      html  css  js  c++  java
  • 桶排序和直接排序的算法和数据结构

    技术支援:E:\資料\計算機\编程语言\数据结构

    E:\資料\計算機\编程语言\数据结构3和计算机知识\pdf

    E:\資料\計算機\编程语言\数据结构3和计算机知识\html

    E:\資料\計算機\编程语言\数据结构算法-Visual C++ 6.0程序集

    http://www.is.pku.edu.cn/~qzy/ds/docs/code/

    1 桶排序

    首先还是一个架构问题:

    image

    image

    image

    这样结构就可以编译:

    --------------------Configuration: compositor - Win32 Debug--------------------
    Compiling...
    compositorm.cpp

    compositorm.obj - 0 error(s), 0 warning(s)

    error C2601: 'zeroBucket' : local function definitions are illegal

    void collectElements(int[],int[][SIZE],int)
    {}

    int  numberOfDigits(int b[],int arraySize)
    {
         int largest=b[0],digits=0;
         for (int i=1;i<arraySize;++i)
         if(b[i]>largest)
             largest=b[i];
         while(largest!=0)
         {
             ++digits;
             largest/=10;

    } //就是在这个符号上少了导致的,今后注意
    return digits;

    }

    void zeroBucket(int buckets[] [SIZE])
    {   
        for  (int i=0;i<10;++i)
            for ( int j=0;j<SIZE;++j)
                buckets[i][j]=0;

    }

    int  numberOfDigits(int b[],int arraySize)//
    {
         int largest=b[0],digits=0;
        for (int i=1;i<arraySize;++i)//既然没有赋值运算,所以++i和i++是一样,在这里
         if(b[i]>largest)
             largest=b[i];
       while(largest!=0)
         {
             ++digits;
             largest/=10;//
         }
    return digits;

    }

    这个子函数的一些解析:

    因为for没有用{ } 把while裹进去,所以,这两个是串行运算的.

    largest就是最大值.

    所以在while中 (largest!=0)

    这个判决明显是一个数轴分界线,

    对这个程式测试:

    #include "stdio.h"
    #define len 12
    void main()
    {   int  largest;
        int digits;
        int i;
        for(  i=0;i<len;i++)
        {
            scanf("%d%d",&largest,&digits);
         while(largest!=0)
         {
             ++digits;
             largest/=10;//  /= 除法赋值符
             printf(" %d,%d\n" ,digits,largest);

         }
        }
    }

    数据如下:

    image

    largest/=10;就是 等价于 largest=largest/10;

    复合赋值运算符(combination assignment operator):+=、-=、*=、/=、%=。下面我们通过一些简单的例子学习这些复合赋值运算符的用法:

        income += 300; 等同于 income = income + 300;
        income -= 200; 等同于 income = income - 200;
        income *= 0.1; 等同于 income = income * 0.1;
        income /= 1.1; 等同于 income = income / 1.1;
        reduce %= 3;   等同于 reduce = reduce % 3;

        income += 200 + 100;    等同于 income = income + (200 + 100);
        income *= 1 - 0.9;      等同于 income = income * (1 - 0.9);
        income /= 2 * 1 - 0.9;  等同于 income = income / (2 * 1 - 0.9);

    这样是出于编译原理的一些规范,简化代码.

    所以,道理是一个样的.

    所以这个程式段,目标是很明确是,那就是确定一个数的位数.

    void collectElements(int a[],int buckets[][SIZE])
    {
        int subscript=0;
        for(int i=0;i<10;++i)
            for(int j=1;j<=buckets[i][0];++j)
            {    a[subscript++]=buckets[i][j];}

    }

    bucket[0][0]                      
    bucket[1][0]                      
    bucket[2][0]                      
    bucket[3][0]                      
    bucket[4][0]                      
    bucket[5][0]                      
    bucket[6][0]                      
    bucket[7][0]                      
    bucket[8][0]                      
    bucket[9][0]                      

    测试代码:

    /* Note:Your choice is C IDE */
    #include "stdio.h"
    void main()
    {    int i,j;
         int digit;
         int divisor;
         for (j=0;j<10;j++)
        {
            scanf("%d",&digit);
            divisor=10;
           for (i=0;i<digit;++i)
           {
             divisor*=10;
             printf("%d\n",divisor);
           }
        }
    }

    image

    /* Note:Your choice is C IDE */
    #include "stdio.h"
    void main()
    {
        int divisor;
        int a;
        int bucketNumber;
        int i;
        for(i=0;i<13;i++)
        {scanf("%d%d",&a,&divisor);
        bucketNumber=( a%divisor-a%(divisor/10))/(divisor/10);
        printf("%d\n",bucketNumber);
        }
    }

    image

    image

    插入排序法和希尔排序法

    先看自己编写结构代码一些问题:

    #include<iostream.h>
    #include<iomanip.h>
    #include<stdlib.h>
    //#define MAXI 11
    typedef int KeyType;
    typedef int ElemType;
    typedef struct{
        KeyType key;
        ElemType data;
    }RecordNode;
    typedef struct{
        int n;
        RecorNode *record;
    }SortObject;

    void insertsort (SortObject *pvector)
    {
    }
    void main()
    {

    }

    Compiling...
    insertsort.cpp
    D:\software\C_installer\王贵歆个人工作文档\算法和数据结构\排序\insertsort1\insertsort.cpp(13) : error C2143: syntax error : missing ';' before '*'
    D:\software\C_installer\王贵歆个人工作文档\算法和数据结构\排序\insertsort1\insertsort.cpp(13) : error C2501: 'RecorNode' : missing storage-class or type specifiers
    D:\software\C_installer\王贵歆个人工作文档\算法和数据结构\排序\insertsort1\insertsort.cpp(13) : error C2501: 'record' : missing storage-class or type specifiers 就是一个简单的写错,就造成这样的错误
    Error executing cl.exe.

    insertsort.obj - 3 error(s), 0 warning(s)

    #include<iostream.h>
    #include<iomanip.h>
    #include<stdlib.h>
    //#define MAXI 11
    typedef int KeyType;
    typedef int ElemType;
    typedef struct{
        KeyType key;
        ElemType data;
    }RecordNode;
    typedef struct{
        int n;
        RecordNode *record;
    }SortObject;

    void insertsort (SortObject *pvector)
    {
    }
    void main()
    {

    }

    --------------------Configuration: insertsort1 - Win32 Debug--------------------
    Compiling...
    insertsort.cpp
    D:\software\C_installer\王贵歆个人工作文档\算法和数据结构\排序\insertsort1\insertsort.cpp(20) : error C2182: 'main' : illegal use of type 'void'
    D:\software\C_installer\王贵歆个人工作文档\算法和数据结构\排序\insertsort1\insertsort.cpp(20) : error C2239: unexpected token '{' following declaration of 'main'

    就是这样的一个( )缺省了,就造成这样错误.


    Error executing cl.exe.

    insertsort.obj - 2 error(s), 0 warning(s)

    以上两个错误今后应该避免

    完整代码:

    #include<iostream.h>
    #include<iomanip.h>
    #include<stdlib.h>
    //#define MAXI 11
    typedef int KeyType;
    typedef int ElemType;
    typedef struct{
        KeyType key;
        ElemType data;
    }RecordNode;
    typedef struct{
        int n;
        RecordNode *record;
    }SortObject;

    void insertsort (SortObject *pvector)
    {
    }
    void main()
    {

    }

    /* 直接插入排序的算法源程序*/

    #include<stdio.h>

    #define MAXNUM 100

    typedef int KeyType;
    typedef int DataType;
    typedef struct {
        KeyType key;       /* 排序码字段 */
        /*DataType info;           记录的其它字段 */
    } RecordNode;

    typedef struct {
        int n;               /* n为文件中的记录个数,n<MAXNUM */
        RecordNode record[MAXNUM];
    } SortObject;

    void insertSort(SortObject * pvector) { /* 按递增序进行直接插入排序 */
        int i, j;
        RecordNode temp;
        RecordNode *data = pvector->record;

        for( i = 1; i < pvector->n; i++ ) { /* 依次插入记录R1, R2…Rn-1 */
            temp = data[i];
            for ( j = i-1; temp.key < data[j].key && j >= 0; j-- )
                /* 由后向前找插入位置 将排序码大于ki的记录后移 */
                data[j+1] = data[j];
            if( j != i-1 ) data[j+1] = temp;
        }
    }

    SortObject vector = {10,
        49, 38, 65, 97, 76, 13, 27, 49, 50, 101};

    int main(){
        int i;
        insertSort(&vector);
        for(i = 0; i < vector.n; i++)
            printf("%d ", vector.record[i]);
        getchar();
        return 0;
    }

    自己的一些错误:

    int n=MAXI;
        int a[n];//error C2057: expected constant expression

    改为 int a[MAXI]

    要用到 srand(time(0));必须用到 #include<time.h>

    到最后 报warning:

    warning C4700: local variable 'a' used without having been initialized

  • 相关阅读:
    【转】html的meta总结
    style.left 与offsetLeft之间的区别
    VM886
    js 获取数据类型
    JS序列化URL方法
    userData 本地存储
    第一章 1.6 HTML5 的新功能(二)
    第一章 1.6 HTML5 的新功能(一)
    第一章 1.5 无插件范式
    第一章 1.4 新的认识
  • 原文地址:https://www.cnblogs.com/fleetwgx/p/1450338.html
Copyright © 2011-2022 走看看