Bubble Sort algorithm

Source: Internet
Author: User

The most primitive sorting method is to only take out the largest, and bubble sort in addition to the largest, but also the adjacent 2 elements of the location of the interchange.

Bubble sorting is a simple algorithm in the sorting algorithm, that is, the loop N-wheel, each round has the largest, while the adjacent 2 elements of the position of the interchange.

Java:

Public class bubblesort {  public static void main (String[] args)  {    integer[] intarr = new integer[] {2, 1, 11,  7, 9, 8, 6, 10};    intarr = bubblesort (INTARR);     for  (int i = 0; i < intarr.length; i++)  {       system.out.println (Intarr[i]);    }  }   public static integer[] bubblesort (Integer[] arr)  {    int  tmp;    for  (int i = 1; i < arr.length; i + +)  {//  How many rounds, the length minus one, because the last value does not have to compare       for  (int j =  0; j < arr.length - i; j++)  {//  where to compare each round          if  (arr[j] > arr[j + 1])  {           tmp = arr[j + 1];           arr[j + 1] = arr[j];           arr[j] = tmp;        }       }    }    return arr;  }}

Scala:

Object bubblesort {  def main (args: array[string]) {    val  list = list (4,3,5,1,6,7,2);     println (list);     println (Bubblesort (list));   }    def bubblesort (List: list[int]):  list[int] = list match {    case list ()  =>  List ();     case head :: tail => compute (Head, bubbleSort ( tail));   }    def compute (Data: int, list: list[int]):  List[int] = list match {    case list ()  => List (data );    case head :: tail => if  (Data <= head)  data :: list else head :: compute (Data, tail);      }}

Reference:

Sort algorithm Getting Started bubble sort

Scala for bubbling sorting, merge sorting, and quick sorting

Bubble Sort algorithm

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.