1. Compare and sort bubble sort, and sort bubble

Source: Internet
Author: User

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)

 

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.