#ifndef __STRATEGY_H__ #define __STRATEGY_H__ #include // 抽象策略类 class Strategy { public: Strategy(){} virtual ~Strategy(){} virtual void sort(int arr[], int N) = 0; }; // 具体策略:冒泡排序 class BubbleSort :public Strategy { public: BubbleSort(){ printf("冒泡排序\n"); } void sort(int arr[], int N){ for (int i = 0; iarr[j + 1]){ int tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } } }; // 具体策略:选择排序 class SelectionSort :public Strategy { public: SelectionSort(){ printf("选择排序\n"); } void sort(int arr[], int N){ int i, j, k; for (i = 0; i= 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