Задача: Сравнение алгоритмов сортировки массива
Исходник: Sort.cpp, язык: C++ [code #36, hits: 13381]
автор: this [добавлен: 17.02.2006]
  1. #include "Sort.h"
  2.  
  3. Sort::Sort()
  4. {
  5. this->setSize(0);
  6. this->swapNum = 0;
  7. this->memEval = 0;
  8. }
  9.  
  10. Sort::Sort(int n, int* x)
  11. {
  12. this->setArr(x);
  13. this->setSize(n);
  14. this->swapNum = 0;
  15. this->memEval = 0;
  16. this->countSwaps = true;
  17. this->algName = "Default Sort";
  18. }
  19. void Sort::Run(void) {
  20.  
  21. }
  22.  
  23. int* Sort::getArr(void) {
  24. return this->x;
  25. }
  26.  
  27. void Sort::setArr(int* x) {
  28. this->swapNum = 0;
  29. this->x = x;
  30. }
  31.  
  32. int Sort::getSize(void) {
  33. return this->n;
  34. }
  35.  
  36. void Sort::setSize(int n) {
  37. this->n = n;
  38. }
  39.  
  40. char* Sort::getName(void) {
  41. return this->algName;
  42. }
  43.  
  44. int Sort::getSwapNum(void) {
  45. return this->swapNum;
  46. }
  47.  
  48. void Sort::DisableSwapCount() {
  49. this->countSwaps = false;
  50. }
  51.  
  52. void Sort::CountSwap() {
  53. if (this->countSwaps) {
  54. this->swapNum++;
  55. }
  56. }
  57.  
  58. void Sort::Print(void) {
  59. for (int i = 0; i < this->n; i++) {
  60. cout << this->x[i] << ' ';
  61. }
  62. cout << endl;
  63. }
  64.  
  65. void Sort::RandomFill(int left = 0, int right = 100) {
  66. srand( (unsigned)time(NULL));
  67.  
  68. for (int i = 0; i < this->n; i++) {
  69. this->x[i] = left + rand() % (right - left);
  70. }
  71. }
  72.  
  73. void Sort::Swap(int i, int j) {
  74. int tmp;
  75. tmp = this->x[i]; this->x[i] = this->x[j]; this->x[j] = tmp;
  76. this->CountSwap();
  77. }
  78.  
  79. void Sort::CopyTo(int* dest) {
  80. for (int i = 0; i < this->n; i++) {
  81. dest[i] = this->x[i];
  82. }
  83. }
  84.  
  85. void Sort::Print2(int* arr, int n) {
  86. for (int i = 0; i < n; i++) {
  87. cout << arr[i] << ' ';
  88. }
  89. cout << endl;
  90. }
  91.  
  92. Sort::~Sort(void)
  93. {
  94. delete [] x;
  95. }
  96.  
Реализация базового абстрактного класса сортировки.

Заголовочный файл: Sort.h
Тестировалось на: MS Visual Studio 2005, .NET Framework 2.0

+добавить реализацию