Bubble sorting is one of the most entry-level algorithms in the sorting algorithm. Because of its simple and easy to understand, often in the classroom as a sort of entry algorithm.
Bubble sort in the name of the business, the sort process is like a bubble in the water generally ascending from the bottom up. As shown in the bubbling sort process: Suppose that the sequence to be sorted is {Ten, 2, one, 8, 7}.
Java
1 Packagecom.algorithm.sort.bubble;2 3 Importjava.util.Arrays;4 5 /**6 * Bubble Sort7 * Created by Yulinfeng on 6/19/17.8 */9 Public classBubble {Ten Public Static voidMain (string[] args) { One int[] Nums = {10, 2, 11, 8, 7}; ANums =Bubblesort (nums); - System.out.println (arrays.tostring (nums)); - } the - /** - * Bubble Sort - * @paramnums sequence of digits to be sorted + * @returnsequential sequence of numbers - */ + Private Static int[] Bubblesort (int[] nums) { A at for(inti = 0; i < nums.length; i++) { - for(intj = 0; J < nums.length-i-1; J + +) { - if(Nums[j] > nums[j + 1]) { - inttemp =Nums[j]; -NUMS[J] = nums[j + 1]; -Nums[j + 1] =temp; in } - } to } + - returnnums; the } *}
Python3
1 #Bubble Sort2 defBubble_sort (nums):3 forIinchRange (len (nums)):4 forJinchRange (len (nums)-i-1):5 ifNUMS[J] > nums[j + 1]:6temp =Nums[j]7NUMS[J] = nums[j + 1]8Nums[j + 1] =Temp9 Ten returnNums One ANums = [10, 2, 11, 8, 7] -Nums =Bubble_sort (nums) - Print(nums)
1. Compare sort of bubble sort