JAVA simple selection sorting algorithm principle and implementation
Simple selection sorting: (select the smallest value, put it first, and then move the first value backward, and so on) Compare the first value with each subsequent one one by one, and put the smallest one at the top each time. The bits are pushed backward (that is, the first bit just selected is the minimum value and will no longer participate in the comparison, and the number of comparisons is reduced by 1)
Complexity: The number of operations required to move the record is less 0--3 (n-1), regardless of the initial arrangement of the records, the number of comparisons required between keywords is the same, which is n(n-1)/2, and the total time complexity is O(n2);
space Complexity O(1)
Algorithm improvement: Every comparison is to put the smallest value first, so you can compare it to the end, find the smallest value, and put it directly in the first place. Eliminate meaningless swap and move operations. You can also change the direction, and compare the last bit with each previous one, making the maximum value sink to the bottom each time, and the last bit advances forward.
JAVA source code:
public static void selectSort(Date[] days) { int min; Date temp; for (int i = 0; i < days.length; i++) { min = i; for (int j = min + 1; j < days.length; j++) { if (days[min].compare(days[j]) > 0) { min = j; } } if (min != i) { temp = days[i]; days[i] = days[min]; days[min] = temp; } } } class Date { int year, month, day; Date(int y, int m, int d) { year = y; month = m; day = d; } public int compare(Date date) { return year > date.year ? 1 : year < date.year ? -1 : month > date.month ? 1 : month < date.month ? -1 : day > date.day ? 1 : day < date.day ? -1 : 0; } public void print() { System.out.println(year + " " + month + " " + day); } }
Simple Selection Sort:
Simple Selection Sort is similar to Bubble Sort (Bubble Sort), each time it will be in the remaining Select the highest value from the set of elements below and fill it into the current position. The only difference is that bubble sort will exchange the position of the element every time it finds that it is less than (or greater than) the current value, while simple selection sort selects the highest value among the remaining elements and exchanges data with the current position.
For example, for the element set R={37, 40, 38, 42, 461, 5, 7, 9, 12}
In the first sorting: 37 is directly exchanged with 5, Form a new sequence R1={5,40,38,42,461,37,7,9,12}
In the second sorting: 40 is directly exchanged with 7 to form a new sequence R2={5,7, 38,42,461,37,40,9,12}
and so on until the last element (note: in the second pass of sorting, 38 is smaller than 42, but they do not exchange data).
The following is a Java implementation version of simple selection sorting:
public static void selectionSort(int[] data) { if (data == null || data.length <= 1) return; int i, j, value, minPos, len = data.length; int outer = len - 1, tmp; for (i = 0; i < outer; i++) { value = data[i]; minPos = -1; for (j = i + 1; j < len; j++) { if (data[j] < value) { minPos = j; value = data[j]; } } if (minPos != -1) { tmp = data[i]; data[i] = value; data[minPos] = tmp; } // for (int k = 0; k < len; k++) { // System.out.print(data[k] + " , "); // } // System.out.println(); } } public static void main(String[] args) { int[] coll = { 37, 40, 38, 42, 461, 5, 7, 9, 12 }; selectionSort(coll); for (int i = 0; i < coll.length; i++) { System.out.print(coll[i] + " , "); } }
Tree Selection Sort (Tree Selection Sort)
The tree selection sorting algorithm is typical of simple selection sorting. Algorithm for exchanging space for time. The idea is to treat the sorted N elements, construct relatively small (n+1)/2 numbers, and then construct relatively small [n+1]/4 numbers until there is only one element. Constructed into a complete binary tree.
When sorting, that element is the smallest, take out the smallest element, replace the element with the "maximum value", and then adjust the complete binary tree.
The following is a Java implementation version of tree selection sorting:
public static void treeSelectionSort(int[] data) { if (data == null || data.length <= 1) return; int len = data.length , low = 0 , i , j; // add Auxiliary Space int[] tmp = new int[2*len -1]; int tSize = tmp.length; //construct a tree for(i =len-1 , j=tmp.length-1;i >=0 ;i--,j--){ tmp[j]=data[i]; } for(i = tSize -1 ; i > 0 ; i-=2){ tmp[(i-1)/2] = tmp[i] > tmp[i-1]? tmp[i-1]:tmp[i]; } //end //remove the minimum node. while(low < len){ data[low++] = tmp[0]; for(j=tSize-1;tmp[j]!=tmp[0];j--); tmp[j] = Integer.MAX_VALUE; while(j > 0){ if(j%2 == 0){ //如果是右节点 tmp[(j-1)/2] = tmp[j] > tmp[j-1]?tmp[j-1]:tmp[j]; j = (j-1)/2; }else{ //如果是左节点 tmp[j/2]=tmp[j] > tmp[j+1]? tmp[j+1]:tmp[j]; j = j/2; } } } }
When constructing a complete binary tree, a set of N elements requires 2*N -1 auxiliary space.
Code:
while(j > 0){ if(j%2 == 0){ //如果是右节点 tmp[(j-1)/2] = tmp[j] > tmp[j-1]?tmp[j-1]:tmp[j]; j = (j-1)/2; }else{ //如果是左节点 tmp[j/2]=tmp[j] > tmp[j+1]? tmp[j+1]:tmp[j]; j = j/2; } }
implements recursive construction of the minimum value in the new set.
For more articles related to the principle and implementation of JAVA simple selection sorting algorithm, please pay attention to the PHP Chinese website!

Javaispopularforcross-platformdesktopapplicationsduetoits"WriteOnce,RunAnywhere"philosophy.1)ItusesbytecodethatrunsonanyJVM-equippedplatform.2)LibrarieslikeSwingandJavaFXhelpcreatenative-lookingUIs.3)Itsextensivestandardlibrarysupportscompr

Reasons for writing platform-specific code in Java include access to specific operating system features, interacting with specific hardware, and optimizing performance. 1) Use JNA or JNI to access the Windows registry; 2) Interact with Linux-specific hardware drivers through JNI; 3) Use Metal to optimize gaming performance on macOS through JNI. Nevertheless, writing platform-specific code can affect the portability of the code, increase complexity, and potentially pose performance overhead and security risks.

