バブルソートとクイックソートの走査カウントと入れ替えカウントをグラフ化するためのプログラムです。 横軸は、ソートする配列の要素数。(MAX = MAX_NUM)
縦軸は、ソートにかかる操作回数(時間) です。複数回試行して平均をとるようにしています。 試行回数(=TEST_COUNT)を大きくとるとなめらかなグラフが出ます。

プロジェクトのダウンロード

[C++コード]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

#pragma comment(lib, "gdiplus")
#include <windows.h>
#include <gdiplus.h>
#include <array>
#include <random>

#define MAX_NUM 200
#define TEST_COUNT 200

using namespace Gdiplus;

TCHAR szClassName[] = TEXT("Window");

void BubleSort(int* data, int size, int* pScanningCount, int* pExchangeCount)
{
  BOOL bFlag;
  int k = 0;
  do {
    bFlag = FALSE;
    for (int i = 0; i < size - 1 - k; ++i) {
      if (data[i] > data[i + 1]) {
        bFlag = TRUE;
        const int nTmp = data[i];
        data[i] = data[i + 1];
        data[i + 1] = nTmp;
        ++*pExchangeCount;
      }
      ++*pScanningCount;
    }
    ++k;
  } while (bFlag);
}

void QuickSort(int start, int end, int *data, int* pScanningCount, int* pExchangeCount)
{
  int lower, upper, div, temp;
  if (start >= end) {
    return;
  }
  div = data[start];
  for (lower = start, upper = end; lower < upper;) {
    while (lower <= upper && data[lower] <= div) {
      ++lower;
      ++*pScanningCount;
    }
    while (lower <= upper&&data[upper] > div) {
      --upper;
      ++*pScanningCount;
    }
    if (lower < upper) {
      temp = data[lower];
      data[lower] = data[upper];
      data[upper] = temp;
      ++*pExchangeCount;
    }
  }
  temp = data[start];
  data[start] = data[upper];
  data[upper] = temp;
  QuickSort(start, upper - 1, data, pScanningCount, pExchangeCount);
  QuickSort(upper + 1, end, data, pScanningCount, pExchangeCount);
}

void InitList(int* pList, int nSize)
{
  std::array<std::seed_seq::result_type, std::mt19937::state_size> seed_data;
  std::random_device rd;
  std::generate_n(seed_data.data(), seed_data.size(), std::ref(rd));
  std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
  std::mt19937 engine(seq);
  for (int i = 0; i < nSize; ++i) {
    pList[i] = engine();
  }
}

