Sort algorithm (II.)--recursive implementation of bubble sort

Source: Internet
Author: User

It is well known that loops and recursion, in many cases, can be converted to each other.

Then, the bubble sort (Bubble sort), as a typical double loop structure, can also be converted to recursive form.

However, converting recursion to a loop is beneficial for the program's operation because it avoids unpredictable "method stack" occurrences.

In most cases, this is not recommended, even if the recursive code might be very nice to implement. Beauty and efficiency are often conflicting.

So, the following bubble sort is for reference only, readers can use it to broaden their thinking. However, never write bubbles sort like this!

Public final class Bubblesortrecursion implements sort {    @Override public    void Sort (int[] array) {        recursion (array, 0, array.length-2);    }    private void recursion (int[] array, int left, int. right) {        if (right > 0) {            if (Array[left] > array[left + 1 ] {                Arrayhelper.swap (array, left, left + 1);            }            if (left >= right) {                recursion (array, 0, right-1), or            else {                recursion (array, left + 1, right);            }        }    }}

Sort algorithm (II.)--recursive implementation of bubble sort

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.