18.1.选择排序
Selection Sort
1.The step of Selection sort
In selection sort, we basically do these three things:
- Find the smallest item.
- Move it to the front.
- Selection sort the rest(using recursion).
For each step, we can design an individual method to implement it.
2.Step implementation
As the moving step requires the index of the to-moving element, the value we return should be index of elements, too.
Find
1 | private static int Find(int[] arr) { |
Given that we may need to Find
the minimun in asked
range, we may improve our method by passing an extra parameter which
point to the start index(We'll use it in the following procedure):
1 | private static int Find(int[] arr, int st) { |
swap
We are going to implement the method by manipulating the index of the elements:
1 | private static void swap(int[] arr, int x, int y) { |
Main
method
Since we would like to do the sorting recursively, we may introduce a helper method and invoke it recursively:
1 | public class Sort { |
Gitalking ...