javascript中的气泡排序

javascript中的气泡排序,javascript,Javascript,我曾尝试在javascript中使用冒泡排序,但在交换数字时失败了。其思想是将数字分组(例如:输入:154514,输出应为:114455)。所以我尝试使用冒泡排序来获得上面的输出 我的代码在这里: function numSort(num){ var temp = ''; var temp1 = ''; arr = num.toString(); var n = arr.length; for(i=0; i<n-1; i++){ for(d=0; d<n-i-1; d++)

我曾尝试在javascript中使用冒泡排序,但在交换数字时失败了。其思想是将数字分组(例如:输入:154514,输出应为:114455)。所以我尝试使用冒泡排序来获得上面的输出

我的代码在这里:

function numSort(num){
var temp = '';
var temp1 = '';
arr = num.toString();
var n = arr.length; 
for(i=0; i<n-1; i++){
    for(d=0; d<n-i-1; d++){
        if(arr[d] > arr[d+1]){
            temp = arr[d];//here I'm trying to swap the inputs as: 89, but it's not letting.
            arr[d] = arr[d+1];
            arr[d+1] = temp;                
        }console.log(arr[d]);
}       
}   
}console.log(numSort(98));
函数numSort(num){
var temp=“”;
var temp1='';
arr=num.toString();
var n=阵列长度;

对于(i=0;i,您实际上非常接近。您遇到的问题是,您无法通过访问索引处的字符来更改字符串中特定字符的值,因为字符串是不可变的。因此,如果我有字符串
var a=“test”
并且我有
a[2]=“p”;
a
仍然是
“test”
而不是
“tept”
,因为字符串是不可变的。尽管如此,我们需要做的是将字符串转换为数组(可变),然后再转换回如下字符串:

function numSort(num){
    var temp = '';
    var temp1 = '';
    arr = num.toString().split(''); // turn the string into an array
    var n = arr.length; 
    for(i=0; i<n-1; i++){
        for(d=0; d<n-i-1; d++){
            if(arr[d] > arr[d+1]){
                temp = arr[d]; // here I'm trying to swap the inputs as: 89, but it's not letting.
                arr[d] = arr[d+1];
                arr[d+1] = temp;                
            }
            console.log(arr[d]);
          }       
    }
    return arr.join(''); // you can optionally do a parseInt() here to convert it back to a number
}
console.log(numSort(98));
函数numSort(num){
var temp=“”;
var temp1='';
arr=num.toString().split(“”);//将字符串转换为数组
var n=阵列长度;

对于(i=0;i,您实际上非常接近。您遇到的问题是,您无法通过访问索引处的字符来更改字符串中特定字符的值,因为字符串是不可变的。因此,如果我有字符串
var a=“test”
并且我有
a[2]=“p”;
a
仍然是
“test”
而不是
“tept”
,因为字符串是不可变的。尽管如此,我们需要做的是将字符串转换为数组(可变),然后再转换回如下字符串:

function numSort(num){
    var temp = '';
    var temp1 = '';
    arr = num.toString().split(''); // turn the string into an array
    var n = arr.length; 
    for(i=0; i<n-1; i++){
        for(d=0; d<n-i-1; d++){
            if(arr[d] > arr[d+1]){
                temp = arr[d]; // here I'm trying to swap the inputs as: 89, but it's not letting.
                arr[d] = arr[d+1];
                arr[d+1] = temp;                
            }
            console.log(arr[d]);
          }       
    }
    return arr.join(''); // you can optionally do a parseInt() here to convert it back to a number
}
console.log(numSort(98));
函数numSort(num){
var temp=“”;
var temp1='';
arr=num.toString().split(“”);//将字符串转换为数组
var n=阵列长度;

对于(i=0;i为什么不将字符串转换为数组

arr = num.toString().split("");

为什么不把字符串转换成数组呢

arr = num.toString().split("");

数组可以使用with方法进行排序-所以我们只使用它:

函数numSort(num){
返回num.toString().split(“”).sort().join(“”);
}
console.log(numSort(154514));//114455
console.log(numSort(765432));//234567

console.log(numSort(32));//23
可以使用with方法对数组进行排序-所以我们只使用它:

函数numSort(num){
返回num.toString().split(“”).sort().join(“”);
}
console.log(numSort(154514));//114455
console.log(numSort(765432));//234567

console.log(numSort(32));/23
交换应该是这样工作的。您是否特别测试了它?隔离了它或将一些系统输出置于两者之间?交换应该是这样工作的。您是否特别测试了它?隔离了它或将一些系统输出置于两者之间?