Bubble sort (Java)

Source: Internet
Author: User
Tags array length

    • L1 Bubble Sort
      • L2 Bubble Sort Introduction
      • L3 code
      • L4 Source
L1 Bubble Sort L2 Bubble Sort Introduction

Bubble sort (Bubble sort), also known as bubble sort or bubble sort.

It is a relatively simple sort algorithm. It iterates through a number of minor sorted columns, each of which compares the size of two adjacent numbers from the next, and, if the former is larger than the latter, swaps their positions. Thus, after a single traversal, the largest element is at the end of the sequence! When traversing again in the same way, the second largest element is arranged before the largest element. Repeat the operation until the entire sequence is in order!

    • Realization idea: Find out the first big element, and the last element interchange; Find the second largest element, and swap it with the elements before the first big one ... Find len-i The maximum value in the previous value, and swap the first len-i-1 element (the last element in each row to remove the ordered element).

L3 code
/** Len: Array length*/ Public int[]Sort() {intBigger =0;intBigger_index =0;inti =0, j =0; for(i = len-1; i >0; i--) {bigger = array[0]; Bigger_index =0; for(j =1; J <= I; J + +) {if(Bigger < ARRAY[J])                    {bigger = Array[j];                Bigger_index = j; }            }//The maximum value of this trip            //System.out.println ("Maximum" +bigger);            Swap(Bigger_index, I); }returnArray }
L4 Source

https://github.com/waddless/data_structure/tree/master/comparison_sorting/BubbleSort#l1-%E5%86%92%E6%B3%A1%E6% 8e%92%e5%ba%8f

Bubble sort (Java)

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.