DesignPattern/22.StrategyPattern/2.Code/Strategy.h

88 lines
1.3 KiB
C
Raw Normal View History

2019-11-09 13:25:30 +00:00
#ifndef __STRATEGY_H__
#define __STRATEGY_H__
#include <stdio.h>
2021-10-28 15:15:51 +00:00
// 抽象策略类
2019-11-09 13:25:30 +00:00
class Strategy
{
public:
Strategy(){}
virtual ~Strategy(){}
2019-11-09 13:25:30 +00:00
virtual void sort(int arr[], int N) = 0;
};
2021-10-28 15:15:51 +00:00
// 具体策略:冒泡排序
2019-11-09 13:25:30 +00:00
class BubbleSort :public Strategy
{
public:
BubbleSort(){
2021-10-28 15:15:51 +00:00
printf("冒泡排序\n");
2019-11-09 13:25:30 +00:00
}
void sort(int arr[], int N){
for (int i = 0; i<N; i++)
{
for (int j = 0; j<N - i - 1; j++)
{
if (arr[j]>arr[j + 1]){
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
};
2021-10-28 15:15:51 +00:00
// 具体策略:选择排序
2019-11-09 13:25:30 +00:00
class SelectionSort :public Strategy
{
public:
SelectionSort(){
2021-10-28 15:15:51 +00:00
printf("选择排序\n");
2019-11-09 13:25:30 +00:00
}
void sort(int arr[], int N){
int i, j, k;
for (i = 0; i<N; i++)
{
k = i;
for (j = i + 1; j<N; j++)
{
if (arr[j] < arr[k]){
k = j;
}
}
int temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
};
2021-10-28 15:15:51 +00:00
// 具体策略:插入排序
2019-11-09 13:25:30 +00:00
class InsertSort :public Strategy
{
public:
InsertSort(){
2021-10-28 15:15:51 +00:00
printf("插入排序\n");
2019-11-09 13:25:30 +00:00
}
void sort(int arr[], int N){
int i, j;
for (i = 1; i<N; i++)
{
for (j = i - 1; j >= 0; j--)
{
if (arr[i]>arr[j]){
break;
}
}
int temp = arr[i];
for (int k = i - 1; k > j; k--){
arr[k + 1] = arr[k];
}
arr[j + 1] = temp;
}
}
};
#endif