Java will further enhance platform independence through cloud-native applications, multi-platform deployment and cross-language interoperability. 1) Cloud native applications will use GraalVM and Quarkus to increase startup speed. 2) Java will be extended to embedded devices, mobile devices and quantum computers. 3) Through GraalVM, Java will seamlessly integrate with languages such as Python and JavaScript to enhance cross-language interoperability.

Java's strong typed system ensures platform independence through type safety, unified type conversion and polymorphism. 1) Type safety performs type checking at compile time to avoid runtime errors; 2) Unified type conversion rules are consistent across all platforms; 3) Polymorphism and interface mechanisms make the code behave consistently on different platforms.

JNI will destroy Java's platform independence. 1) JNI requires local libraries for a specific platform, 2) local code needs to be compiled and linked on the target platform, 3) Different versions of the operating system or JVM may require different local library versions, 4) local code may introduce security vulnerabilities or cause program crashes.

Emerging technologies pose both threats and enhancements to Java's platform independence. 1) Cloud computing and containerization technologies such as Docker enhance Java's platform independence, but need to be optimized to adapt to different cloud environments. 2) WebAssembly compiles Java code through GraalVM, extending its platform independence, but it needs to compete with other languages for performance.

Different JVM implementations can provide platform independence, but their performance is slightly different. 1. OracleHotSpot and OpenJDKJVM perform similarly in platform independence, but OpenJDK may require additional configuration. 2. IBMJ9JVM performs optimization on specific operating systems. 3. GraalVM supports multiple languages and requires additional configuration. 4. AzulZingJVM requires specific platform adjustments.

Platform independence reduces development costs and shortens development time by running the same set of code on multiple operating systems. Specifically, it is manifested as: 1. Reduce development time, only one set of code is required; 2. Reduce maintenance costs and unify the testing process; 3. Quick iteration and team collaboration to simplify the deployment process.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
