You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
905 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<bits/stdc++.h>
using namespace std;
//定义一个获取数组长度的宏
#define length(arr) ((sizeof(arr)) / (sizeof(arr[0])))
/*
功能:输出一组数组
作者:黄海
注意:数组作为函数参数时,传递的是指向首地址的指针。
对调用函数而言数组参数的大小是不可用的。而指针是4字节。。
*/
void print(int array[],int size) {
for(int i = 0; i < size; i++)
printf("%d ", array[i] );
printf("\n");
}
//降序
bool cmpDesc(int a,int b) {
return a>b;//降序排列
}
//升序
bool cmpAsc(int a,int b) {
return a<b;//升序排列
}
int main() {
//原始数组
int arr[]= {1, 9, 12, 29, 0, 31, 8, 10};
//数组长度
int size=length(arr);
//输出原始数组
print(arr,size);
//快速排序(降序)
sort(arr, arr + size,cmpDesc); //arr的地址开始到arr+size的地址结束
//输出新数组
print(arr,size);
//快速排序(升序)
sort(arr, arr + size,cmpAsc); //arr的地址开始到arr+size的地址结束
//输出新数组
print(arr,size);
return 0;
}