Home  >  Article  >  Java  >  Comparison of Java and Python3 bubble sorting

Comparison of Java and Python3 bubble sorting

PHP中文网
PHP中文网Original
2017-06-20 09:44:431360browse

Bubble sorting can be said to be one of the most entry-level algorithms among sorting algorithms. Because it is simple and easy to understand, it is often used as an introductory algorithm for sorting in the classroom.

Bubble sorting is a well-known business, and its sorting process is like bubbles in water, ascending from bottom to top. The figure below shows 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  * 冒泡排序 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      * 冒泡排序18      * @param nums 待排序数字序列19      * @return 排好序的数字序列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 #冒泡排序 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)

The above is the detailed content of Comparison of Java and Python3 bubble sorting. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn