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