In Java, array-related algorithms, commonly used are: bubble sort, select sort, and reverse sort.
" Bubble Sort "
Bubble sort (Bubble sort) is a simpler sort algorithm in the field of computer science. It iterates over the sequence of columns to be sorted, compares two elements at a time, and swaps them if their order is wrong. The work of traversing the sequence is repeated until there is no need to swap, meaning that the sequence is sorted.
The name of this algorithm is because the larger the element will slowly "float" to the top of the sequence of numbers, hence the name.
The total average time complexity of the bubble sort is , so the speed is relatively slow!
Output Result:
" Select Sort "
The basic idea of direct selection is to compare the specified sort position with other array elements, and exchange the element values if the condition satisfies.
Difference and bubble sort, instead of exchanging adjacent elements, instead of exchanging the elements that satisfy the condition with the position of the specified sort
Each trip selects the largest or smallest element in the data element to be sorted, placing it in the final order until all the data elements to be sorted are finished.
The total average time complexity of the bubble sort O (n), so the speed is relatively fast!
the invert Sort "
Reversal sorting is the reordering of the contents of the original array in reverse order, and the reverse sort algorithm is often used in program development. The basic idea is:
The last element is replaced with the first element, and the penultimate element is replaced with the second one, until all the array elements are reversed.
Reverse sorting is the substitution of elements on both sides of an array, so only half of the loop's length is required.
For example, if the array length is 7, then the for loop only needs to loop 3 times. Average Time complexity O (N/2), so its speed should be the fastest!
Several classical algorithms for Java arrays