The JavaScript implementation of bubble sort

Source: Internet
Author: User

1. Common Bubbling Ideas

Suppose there are n numbers, sorted by small to large:

    • For n-1 cycles, each outer loop will be lined up to the maximum number of currently processed numbers, that is, the first outer loop to arrange the maximum number of all numbers, the second outer loop to arrange all the number of the second large number .... Until the first n-1, the number of n-1, which is the second-lowest number, then the rest is the smallest number
    • In the i+1 outer loop, only the number of previous n-i-1 needs to be processed, because the number of i+1 in the back is already lined up. That is, the loop needs to compare the number of previous n-i-1.
    • In the inner loop, compare two adjacent items, then exchange them if the previous item is larger than the next one. Finally, after the internal loop processing the number of the first n-i-1, this n-i-1 number of the largest number in the first row to the n-i-1 position.
Code
function bubbleSort(arr) {  const length = arr.length;  for (let i = 0; i < length - 1; i++) {    let changeOccur = false; //用于标记某次外循环中,是否方式内循环交换事件    for (let j = 0; j < length - i -1; j++) {      if (arr[j] > arr[j+1]) {        /*        const temp = arr[j];         arr[j] = arr[j+1];        arr[j+1] = temp;         */        //这三行的交换函数用ES6来写:        [arr[j], arr[j+1]] = [arr[j+1], arr[j]];        changeOccur = true;      }    }    if (!changeOccur) { //如果一次外循环中,没有发生一次内循环交换,那么可以直接结束排序比较      break;    }  }}
Performance analysis
    • Time complexity: Best O (N), average, worst O (n^2)
    • Space complexity: O (1), stable
2. Cocktail ordering Idea

Two-way bubble sort.

Code
function coaktailBubbleSort(arr) {  const length = arr.length;  let low = 0;  let high = length - 1;    while(low < high) {    let changeOccur = false;    for (let j = low; j < high; j++) {      if(arr[j] > arr[j+1]) {        [arr[j], arr[j+1]] = [arr[j+1], arr[j]];        changeOccur = true;      }    }    if(!changeOccur) {      break;//如果一次交换也没有发生,那直接就可以跳出,结束排序    }    high--;    changeOccur = false;    for (let j = high; j > low; j--) {      if (arr[j] < arr[j-1]) {        [arr[j-1], arr[j]] = [arr[j], arr[j-1]];        changeOccur = true;      }    }    if(!changeOccur) {      break;    }    low++;  }}
Extension: Compare the C language implementation of bubble sort: 1. Normal bubbling
# include<stdio.h>void bubble(int *list,int len){    int i,j,t,flag=0;    for(i=0;i<len-1;i++)    {        flag=0;//设置标记,当某一轮交换没有交换任何数,那下一轮交换也不必进行了        for(j=0;j<len-1-i;j++)        {            if(list[j]>list[j+1])            {                t=list[j];                list[j]=list[j+1];                list[j+1]=t;                flag=1;            }                   }        if(flag==0)        {          break;        }    }}void main(){    int n,list[10];    printf("请输入10个整数:");    for(n=0;n<10;n++)    {        scanf("%d",&list[n]);    }    printf("\n");    bubble(list,10);    for(n=0;n<10;n++)    {        printf("%d\t",list[n]);    }    printf("\n");}
2. Cocktail Bubbles
#include <stdio.h>void cocktailbubble (int *list,int n) {int low=0,high=n-1,j,t,flag; while (Low

The JavaScript implementation of bubble sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.