Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement Array selection sorting in java

Shulou Source: shulou.com Published: 2022-06-02 08:24:21 09月25日 Update

Java how to achieve array selection sort, for this problem, this article details the corresponding analysis and solution, hoping to help more small partners who want to solve this problem find a simpler and easier way.

public static void main(String[] args) { //sort array int[] arr = {5,6,3,1,8,7,2,4}; //minimum numerical subscript int minIndex = 0; //array length int arrLen = arr.length; //array length minus 1 is because the last one after the previous number sorting is the maximum or minimum value for (int i = 0; i

< arrLen - 1; i++) { minIndex = i; for (int j = i + 1; j < arrLen; j++) { //比较运算符>

Sort from small to large, if you need to sort from large to small, change to

< if (arr[minIndex] >

arr[j]) { minIndex = j; } } //replace the minimum value to the front int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; //after sorting System.out.print("Result after sorting round"+(i+1)+":"); for (int item : arr) { System.out.print(item); } System.out.println(""); } //after sorting System.out.print("Sort result: "); for (int item : arr) { System.out.print(item); }} Execution Results:

1st round sorting results: 16358724

Results after the second round sorting: 12358764

3rd round sorting results: 12358764

4th round sorting results: 12348765

The fifth round of sorting results: 12345768

6th round sorting result: 12345678

7th round sorting result: 12345678

Sorted results: 12345678

About java how to achieve the array selection sorting problem solution to share here, I hope the above content can have some help for everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.

Tags: Sort result array minimum question selection number more length help solution easy maximum small to large easy subscript small content guys buddies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB NVidia Redmi vpn Shulou Technology