冒泡排序算法 JavaScript

发布于 2024-12-05 15:17:44 字数 1753 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

失去的东西太少 2024-12-12 15:17:44

冒泡排序的一些代码

不应该用于较大的数组,由于其简单性可以用于较小的数组。

优化方式,包含所有检查

const bubble_Sort = (nums) => {
  if(!Array.isArray(nums)) return -1; // --->if passed argument is not array
  if(nums.length<2) return nums; // --->if array length is one or less

    let swapped=false
     temp=0,
     count=-1,
     arrLength=0;


    do{
      count ++;
      swapped=false;
      arrLength = (nums.length-1) - count; //---> not loop through sorted items
      for(let i=0; i<=arrLength; i++){
          if(nums[i]>nums[i+1]){
            temp=nums[i+1];
            nums[i+1]=nums[i];
            nums[i]=temp;
            swapped=true;
          }
      }
    }

    while(swapped)
    return nums;
  }
  console.log(bubble_Sort([3, 0, 2, 5, -1, 4, 1]));

方法 1

var a = [33, 103, 3, 726, 200, 984, 198, 764, 9];

function bubbleSort(a) {
    var swapped;
    do {
        swapped = false;
        for (var i=0; i < a.length-1; i++) {
            if (a[i] > a[i+1]) {
                var temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
                swapped = true;
            }
        }
    } while (swapped);
}

bubbleSort(a);
console.log(a);

方法 2

function bubbleSort(items) {
    var length = items.length;
    //Number of passes
    for (var i = 0; i < length; i++) { 
        //Notice that j < (length - i)
        for (var j = 0; j < (length - i - 1); j++) { 
            //Compare the adjacent positions
            if(items[j] > items[j+1]) {
                //Swap the numbers
                var tmp = items[j];  //Temporary variable to hold the current number
                items[j] = items[j+1]; //Replace current number with adjacent number
                items[j+1] = tmp; //Replace adjacent number with current number
            }
        }        
    }
}

方法 3

function bubbleSort() {
    var numElements = this.dataStore.length;
    var temp;
    for (var outer = numElements; outer >= 2; --outer) {
        for (var inner = 0; inner <= outer-1; ++inner) {
            if (this.dataStore[inner] > this.dataStore[inner+1]) {
                swap(this.dataStore, inner, inner+1); }
        }
        console.log(this.toString()); 
    }
}

Couple of codes for bubble sort

bubblesort should not be used for larger arrays, can be used for smaller ones for its simplicity.

Optimized way, with all Checks

const bubble_Sort = (nums) => {
  if(!Array.isArray(nums)) return -1; // --->if passed argument is not array
  if(nums.length<2) return nums; // --->if array length is one or less

    let swapped=false
     temp=0,
     count=-1,
     arrLength=0;


    do{
      count ++;
      swapped=false;
      arrLength = (nums.length-1) - count; //---> not loop through sorted items
      for(let i=0; i<=arrLength; i++){
          if(nums[i]>nums[i+1]){
            temp=nums[i+1];
            nums[i+1]=nums[i];
            nums[i]=temp;
            swapped=true;
          }
      }
    }

    while(swapped)
    return nums;
  }
  console.log(bubble_Sort([3, 0, 2, 5, -1, 4, 1]));

Method 1

var a = [33, 103, 3, 726, 200, 984, 198, 764, 9];

function bubbleSort(a) {
    var swapped;
    do {
        swapped = false;
        for (var i=0; i < a.length-1; i++) {
            if (a[i] > a[i+1]) {
                var temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
                swapped = true;
            }
        }
    } while (swapped);
}

bubbleSort(a);
console.log(a);

Method 2

function bubbleSort(items) {
    var length = items.length;
    //Number of passes
    for (var i = 0; i < length; i++) { 
        //Notice that j < (length - i)
        for (var j = 0; j < (length - i - 1); j++) { 
            //Compare the adjacent positions
            if(items[j] > items[j+1]) {
                //Swap the numbers
                var tmp = items[j];  //Temporary variable to hold the current number
                items[j] = items[j+1]; //Replace current number with adjacent number
                items[j+1] = tmp; //Replace adjacent number with current number
            }
        }        
    }
}

Method 3

