1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Visual C++ 2017] ソートを可視化してみた #1

Posted at

#はじめに
ソート処理をなんとなく使っていたため、視覚的に取り扱ってみたかった。
バブルソートやクイックソートは知っていたが、他のソートアルゴリズムには疎く、自己学習にもなり良い機会だと思った。

ソートアルゴリズムは、参考となるページがWeb上には多くあるため、この投稿では割愛。

#Winsowの作成
Windowsデスクトップアプリケーションで新規Windowが作成できる。
New_Window.jpg
New_Window2.jpg

#描画
hPen MoveToEx() LineTo() により枠組みを描画。
hButtonWnd[] でボタンを設置し、初期化やソート処理の開始などをコントロールさせる。
Sample.jpg
とても素朴なインターフェースですが、とりあえずはこれで。

#可視化
計算の度に画面の更新が必要なため、ソートアルゴリズム中に、InvalidateRect() UpdateWindow()を入れた。

sample.cpp
if (input_num[j - 1] > input_num[j]) {
    temp = input_num[j];
    input_num[j] = input_num[j - 1];
    input_num[j - 1] = temp;
    InvalidateRect(hWd, NULL, TRUE);
    UpdateWindow(hWd);
}

データ個数を100個用意し、乱数を生成。バブルソートを試してみる。
sample_bubble.gif

目的を達成できたようです。
他のソートも充実させていきたいと思います。

#リンク
以下リンク先では多くのソートを見ることができ、その影響を受けました。

15 Sorting Algorithms in 6 Minutes
https://www.youtube.com/watch?v=kPRA0W1kECg

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?