zoukankan      html  css  js  c++  java
  • quick sort, 第一次试着用templete变小程序

    代码
    #include <stdio.h>
    #include 
    <stdlib.h>
    #ifdef _DEBUG
    #define DEBUG_NEW new (_NORMAL_BLOCK, THIS_FILE, __LINE__)
    #endif

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif

    #ifdef _DEBUG
    #ifndef ASSERT
    #define ASSERT  assert
    #endif
    #else   // not _DEBUG
    #ifndef ASSERT
    #define ASSERT
    #endif
    #endif  // _DEBUG

    template
    <typename T>
    int _partition(T *A, int p, int r)
    {
        T x 
    = A[r];
        
    int i = p - 1;
        
    for (int j = p; j <= r - 1++j)
        {
            
    if (A[j] <= x)
            {
                i
    ++;
                T temp 
    = A[j];
                A[j] 
    = A[i];
                A[i] 
    = temp;
            }
        }
        T temp 
    = A[i + 1];
        A[i 
    + 1= A[r];
        A[r] 
    = temp;
        
    return i+1;
    }



    template
    <typename T>
    void quick_sort(T *A, int p, int r)
    {

        
    if (p < r)
        {
            
    int q = _partition(A, p, r);
            quick_sort(A, p, q 
    - 1);
            quick_sort(A, q 
    + 1, r);
        }
    }

    int main()
    {
        
        
    int  i;
        
    int N = 20;
        
    double *= (double *)malloc(sizeof(double* N);
        
    for (i = 0; i < N; i ++)
        {
            A[i] 
    = rand() % 100 + 0.25;
            printf(
    "%4f\t", A[i]);
        }
        quick_sort(A, 
    0, N - 1);
        
        printf(
    "\n--------------\n");
        
        
    for (i = 0; i != N; i++)
        {
            printf(
    "%4f\t", A[i]);
        }
        printf(
    "\n");
        free(A);A 
    = NULL;
    }
  • 相关阅读:
    案例7-1.2 插入排序还是归并排序 (25分)
    自动化运维工具——puppet详解(一)
    centos6.8的安装和配置
    ZooKeeper内部原理
    ZooKeeper安装和配置
    zookeeper入门
    shell中uniq与sort -u 两种去重的对别
    tomcat日志文件 访问IP统计
    Mysql常用命令
    linux一键安装php脚本
  • 原文地址:https://www.cnblogs.com/luweiseu/p/1645863.html
Copyright © 2011-2022 走看看