Several classical algorithms for Java arrays

Source: Internet
Author: User

In Java, array-related algorithms, commonly used are: bubble sort, select sort, and reverse sort.

" Bubble Sort "

    • Basic ideas

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.


    • Algorithm analysis


The total average time complexity of the bubble sort is  , so the speed is relatively slow!

    • Specific implementation


Output Result:


" Select Sort "

    • Basic ideas

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

    • Algorithm analysis

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!

    • Specific implementation


the invert Sort "

    • Basic ideas

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.

    • Algorithm analysis

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!


    • Specific implementation



Several classical algorithms for Java arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.