Home  >  Article  >  Java  >  Learn the principles and code implementation steps of Java selection sort algorithm

Learn the principles and code implementation steps of Java selection sort algorithm

WBOY
WBOYOriginal
2024-02-18 17:27:06973browse

Learn the principles and code implementation steps of Java selection sort algorithm

Understand the Java selection sort algorithm and its code implementation steps

Selection Sort is one of the common sorting algorithms. Its core idea is to find the the minimum value and swap its position with the first element of the array; then find the minimum value among the remaining elements and swap its position with the second element; and so on until the entire array is sorted. The time complexity of selection sort is O(n^2), which is not suitable for sorting large-scale data. However, its principle is simple and intuitive, and it is a good introduction for beginners to understand the sorting algorithm.

The code implementation steps of the Java selection sort algorithm will be analyzed in detail below and specific code examples will be provided.

The implementation steps of the selection sorting algorithm are as follows:

  1. Find the minimum value in the array and record its subscript;
  2. Compare the minimum value in the array with the array Exchange the position of the first element;
  3. Find the minimum value among the remaining elements and record its subscript;
  4. Exchange the minimum value in the array with the second element of the array ;
  5. And so on until the entire array is sorted.

The following is a code example for implementing the selection sort algorithm in Java:

public class SelectionSort {
    public static void main(String[] args) {
        int[] arr = {64, 25, 12, 22, 11};
        selectionSort(arr);
        System.out.println("排序后的数组:");
        printArray(arr);
    }

    public static void selectionSort(int[] arr) {
        int n = arr.length;

        for (int i = 0; i < n-1; i++) {
            int minIndex = i;
            for (int j = i+1; j < n; j++) {
                if (arr[j] < arr[minIndex])
                    minIndex = j;
            }
            // 将最小值与第i个元素交换位置
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }
    
    public static void printArray(int[] arr) {
        int n = arr.length;
        for (int i=0; i < n; ++i)
            System.out.print(arr[i] + " ");
        System.out.println();
    }
}

In the above code, we first define an int array arr and assign it an initial value. The selectionSort() method is then called to sort the array, in which we use two nested for loops to traverse the array and find the minimum value.

In the outer loop, we use the variable i to control each element of the array traversed, and then in the inner loop, we use the variable j to traverse the remaining elements starting from the next position of i, and compare The sizes of arr[j] and arr[minIndex] are used to find the subscript of the current minimum value.

After finding the minimum value, we use a temporary variable temp to exchange the minimum value with the value of the current position element. Finally, when the outer loop traversal ends, the entire array will have been sorted from small to large.

After completing the sorting, we call the printArray() method to print the sorted array.

The above is a detailed analysis of the Java selection sort algorithm and its code implementation steps, and provides specific code examples. Although selection sorting does not have high efficiency, it is simple and easy to understand, and it lays the foundation for understanding other more complex sorting algorithms. At the same time, understanding different sorting algorithms helps us choose the most appropriate sorting method to meet our needs in actual development.

The above is the detailed content of Learn the principles and code implementation steps of Java selection sort algorithm. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn