Java 4 methods to implement bubble sort algorithm

Source: Internet
Author: User
first, the basic idea:

Bubble sort is a simple sort of commutative class. The basic idea is to scan the elements that are to be sorted from scratch, to compare the adjacent elements in sequence during the scan, and to move the elements of the key value back. After each trip, the element with the largest key value is moved to the end, and the position of the element is noted, and the next order is only required to compare to this location until all elements are ordered.

In general, the N-elements are bubble-sorted, requiring a total n-1 trip. The 1th trip needs to compare n-1 times, the 2nd trip needs to compare n-2 times, ... I need to compare n-i times for the first trip. two, 4 algorithm realization

Package Com.struct.array; /** * @ Description Array Sort * @ Project name Java_datastruct * @ Package Name Com.struct.array * @ class name ArraySort * @author Chenlin * @date June 22, 2010 Midday 8:28:46/public class ArraySort {/** * Adjacent two values * * @param arr/public static void B UbbleSort1 (long[] arr) {for (int i = 0; i < arr.length-1; i++) {//MAX do n-1 trip sort for (int j = 0; j < arr.length-1-I; J + +) {//To sort the current unordered interval arr[0......length-i-1] (the range of J is critical, this range is incrementally narrowed) if (Arr[j] > arr[j + 1]) {//Exchange small value to post
                    Face arr[j] = Arr[j] ^ arr[j + 1];
                    Arr[j + 1] = Arr[j] ^ arr[j + 1];
                ARR[J] = Arr[j] ^ arr[j + 1];
    }} list (arr); /** * Adjacent Two value comparison * * @param arr/public static void BubbleSort2 (long[) arr) {for (int i = 0; i < arr.length-1; i++)
            {for (int j = 0; J < arr.length-1-I; j + +) {    if (Arr[j] > arr[j + 1]) {arr[j] = Arr[j] + arr[j + 1];
                    Arr[j + 1] = Arr[j]-arr[j + 1];
                ARR[J] = arr[j]-arr[j + 1];
    }} list (arr); /** * Adjacent Two value comparison * * @param arr/public static void BubbleSort3 (long[) arr) {for (int i = 0; i < arr.length-1; i++)
                    {for (int j = 0; J < arr.length-1-I; j +) {if (Arr[j] > arr[j + 1]) {
                    Long temp = arr[j];
                    ARR[J] = arr[j + 1];
                Arr[j + 1] = temp;
    }} list (arr); /** * Adjacent Two value comparison * * @param arr/public static void BubbleSort4 (long[) arr) {for (int i = 0; i < arr.length-1; i++)
                  {for (int j = arr.length-1-i; j > 0; j--) {if (Arr[j] < arr[j-1]) {  Long temp = arr[j];
                    ARR[J] = arr[j-1];
                ARR[J-1] = temp;
    }} list (arr); public static void list (long[] arr) {for (int i = 0; i < arr.length; i++) {System.out.prin
        T (Arr[i] + "");
    } System.out.println ("");
        public static void Main (string[] args) {long arr[] = {22, 33, 44, 11, 23, 43, 54, 0, 98};
    BUBBLESORT4 (arr);
 }
}

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.