How to use Java to implement selection algorithm
This article introduces the knowledge of "how to use Java to implement the selection algorithm". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. selection and sorting
Selective sorting is to rank the lowest value in the array to the current first place during each traversal.
A total of (array length-1) traverses are required. In each traversal, the value of the first index is assumed to be the minimum value, and then compared with the next value. If the value of the minimum index is greater than the other values, the smaller index is regarded as the minimum index. Then continue to compare the value of the minimum index with the value of the next index, repeat this operation, and you will eventually get the minimum value and its index in this traversal. Swap the minimum value with the value of the first bit, so that the minimum value is placed at the beginning of the array to complete this traversal.
The time complexity of selective sorting is O (N ^ 2).
Second, the code implements package com.example.algorithmdemo.sortingAlgorithm;/** * selection sorting * / public class SelectionSort {/ * array sorting * @ param a * / public static void sort (Comparable [] a) {for (int I = 0 * i0) } / * Array elements I and j exchange positions * @ param a * @ param I * @ param j * / private static void exchange (Comparable [] amemint I int j) {Comparable temp = a [I]; a [I] = a [j]; a [j] = temp;}} III, Test package com.example.algorithmdemo.test Import com.example.algorithmdemo.sortingAlgorithm.SelectionSort;import java.util.Arrays;public class SelectionTest {public static void main (String [] args) {Integer [] a = {3, 2, 7, 6, 8, 1, 4, 5, 7}; SelectionSort.sort (a); System.out.println (Arrays.toString (a));}} "how to use Java to implement the selection algorithm" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!