Files
python/C++专题课程/排序/C++快速排序调用.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

51 lines
955 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}