Several classic algorithms of Java Arrays

Source: Internet
Author: User

Several classic algorithms of Java Arrays

Array-Related Algorithms in Java are commonly used: Bubble sorting, selection sorting, and reverse sorting.

Bubble Sorting]

Basic Ideas

Bubble Sort is a simple Sorting Algorithm in the computer science field. It repeatedly traverses the series to be sorted, compares two elements at a time, and exchanges them if their order is wrong. The traversal of a series is repeated until there is no need to exchange, that is, the series has been sorted.
The name of this algorithm comes from because the larger elements will slowly "float" to the top of the series through the exchange, hence the name.


Algorithm Analysis


The average time complexity of Bubble Sorting is low!

Implementation


Output result:


Select sort <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Signature + Cgo8ZW0 + u/mxvsu8z + s8L2VtPgo8cD48L3A + signature/Signature + 8r91 + signature + LHw0 + signature/bX7rTzu/Signature + signature + 0/Signature = "http://www.2cto.com/uploadfile/Collfiles/20150126/20150126084256206.png" alt = "\">

Average time complexity of Bubble SortingO (n), So the speed is relatively fast!

Implementation


ReverseSort]

Basic Ideas

Reverse sorting sorts the content of the original array in reverse order, which is also frequently used in program development. The basic idea is:

Replace the last element with the first element, the last and second elements with the second element until all array elements are replaced in reverse order.

Algorithm Analysis

Reverse sorting is to replace the elements on both sides of the array, so you only need to loop the length of the array half,

For example, if the length of an array is 7, The for loop only needs three times. Average time complexityO (n/2), So the speed should be the fastest!


Implementation



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.