sort
自定义排序
这里使用 STL 中的 sort 函数, sort 函数的简单使用, 默认是从小到大进行排序
sort 函数可以传入第三个参数
#include <stdio.h>
#include <algorithm>
using namespace std;
int dat[110], n;
bool cmp(int a,int b){
//这里相当于判断 a “小于” b
// return a < b; //如果这样, 就是正常的从小到大排序
return a > b; //这样就是大的在前面,小的在后面
}
int main(){
n = 5;
for(int i = 0;i < n;i++) dat[i] = i;
sort(dat, dat + n); // 排序 dat[0...n-1], 左闭右开的一个区间
//dat : 0, 1, 2, 3, 4
sort(dat, dat + n, cmp);
//dat : 4, 3, 2, 1, 0
}
对结构体排序
struct people{
char name[MAX_NAME_LEN];
int age;
int money;
}dat[MAXN];
bool cmp(people a, people b){
if(a.money != b.money){
return a.money > b.money; //钱多的在前面
}
if(a.age != b.age){
return a.age < b.age; //年轻的在前面
}
return strcmp(a.name, b.name) < 0; //名字字典序小的在前面
}
int main(){
...
sort(dat + 1,dat + 1 + n, cmp);
}
编程题最后一题参考:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const int MAX_NAME_LEN = 10;
struct people{
char name[MAX_NAME_LEN];
int age;
int money;
}dat[MAXN];
bool cmp(people a, people b){
if(a.money != b.money){
return a.money > b.money; //钱多的在前面
}
if(a.age != b.age){
return a.age < b.age; //年轻的在前面
}
return strcmp(a.name, b.name) < 0; //名字字典序小的在前面
}
int n, q, m, l, r;
int main(){
scanf("%d%d",&n, &q);
for(int i = 1;i <= n;i++){
scanf("%s%d%d",&dat[i].name, &dat[i].age, &dat[i].money);
}
sort(dat + 1,dat + 1 + n, cmp);
int caseNum = 0;
while(q--){
scanf("%d%d%d",&m, &l, &r);
printf("Case #%d:\n", ++caseNum);
int raw_m = m;
for(int i = 1;i <= n && m > 0;i++){
if(dat[i].age >= l && dat[i].age <= r){
m--;
printf("%s %d %d\n", dat[i].name, dat[i].age, dat[i].money);
}
}
if(m == raw_m) puts("None");
}
}
各种排序代码
这是一份测试代码, 有时间可以自己补全空着的函数
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int dat[N], n, rdat[N];
clock_t st, ed;
// 调用: swap(&dat[i], &dat[j]);
void swap(int* a,int *b){
int tmp = *a;
*a = *b;
*b = tmp;
}
//也可以写不同的接口, 调用 swap(dat, i, j);
void swap(int* arr, int i, int j){
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
// 判断是否排好序
bool is_sorted(){
for(int i = 2;i <= n;i++){
if(dat[i] < dat[i - 1]) return false;
}
return true;
}
void randomDataCopy(){
for(int i = 1;i <= n;i++)
dat[i] = rdat[i];
}
void test(const char* funcName, void(*sort)()){
randomDataCopy(); //复制一份相同的数据
st = clock();
sort(); //传进来的排序函数
ed = clock();
double ms = 1000.0*(double)(ed-st)/CLOCKS_PER_SEC;
//计时
if(is_sorted() == true){
printf("%s success, cost {%.2f ms}\n", funcName, ms);
}else{
printf("%s fail!\n", funcName);
}
}
void bubbleSort(){
//fill something to sort dat[1...n]
}
void insertSort(){
}
void selectSort(){
}
//------quick sort------
int partition(int l,int r){
}
void quicksort(int l, int r){
}
void quickSort(){
quicksort(1, n);
}
//ed
//-------merge sort
int tmp[N]; //辅助数组
void mergesort(int l,int r){
}
void mergeSort(){
mergesort(1, n);
}
// ed
//----- heap sort
int heap[N], sz; //[1,sz]
void down(int idx){
}
void up(int idx){
}
void heapInsert(int value){
}
int heapTopAndPop(){
}
void heapSort(){
// for(int i = 1;i <= n;i++){
// heapInsert(dat[i]);
// }
// for(int i = n;i >= 1;i--){
// dat[i] = heapTopAndPop();
// }
}
// ed
int main(){
//生成随机数据, 每次 test 时 copy 到 dat 中, 保证排序的数据相同
n = 3e4;
srand(time(NULL));
for(int i = 1;i <= n;i++) rdat[i] = rand();
//需要补全的是下面这些 test 函数里的第二个函数参数
test("insertSort", insertSort);
test("bubbleSort", bubbleSort);
test("selectSort", selectSort);
test("quickSort", quickSort);
test("mergeSort", mergeSort);
test("heapSort", heapSort);
}
这里是我写的, 可以稍作参考
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int dat[N], n, rdat[N];
clock_t st, ed;
void swap(int* a,int *b){
int tmp = *a;
*a = *b;
*b = tmp;
}
bool is_sorted(){
for(int i = 2;i <= n;i++){
if(dat[i] < dat[i - 1]) return false;
}
return true;
}
void randomDataCopy(){
for(int i = 1;i <= n;i++)
dat[i] = rdat[i];
}
void test(const char* funcName, void(*sort)()){
randomDataCopy();
st = clock();
sort();
ed = clock();
double ms = 1000.0*(double)(ed-st)/CLOCKS_PER_SEC;
if(is_sorted() == true){
printf("%s success, cost {%.2f ms}\n", funcName, ms);
}else{
printf("%s fail!\n", funcName);
}
}
void bubbleSort(){
for(int i = n;i >= 1;i--){
for(int j = 1;j <= i - 1;j++){
if(dat[j] > dat[j + 1]){
swap(&dat[j], &dat[j + 1]);
}
}
}
}
void insertSort(){
int j;
for(int i = 2;i <= n;i++){
int nowValue = dat[i];
for(j = i - 1;j >= 1 && dat[j] > nowValue;j--){
swap(&dat[j], &dat[j+1]);
}
dat[++j] = nowValue;
}
}
void selectSort(){
int mx = INT_MAX;
for(int i = 1; i <= n;i++){
int* min = &mx;
for(int j = i;j <= n;j++){
if(dat[j] < *min){
min = &dat[j];
}
}
swap(min, &dat[i]);
}
}
//------quick sort------
int partition(int l,int r){
//dat[r] 做 pivot
int st = l;
int pivot = dat[r];
for(int i = l;i <= r;i++){
if(dat[i] <= pivot){
swap(&dat[i], &dat[st++]);
}
}
return st - 1;
}
void quicksort(int l, int r){
if(l <= r){
int idx = partition(l, r);
quicksort(l, idx - 1);
quicksort(idx + 1, r);
}
}
void quickSort(){
quicksort(1, n);
}
//ed
//-------merge sort
int tmp[N]; //辅助数组
void mergesort(int l,int r){
if(l == r) return;
int mid = (l + r) / 2;
mergesort(l, mid); mergesort(mid + 1, r);
//合并两个有序列表
int i = l, j = mid + 1;
int st = 0;
while(i <= mid && j <= r){
if(dat[i] <= dat[j]){
tmp[st++] = dat[i++];
}else{
tmp[st++] = dat[j++];
}
}
while(i <= mid) tmp[st++] = dat[i++];
while(j <= r) tmp[st++] = dat[j++];
for(int k = 0;k < st;k++){
dat[l + k] = tmp[k];
}
}
void mergeSort(){
mergesort(1, n);
}
// ed
//----- heap sort
int heap[N], sz; //[1,sz]
void down(int idx){
int lv = 2 * idx <= sz ? heap[2 * idx] : INT_MIN;
int rv = (2 * idx + 1) <= sz ? heap[2 * idx + 1] : INT_MIN;
if(heap[idx] >= lv && heap[idx] >= rv)
return;
if(lv > rv){
swap(&heap[idx], &heap[idx * 2]);
idx = 2 * idx;
}else{
swap(&heap[idx], &heap[idx * 2 + 1]);
idx = 2 * idx + 1;
}
down(idx);
}
void up(int idx){
while(idx != 1 && heap[idx] > heap[idx/2]){
swap(&heap[idx], &heap[idx/2]);
idx /= 2;
}
}
void heapInsert(int value){
heap[++sz] = value;
up(sz);
}
int heapTopAndPop(){
int top = heap[1];
swap(&heap[1], &heap[sz--]);
down(1);
return top;
}
void heapSort(){
for(int i = 1;i <= n;i++){
heapInsert(dat[i]);
}
for(int i = n;i >= 1;i--){
dat[i] = heapTopAndPop();
}
}
// ed
int main(){
//生成随机数据, 每次 test 时 copy 到 dat 中, 保证排序的数据相同
n = 3e4;
srand(time(NULL));
for(int i = 1;i <= n;i++) rdat[i] = rand();
test("insertSort", insertSort);
test("bubbleSort", bubbleSort);
test("selectSort", selectSort);
test("quickSort", quickSort);
test("mergeSort", mergeSort);
test("heapSort", heapSort);
}

浙公网安备 33010602011771号