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;
}
默默无闻的菜鸟小白。。。