Home > Java > javaTutorial > Guidance and Examples: Learn to implement the selection sort algorithm in Java

Guidance and Examples: Learn to implement the selection sort algorithm in Java

WBOY
Release: 2024-02-18 10:52:06
Original
487 people have browsed it

Guidance and Examples: Learn to implement the selection sort algorithm in Java

Java Selection Sorting Method Code Writing Guide and Examples

Selection sort is a simple and intuitive sorting algorithm. The idea is to select from unsorted elements each time The smallest (or largest) elements are swapped until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code.

  1. Algorithm Principle
    The basic principle of selection sort is to divide the array to be sorted into sorted and unsorted parts, and select the smallest (or largest) element from the unsorted part each time. It is placed at the end of the sorted section. Repeat the above steps until all elements are sorted.
  2. Code implementation steps
    (1) Determine the starting position of the unsorted part, the initial value is 0;
    (2) Find the index of the smallest (or largest) element from the unsorted part No.;
    (3) Exchange the first element of the unsorted part with the smallest (or largest) element;
    (4) Move the starting position of the unsorted part back by one, and repeat steps 2 and 3 until All elements are sorted.
  3. Java sample code

The following is a sample code for the selection sort method written in Java language:

public class SelectionSort {
    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;
                }
            }

            // 将未排序部分的最小元素与未排序部分的首位元素交换
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }

    public static void main(String[] args) {
        int[] arr = {64, 25, 12, 22, 11};
        selectionSort(arr);

        System.out.println("排序后的数组:");
        for (int i : arr) {
            System.out.print(i + " ");
        }
    }
}
Copy after login

In the above sample code, we first define a selectionSort method, used to implement the selection sorting algorithm. Then, create an array to be sorted arr in the main method, and pass it to the selectionSort method for sorting. Finally, the sorted results are output by looping through the array.

  1. Summary
    Selection sort is a simple but inefficient sorting algorithm, and its time complexity is O(n^2). Although it is not suitable for sorting large-scale data, the idea and implementation of selection sort are also helpful in understanding the principles of other sorting algorithms. Through the above code examples, we hope to help readers understand the implementation of selection sorting and flexibly apply it to actual projects.

The above is the detailed content of Guidance and Examples: Learn to implement the selection sort algorithm in Java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template