function bubbleSort() {
    var numElements = this.dataStore.length;
    var temp;
    for (var outer = numElements; outer >= 2; --outer) {
        for (var inner = 0; inner <= outer-1; ++inner) {
            if (this.dataStore[inner] > this.dataStore[inner+1]) {
                swap(this.dataStore, inner, inner+1); }
        }
        console.log(this.toString()); 
    }
}
最舍不得你 2024-12-12 15:17:44
for (var j=records.length; j<1; j--){

难道不应该是这样吗

for (var j=records.length; j>1; j--){
for (var j=records.length; j<1; j--){

Shouldn't that be

for (var j=records.length; j>1; j--){
愁以何悠 2024-12-12 15:17:44

ES6 JavaScript 中的一个简单实现是

    function BubbleSort(arr) {
      const sortedArray = Array.from(arr);
      let swap;
      do {
        swap = false;
        for (let i = 1; i < sortedArray.length; ++i) {
          if (sortedArray[i - 1] > sortedArray[i]) {
            [sortedArray[i], sortedArray[i - 1]] = [sortedArray[i - 1], sortedArray[i]];
            swap = true;
          }
        }
      } while (swap)
      return sortedArray;
    }
console.log(BubbleSort([3, 12, 9, 5]));

A simple implementation in ES6 JavaScript will be

    function BubbleSort(arr) {
      const sortedArray = Array.from(arr);
      let swap;
      do {
        swap = false;
        for (let i = 1; i < sortedArray.length; ++i) {
          if (sortedArray[i - 1] > sortedArray[i]) {
            [sortedArray[i], sortedArray[i - 1]] = [sortedArray[i - 1], sortedArray[i]];
            swap = true;
          }
        }
      } while (swap)
      return sortedArray;
    }
console.log(BubbleSort([3, 12, 9, 5]));
复古式 2024-12-12 15:17:44

您应该在第二个循环中使用 j 而不是 i,并且不要忘记将 j<1 更改为 j>1

you should use j instead of i in the second loop, and don't forget to change the j<1 to j>1

茶底世界 2024-12-12 15:17:44

我相信在冒泡排序中,一旦 i 循环完成迭代,那么第 i 个元素现在就处于正确的位置。这意味着您应该将 j 循环编写为

for (var j = i + 1; j < records.length; j++)

否则您的冒泡排序将(甚至更加)效率低下。

I believe that in a bubble sort, once the i loop has completed an iteration, then the i'th element is now in its correct position. That means that you should write the j loop as

for (var j = i + 1; j < records.length; j++)

Otherwise your bubble sort will be (even more) inefficient.

马蹄踏│碎落叶 2024-12-12 15:17:44

我的解决方案:

  function bubbleSort(A){
  var swapped,
      len = arr.length;

  if(len === 1) return;

  do {
    swapped = false;
    for(var i=1;i<len;i++) {
      if(A[i-1] > A[i]) {   
        var b = A[i];
        A[i] = A[i-1];
        A[i-1] = b;
        swapped = true;
      }   
    }

  }
  while(swapped)  
}

var arr = [1, 6, 9, 5, 3, 4, 2, 12, 4567, 5, 34];
bubbleSort(arr);
document.write(arr);

My solution:

  function bubbleSort(A){
  var swapped,
      len = arr.length;

  if(len === 1) return;

  do {
    swapped = false;
    for(var i=1;i<len;i++) {
      if(A[i-1] > A[i]) {   
        var b = A[i];
        A[i] = A[i-1];
        A[i-1] = b;
        swapped = true;
      }   
    }

  }
  while(swapped)  
}

var arr = [1, 6, 9, 5, 3, 4, 2, 12, 4567, 5, 34];
bubbleSort(arr);
document.write(arr);
↘紸啶 2024-12-12 15:17:44

第二个 for 循环编码错误,应该是

for (var i=0; i<records.length; i++){
    for (var j=0; j<records.length; j++){
        if (parseInt(records[i]) > parseInt(records[j])){
            var temp = records[i];
            records[i] = records[j];
            records[j] = temp;
        }   
    }    
}

the second for loop is coded wrong it should be

for (var i=0; i<records.length; i++){
    for (var j=0; j<records.length; j++){
        if (parseInt(records[i]) > parseInt(records[j])){
            var temp = records[i];
            records[i] = records[j];
            records[j] = temp;
        }   
    }    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文