JavaScript
function bubbleSort(ary) {
var i, j, temp, len = ary.length;
for(i=1; i<len; i++) {
for(j=len-1; j>=i; j--) {
temp = ary[j];
if(temp < ary[j-1]) {
ary[j] = ary[j-1];
ary[j-1] = temp;
}
}
}
return ary;
}
var ary = [5,4,3,2,1];
console.log(bubbleSort(ary));
Java
public class Test {
public static void bubbleSort(int[] ary) {
int i, j, temp;
int len = ary.length;
for(i=1; i<len; i++) {
for(j=len-1; j>=i; j--) {
temp = ary[j];
if(ary[j] < ary[j-1]) {
ary[j] = ary[j-1];
ary[j-1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] ary = {5,4,3,2,1};
Test.bubbleSort(ary);
for(int it : ary) {
System.out.println(it);
}
}
}
C
#include <stdio.h>
void bubbleSort(int ary[], int len) {
int i, j, temp;
for(i=1; i<len; i++) {
for(j=len-1; j>=i; j--) {
temp = ary[j];
ary[j] = ary[j-1];
ary[j-1] = temp;
}
}
}
main() {
int i;
int ary[] = {5,4,3,2,1};
bubbleSort(ary, 5);
for(i=0; i<5; i++) {
printf("%d", ary[i]);
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/110540.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...