编一个程序,用同一个函数名对n个数据进行从小到大排序,数据类型可以是整型,单精度型、双精度型。

1.用重载函数实现。

这里用了快速排序法。

#include <iostream>
using namespace std;

void swap(int* a, int* b);
void sort1(int* array, int n);
void sort2(int* array, int left, int right);
void swap(float* a, float* b);
void swap(double* a, double* b);
void sort1(float* array, int n);
void sort1(double* array, int n);
void sort2(float* array, int left, int right);
void sort2(double* array, int left, int right);

int main(void) {
    int  n;
    cin >> n;
    cout << "输入n个数:n=" << n << endl;
    //  1代表整数, 2代表单精度浮点型float   , 3代表双精度浮点型double
    int co;
    cout << "类型为co=" ;
    cin >> co;
    if (co == 1) {
        cout << ",整数" << endl;
        int x;
        int* num = new int[n];
        for (int i = 0; i < n; i++) {
            cin >> x;
            num[i] = x;
        }
        sort1(num, n);
        for (int i = 0; i < n; i++) {
            cout << num[i] << "  ";
        }
        delete[] num;
    }

    if (co == 2) {
        cout << ",单精度浮点型" << endl;
        float x;
        float* num = new float[n];
        for (int i = 0; i < n; i++) {
            cin >> x;
            num[i] = x;
        }
        sort1(num, n);
        for (int i = 0; i < n; i++) {
            cout << num[i] << "  ";
        }
        delete[] num;
    }

    if (co == 3) {
        cout << ",双精度浮点型" << endl;
        double x;
        double* num = new double[n];
        for (int i = 0; i < n; i++) {
            cin >> x;
            num[i] = x;
        }
        sort1(num, n);
        for (int i = 0; i < n; i++) {
            cout << num[i] << "  ";
        }
        delete[] num;
    }

    return 0;

}


void swap(int* a, int* b) {
    int temp;
    temp = *b;
    *b = *a;
    *a = temp;
}

void swap(float* a, float* b) {
    float temp;
    temp = *b;
    *b = *a;
    *a = temp;
}

void swap(double* a, double* b) {
    double temp;
    temp = *b;
    *b = *a;
    *a = temp;
}

void sort1(int* array, int n) {
    sort2(array, 0, n - 1);
}

void sort1(float* array, int n) {
    sort2(array, 0, n - 1);
}

void sort1(double* array, int n) {
    sort2(array, 0, n - 1);
}
void sort2(int* array, int left, int right) {
    int left_index = left;
    int right_index = right;
    int pivot = array[(left + right) / 2];

    while (left_index <= right_index) {
        for (; array[left_index] < pivot; left_index++);
        for (; array[right_index] > pivot; right_index--);
        if (left_index <= right_index) {
            swap(&array[left_index], &array[right_index]);
            left_index++;
            right_index--;
        }
    }

    
    if (right_index > left) {
        sort2(array, left, right_index);
    }
    if (left_index < right) {
        sort2(array, left_index, right);
    }
}

void sort2(float* array, int left, int right) {
    int left_index = left;
    int right_index = right;
    float pivot = array[(left + right) / 2];

    while (left_index <= right_index) {
        for (; array[left_index] < pivot; left_index++);
        for (; array[right_index] > pivot; right_index--);
        if (left_index <= right_index) {
            swap(&array[left_index], &array[right_index]);
            left_index++;
            right_index--;
        }
    }

    if (right_index > left) {
        sort2(array, left, right_index);
    }
    if (left_index < right) {
        sort2(array, left_index, right);
    }
}

void sort2(double* array, int left, int right) {
    int left_index = left;
    int right_index = right;
    double pivot = array[(left + right) / 2];

    while (left_index <= right_index) {
        for (; array[left_index] < pivot; left_index++);
        for (; array[right_index] > pivot; right_index--);
        if (left_index <= right_index) {
            swap(&array[left_index], &array[right_index]);
            left_index++;
            right_index--;
        }
    }

    if (right_index > left) {
        sort2(array, left, right_index);
    }
    if (left_index < right) {
        sort2(array, left_index, right);
    }
}

2.用函数模版

#include <iostream>
using namespace std;

template <class T>
void swap(T* a, T* b) {
    T temp;
    temp = *b;
    *b = *a;
    *a = temp;
}
template <class T>
void sort2(T* array, int left, int right) {
    int left_index = left;
    int right_index = right;
    T pivot = array[(left + right) / 2];

    while (left_index <= right_index) {
        for (; array[left_index] < pivot; left_index++);
        for (; array[right_index] > pivot; right_index--);
        if (left_index <= right_index) {
            swap(&array[left_index], &array[right_index]);
            left_index++;
            right_index--;
        }
    }

    if (right_index > left) {
        sort2(array, left, right_index);
    }
    if (left_index < right) {
        sort2(array, left_index, right);
    }
}
template <class T>
void sort1(T* array, int n) {
    sort2(array, 0, n - 1);
}



int main(void) {
    int  n;
    cin >> n;
    cout << "输入n个数:n=" << n << endl;
    //  1代表整数, 2代表单精度浮点型float   , 3代表双精度浮点型double
    int co;
    cout << "类型为co=";
    cin >> co;

    if (co == 1) {
        cout << ",整数" << endl;
        int x;
        int* num = new int[n];
        for (int i = 0; i < n; i++) {
            cin >> x;
            num[i] = x;
        }
        sort1(num, n);
        for (int i = 0; i < n; i++) {
            cout << num[i] << "  ";
        }
        delete[] num;
    }

    if (co == 2) {
        cout << ",单精度浮点型" << endl;
        float x;
        float* num = new float[n];
        for (int i = 0; i < n; i++) {
            cin >> x;
            num[i] = x;
        }
        sort1(num, n);
        for (int i = 0; i < n; i++) {
            cout << num[i] << "  ";
        }
        delete[] num;
    }

    if (co == 3) {
        cout << ",双精度浮点型" << endl;
        double x;
        double* num = new double[n];
        for (int i = 0; i < n; i++) {
            cin >> x;
            num[i] = x;
        }
        sort1(num, n);
        for (int i = 0; i < n; i++) {
            cout << num[i] << "  ";
        }
        delete[] num;
    }

    return 0;
}

默默无闻的菜鸟小白。。。

  • 10
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值