1. Compare sort of bubble sort

Source: Internet
Author: User

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

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.