Skip to content

Latest commit

 

History

History
233 lines (196 loc) · 5.21 KB

README.md

File metadata and controls

233 lines (196 loc) · 5.21 KB

冒泡排序

定义一个布尔变量 hasChange,用来标记每轮是否进行了交换。在每轮遍历开始时,将 hasChange 设置为 false。

若当轮没有发生交换,说明此时数组已经按照升序排列,hasChange 依然是为 false。此时外层循环直接退出,排序结束。

代码示例

Python3

defbubbleSort(arr): n=len(arr) # Iterate over all array elementsforiinrange(n): # Last i elements are already in placeforjinrange(n-i-1): ifarr[j] >arr[j+1]: arr[j], arr[j+1] =arr[j+1], arr[j] # 改进版本defbubbleSort(arr): n=len(arr) foriinrange(n-1): has_change=Falseforjinrange(n-i-1): ifarr[j] >arr[j+1]: arr[j], arr[j+1] =arr[j+1], arr[j] has_change=Trueifnothas_change: breakarr= [64, 34, 25, 12, 22, 11, 90] bubbleSort(arr) print(arr)

Java

importjava.util.Arrays; publicclassBubbleSort { privatestaticvoidbubbleSort(int[] nums) { booleanhasChange = true; for (inti = 0, n = nums.length; i < n - 1 && hasChange; ++i) { hasChange = false; for (intj = 0; j < n - i - 1; ++j) { if (nums[j] > nums[j + 1]) { swap(nums, j, j + 1); hasChange = true; } } } } privatestaticvoidswap(int[] nums, inti, intj) { intt = nums[i]; nums[i] = nums[j]; nums[j] = t; } publicstaticvoidmain(String[] args) { int[] nums = {1, 2, 7, 9, 5, 8}; bubbleSort(nums); System.out.println(Arrays.toString(nums)); } }

C++

#include<iostream> #include<vector>usingnamespacestd;voidbubbleSort(vector<int>& arr) { int n = arr.size(); for (int i = 0; i < n - 1; ++i) { bool change = false; for (int j = 0; j < n - i - 1; ++j) { if (arr[j] > arr[j + 1]) { swap(arr[j], arr[j + 1]); change = true; } } if (!change) break; } } intmain() { vector<int> arr = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; bubbleSort(arr); for (int v : arr) cout << v << ""; cout << endl; }

Go

package main import"fmt"funcbubbleSort(nums []int) { hasChange:=truefori, n:=0, len(nums); i<n-1&&hasChange; i++ { hasChange=falseforj:=0; j<n-i-1; j++ { ifnums[j] >nums[j+1] { nums[j], nums[j+1] =nums[j+1], nums[j] hasChange=true } } } } funcmain() { nums:= []int{1, 2, 7, 9, 5, 8} bubbleSort(nums) fmt.Println(nums) }

Rust

fnbubble_sort(nums:&mutVec<i32>){let n = nums.len();for i in0..n - 1{for j in i..n {if nums[i] > nums[j]{let temp = nums[i]; nums[i] = nums[j]; nums[j] = temp;}}}}fnmain(){letmut nums = vec![1,2,7,9,5,8];bubble_sort(&mut nums);println!("{:?}", nums);}

JavaScript

functionbubbleSort(inputArr){for(leti=inputArr.length-1;i>0;i--){lethasChange=false;for(letj=0;j<i;j++){if(inputArr[j]>inputArr[j+1]){consttemp=inputArr[j];inputArr[j]=inputArr[j+1];inputArr[j+1]=temp;hasChange=true;}}if(!hasChange){break;}}returninputArr;}constarr=[6,3,2,1,5];console.log(bubbleSort(arr));

C#

usingstaticSystem.Console;namespacePro;publicclassProgram{publicstaticvoidMain(){int[]test=newint[]{56,876,34,23,45,501,2,3,4,6,5,7,8,9,11,10,12,23,34};BubbleSortNums(test);foreach(varitemintest){WriteLine(item);}ReadLine();}publicstaticvoidBubbleSortNums(int[]nums){intnumchange=0;for(intinitial=0;initial<nums.Length-numchange;initial++){WriteLine($"{initial} start ");// 记录此值 用于迭代开始位置boolchangelog=false;for(intsecond_sortnum=initial;second_sortnum<nums.Length-1;second_sortnum++){if(nums[second_sortnum]>nums[second_sortnum+1]){swap(refnums[second_sortnum],refnums[second_sortnum+1]);if(!changelog){// 记录转换的位置,让initial开始位置从转换位置前开始initial=((second_sortnum-2)>0)?(second_sortnum-2):-1;numchange+=1;}changelog=true;}}}}privatestaticvoidswap(refintcompare_left,refintcompare_right){inttemp=compare_left;compare_left=compare_right;compare_right=temp;}}
close