JS in bubble sort, select sort, quick sort

Source: Internet
Author: User

var arr = [1,4,2,9,7,6,5,4,7,5]; //Bubble Sort (popularly speaking is J and J+1 Dozen, who wins who goes back)Forvar i = 1;i<arr.length; i++) {for (var j = 0;j<arr.length-i; j + +) {if (arr[j]>arr[j+1]) {var temp = arr[j];                ARR[J] = arr[j+1];            ARR[J+1] = temp; }}} console.log (arr);

//Select sort (popularly speaking is J and I dozen, who wins who goes ahead)Forvar i = 0;i<arr.length-1; i++) {for (var j = i+1;j<arr.length; j + +) {if (Arr[i]>arr[j]) {var temp = arr[j];                ARR[J] = Arr[i];            Arr[i] = temp; }}} console.log (arr);


//FastQuick Sort:Take the value of the middle of the array, and then compare the other values with the value, greater than the join right array, less than the left array, recursive call function Quick (arr) {//recursive export conditionsif (arr.length<=1) {return arr;}//Get intermediate subscriptvar centerindex = Arr.length%2==0?arr.length/2: (arr.length+1)/2;//Get the value corresponding to the intermediate subscriptvar centerelement = Arr[centerindex];//Create left and right arraysvar left = []; var right = [];//cyclic judgment, if the element is greater than the middle of the table corresponding to the value of adding the left array, and vice versa to join the right arrayfor (var i = 0;i<arr.length;i++) {if ((I!=centerindex) && (arr[i]>=centerelement)) {Right.push (arr[i]); } if ((I!=centerindex) && (arr[i]<centerelement)) {Left.push (arr[i]); }        }//Recursive call to this functionreturn Quick (left). Concat (centerelement). Concat (quick);} console.log (Quick (arr));

JS in bubble sort, select sort, quick 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.