Placeholder Image

Subtitles section Play video

  • 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.

    它的輸入將是一個整數數組和我要交換的兩個項目的索引。

  • 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.

    然後用第二個位置的值覆蓋第一個位置。

  • And then you put your temporary variable in that second location.

    然後將臨時變量放在第二個位置。

  • 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.

    完成後,我將使用打印語句。

  • 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.

    在我看來,它已被分類。

So as a programming refresher, I'm going to convert the algorithm that we just talked about, selection sort, into code.

是以,作為編程複習,我將把我們剛才談到的算法--選擇排序--轉換成代碼。

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it