1. Compare and sort bubble sort, and sort bubble
Bubble Sorting is one of the most entry-level algorithms in sorting algorithms. It is easy to understand and often serves as an entry-level Algorithm for sorting in the classroom.
Bubble Sorting is known as a business. The sorting process is like a bubble in the water, which is generally upgraded from bottom to top. As shown in the Bubble sorting process: assume that the sequence to be sorted is {10, 2, 11, 8, 7 }.
Java
1 package com. algorithm. sort. bubble; 2 3 import java. util. arrays; 4 5/** 6 * bubble sort 7 * Created by yulinfeng on 6/19/17. 8 */9 public class Bubble {10 public static void main (String [] args) {11 int [] nums = {10, 2, 11, 8, 7 }; 12 nums = bubbleSort (nums); 13 System. out. println (Arrays. toString (nums )); 14} 15 16/** 17 * Bubble Sorting 18 * @ param nums number sequence to be sorted 19 * @ return sort the number sequence 20 */21 private static int [] bubbleSort (int [] nums) {22 23 for (int I = 0; I <nums. length; I ++) {24 for (int j = 0; j <nums. length-I-1; j ++) {25 if (nums [j]> nums [j + 1]) {26 int temp = nums [j]; 27 nums [j] = nums [j + 1]; 28 nums [j + 1] = temp; 29} 30} 31} 32 33 return nums; 34} 35}
Python3
1 # Bubble Sorting 2 def bubble_sort (nums): 3 for I in range (len (nums): 4 for j in range (len (nums)-I-1 ): 5 if nums [j]> nums [j + 1]: 6 temp = nums [j] 7 nums [j] = nums [j + 1] 8 nums [j + 1] = temp 9 10 return nums11 12 nums = [10, 2, 11, 8, 7] 13 nums = bubble_sort (nums) 14 print (nums)