void Shuffle(int* pList, int nSize)
{
  std::array<std::seed_seq::result_type, std::mt19937::state_size> seed_data;
  std::random_device rd;
  std::generate_n(seed_data.data(), seed_data.size(), std::ref(rd));
  std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
  std::mt19937 engine(seq);
  for (int i = 0; i < nSize; ++i) {
    const int j = engine() % nSize;
    if (i != j) {
      int temp = pList[i];
      pList[i] = pList[j];
      pList[j] = temp;
    }
  }
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  static HWND hButton;
  static double averageBubleSort1[MAX_NUM];
  static double averageBubleSort2[MAX_NUM];
  static double averageQuickSort1[MAX_NUM];
  static double averageQuickSort2[MAX_NUM];
  switch (msg)
  {
  case WM_CREATE:
    hButton = CreateWindow(TEXT("BUTTON"), TEXT("計測"), WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hWnd, (HMENU)IDOK, ((LPCREATESTRUCT)lParam)->hInstance, 0);
    break;
  case WM_SIZE:
    MoveWindow(hButton, 10, 10, 256, 32, TRUE);
    break;
  case WM_PAINT:
    {
      PAINTSTRUCT ps;
      HDC hdc = BeginPaint(hWnd, &ps);
      {
        RECT rect;
        GetClientRect(hWnd, &rect);
        Graphics g(hdc);
        g.SetSmoothingMode(SmoothingModeAntiAlias);
        double dmax = 0.0;
        for (int i = 0; i < MAX_NUM; ++i) {
          dmax = max(averageBubleSort1[i], dmax);
        }
        for (int i = 0; i < MAX_NUM; ++i) {
          const int x = i * rect.right / MAX_NUM;
          g.DrawLine(&Pen(Color(248 + 3, 183 + 3, 84 + 3), 10.0f), Point(x, rect.bottom), Point(x, (INT)(rect.bottom - averageBubleSort1[i] * rect.bottom / dmax)));
          g.DrawLine(&Pen(Color(248 - 3, 183 - 3, 84 - 3), 10.0f), Point(x, rect.bottom), Point(x, (INT)(rect.bottom - averageBubleSort2[i] * rect.bottom / dmax)));
          g.DrawLine(&Pen(Color(155 + 3, 216 + 3, 236 + 3), 10.0f), Point(x, rect.bottom), Point(x, (INT)(rect.bottom - averageQuickSort1[i] * rect.bottom / dmax)));
          g.DrawLine(&Pen(Color(155 - 3, 216 - 3, 236 - 3), 10.0f), Point(x, rect.bottom), Point(x, (INT)(rect.bottom - averageQuickSort2[i] * rect.bottom / dmax)));
        }
        g.FillRectangle(&SolidBrush(Color(248, 183, 84)), 100.0f, 100.0f, 64.0f, 64.0f);
        g.DrawString(TEXT("バブルソート"), -1, &Font(L"Times New Roman", 48), PointF(100.0f + 64.0f, 100.0f), &SolidBrush(Color::Black));
        g.FillRectangle(&SolidBrush(Color(155, 216, 236)), 100.0f, 100.0f + 64.0f + 64.0f, 64.0f, 64.0f);
        g.DrawString(TEXT("クイックソート"), -1, &Font(L"Times New Roman", 48), PointF(100.0f + 64.0f, 100.0f + 64.0f + 64.0f), &SolidBrush(Color::Black));
      }
      EndPaint(hWnd, &ps);
    }
    break;
  case WM_COMMAND:
    if (LOWORD(wParam) == IDOK) {
      for (int i = 0; i < MAX_NUM; ++i) {
        int nSumBubleSort1 = 0;
        int nSumBubleSort2 = 0;
        int nSumQuickSort1 = 0;
        int nSumQuickSort2 = 0;
        for (int j = 0; j < TEST_COUNT; ++j) {
          int* pList1 = new int[i];
          if (!pList1) { MessageBox(hWnd, 0, 0, 0); return 0; }
          InitList(pList1, i);
          Shuffle(pList1, i);
          int* pList2 = new int[i];
          if (!pList2) { MessageBox(hWnd, 0, 0, 0); return 0; }
          CopyMemory(pList2, pList1, sizeof(int)*i);
          int nCountBubleSort1 = 0;
          int nCountBubleSort2 = 0;
          int nCountQuickSort1 = 0;
          int nCountQuickSort2 = 0;
          BubleSort(pList1, i, &nCountBubleSort1, &nCountBubleSort2);
          QuickSort(0, i - 1, pList2, &nCountQuickSort1, &nCountQuickSort2);
          nSumBubleSort1 += nCountBubleSort1;
          nSumBubleSort2 += nCountBubleSort2;
          nSumQuickSort1 += nCountQuickSort1;
          nSumQuickSort2 += nCountQuickSort2;
          delete[]pList1;
          delete[]pList2;
        }
        averageBubleSort1[i] = (double)nSumBubleSort1 / (double)TEST_COUNT;
        averageBubleSort2[i] = (double)nSumBubleSort2 / (double)TEST_COUNT;
        averageQuickSort1[i] = (double)nSumQuickSort1 / (double)TEST_COUNT;
        averageQuickSort2[i] = (double)nSumQuickSort2 / (double)TEST_COUNT;
      }
      InvalidateRect(hWnd, 0, 1);
    }
    break;
  case WM_DESTROY:
    PostQuitMessage(0);
    break;
  default:
    return DefWindowProc(hWnd, msg, wParam, lParam);
  }
  return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInst, LPSTR pCmdLine, int nCmdShow)
{
  ULONG_PTR gdiToken;
  GdiplusStartupInput gdiSI;
  GdiplusStartup(&gdiToken, &gdiSI, NULL);
  MSG msg;
  const WNDCLASS wndclass = {
    CS_HREDRAW | CS_VREDRAW,
    WndProc,
    0,
    0,
    hInstance,
    0,
    LoadCursor(0,IDC_ARROW),
    (HBRUSH)(COLOR_WINDOW + 1),
    0,
    szClassName
  };
  RegisterClass(&wndclass);
  const HWND hWnd = CreateWindow(
    szClassName,
    TEXT("Window"),
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    0,
    CW_USEDEFAULT,
    0,
    0,
    0,
    hInstance,
    0
  );
  ShowWindow(hWnd, SW_SHOWDEFAULT);
  UpdateWindow(hWnd);
  while (GetMessage(&msg, 0, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  GdiplusShutdown(gdiToken);
  return (int)msg.wParam;
}