A complete set of in-depth exploration of Java arrays - advanced knowledge stage 1, selection sorting

A complete set of in-depth exploration of Java arrays - advanced knowledge stage 1, selection sorting


Table of contents

The importance of array learning

The specific sorting process of selection sorting

Specific selection and sorting process


General article link:https://laoshifu.blog.csdn.net/article/details/134906408

The importance of array learning

Arrays are one of the data structures that we must master, and they will be of great help to us in the future.

  • Improve program efficiency: Array is an efficient data structure that can quickly access and modify data. In actual production and life, arrays are widely used in various scenarios that require efficient data processing, such as image processing, scientific computing, financial analysis, etc. By learning arrays, students can process data more efficiently and improve program execution efficiency.
  • Enhance programming abilities: Arrays are one of the commonly used data structures in programming. Mastering the use of arrays is very important for students to improve their programming abilities. In the actual programming process, the use of arrays is very common. Mastering the use of arrays can help students become more proficient in programming and improve programming efficiency and code quality.
  • Cultivate logical thinking: Array is an abstract data structure. By learning arrays, students can develop their logical thinking abilities. In actual problem solving, many problems can be transformed into array processing problems. By learning arrays, students can think about problems more clearly and come up with effective solutions.

Learning arrays can be a somewhat difficult task for students, but if you keep studying, you can master it. Here are some words of encouragement for students learning about arrays:

  • Arrays are the basis of programming, and mastering the use of arrays is very important to becoming a good programmer.
  • Learning arrays can be difficult, but if you stick with it, you'll be able to master it.
  • By learning arrays, you can process data more efficiently, improve program execution efficiency, and show your programming abilities.
  • Arrays are widely used. Mastering the use of arrays can make you better in your future study and work.
  • Believe in yourself, you will be able to master the use of arrays and become an excellent programmer!

The specific sorting process of selection sorting

Selection Sort is a simple and intuitive sorting algorithm. Its working principle is to select the smallest (or largest) element from the data elements to be sorted each time and store it at the beginning of the sequence until all the data elements to be sorted are arranged.

The specific sorting process is as follows:

Find the smallest (or largest) element in the unsorted sequence and store it at the beginning of the sorted sequence.

Then continue to find the smallest (or largest) element from the remaining unsorted elements, and then put it at the end of the sorted sequence.

Repeat step two until all elements are sorted.

For example, for an integer array [64, 25, 12, 22, 11], the specific process of selection sorting is as follows:

Round 1: Find the smallest element 11, exchange positions with the first element 64, and get [11, 25, 12, 22, 64].

Round 2: Find the smallest element 12 among the remaining elements, exchange positions with the second element 25, and get [11, 12, 25, 22, 64].

Round 3: Find the smallest element 22 among the remaining elements, exchange positions with the third element 25, and get [11, 12, 22, 25, 64].

Round 4: There is no need to compare the remaining elements because the last element must be the largest. So the final sorting result [11, 12, 22, 25, 64] is obtained.

During selection sort, each round finds the smallest (or largest) element of the current unsorted section and exchanges it with the first element of the unsorted section. In this way, after each round, the first element of the unsorted part will be the dividing line between the sorted part and the unsorted part.

public class Demo1 {
    public static void main(String[] args) {
        // 定义待排序的数组
        int[] arr = { 64, 25, 12, 22, 11 };

        // 打印排序前的数组
        System.out.println("排序前的数组:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();

        // 执行选择排序算法
        int n = arr.length;

        // 遍历数组,进行n-1轮比较和交换
        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[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }

        // 打印排序后的数组
        System.out.println("排序后的数组:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}

Specific selection and sorting process

The following is an example of using continuous array changes to represent selection sort. We will perform selection sort on the array [64, 25, 12, 22, 11]:

Array before sorting: [64, 25, 12, 22, 11]


Round 1:

Find the smallest element 11 and swap places with the first element 64.

[11, 25, 12, 22, 64]


Round 2:

Find the smallest element 12 among the remaining elements, and exchange positions with the second element 25.

[11, 12, 25, 22, 64]


Round 3:

Find the smallest element 22 among the remaining elements, and exchange positions with the third element 25.

[11, 12, 22, 25, 64]


Round 4:

There is no need to compare the remaining elements because the last element must be the largest.

[11, 12, 22, 25, 64]


Sorted array: [11, 12, 22, 25, 64]


In this way, through continuous array changes, we demonstrate the process of selection sort. In each round, we find the smallest element of the current unsorted part and swap it with the first element of the unsorted part. Finally get the sorted array.

Guess you like

Origin blog.csdn.net/feng8403000/article/details/134909468