字幕表 動画を再生する
So as a programming refresher, I'm going to convert the algorithm that we just talked about, selection sort, into code.
そこで、プログラミングの復習として、先ほど話したアルゴリズム、選択ソートをコードに変換してみよう。
So I'm going to start off with an array that represents the values that I just did a demo of.
まずは、先ほどデモを行った値を表す配列から始めることにする。
I'm going to create a method that can be used to find the location of the minimum item.
最小アイテムの位置を見つけるのに使えるメソッドを作ろうと思う。
So it's going to be finding the index.
だからインデックスを見つけることになる。
So its input will be an array.
つまり、入力は配列となる。
It's going to iterate through the entire array.
配列全体を繰り返し処理する。
Okay, and we need some way of keeping track of where that minimum item is at.
そして、その最小限のアイテムがどの位置にあるのかを把握する方法が必要だ。
So I'm going to create an integer variable that represents that location.
そこで、その場所を表す整数変数を作ろうと思う。
We'll start it out at the beginning of the array.
配列の最初から始めることにしよう。
And then I'm going to look at each individual item as I go through this array and see if it's better than that minimum candidate I've got so far.
そして、この配列を見ながら個々の項目を見ていき、これまでに得た最低候補よりも良いかどうかを確認するんだ。
So this is kind of comparable to when I was pushing a piece up to be slightly out of line.
だからこれは、私がピースを押し上げて少しラインから外れていたときに匹敵するようなものだ。
If it's better, I'll kind of update what my best candidate is.
もし良くなったら、私の最有力候補を更新するつもりだ。
And when I'm all done, I'll return my best overall location.
そして、すべてが終わったら、総合的なベストロケーションを返す。
Of course, we sometimes want to start at a different location.
もちろん、別の場所からスタートしたいこともある。
So I'm going to add in an integer variable that represents my start location and both my initial value of location and my loop start there.
そこで、開始位置を表す整数変数を追加し、位置の初期値とループの開始位置の両方を追加することにする。
Okay, we also need the ability to swap elements.
さて、エレメントを入れ替える機能も必要だ。
So I'm going to create a static method to help me swap elements.
そこで、要素を入れ替えるための静的メソッドを作ろうと思う。
Its input will be an array of integers and the two indices of the items that I want to swap.
入力は整数の配列と、入れ替えたい項目の2つのインデックスである。
So if you remember the process of a swap, you have to have a temporary variable that represents the value in one of the locations.
つまり、スワップのプロセスを思い出すなら、いずれかの場所の値を表す一時変数を用意しなければならない。
You copy it from one location.
ある場所からコピーするのだ。
Then you overwrite that first location with the value from the second location.
そして、最初の場所を2番目の場所の値で上書きする。
And then you put your temporary variable in that second location.
そして、その2番目の場所に一時変数を置く。
Okay, so let's go back and finish our main selection sort.
さて、戻ってメインのセレクション・ソートを仕上げよう。
So I'm going to have to go through each of my positions of interest in my array.
だから、私の配列の中で興味のあるポジションをひとつひとつ調べていかなければならない。
So I'm going to create a loop that starts at i and 0 and goes all the way through my array.
そこで、iと0から始まって配列の中をずっと回るループを作ろうと思う。
And this is identifying my location that I'm going to be swapping something into.
そして、これは私が何かを交換する場所を特定するものだ。
I'm then going to use my little helper method to find the minimum value from my array.
次に、この小さなヘルパー・メソッドを使って、配列から最小値を見つけよう。
Starting at that particular location.
その特定の場所からスタートする。
And then I'll do my swap.
それからスワップをする。
And when I'm all done, I'm going to use a print statement.
すべてが終わったら、printステートメントを使ってみよう。
I'm going to use a helper method, arrays.toString, to help me print out the contents of the array.
配列の中身をプリントアウトするために、ヘルパー・メソッドarrays.toStringを使おう。
And let's run it.
そして実行しよう。
And it looks sorted to me.
そして、私には整理されているように見える。