JS implementation of binary search and fast sorting algorithm

Source: Internet
Author: User

Two-Part search method

function Binary_search (Source_arr, target) {

var len = source_arr.length,

Start = 0,

End = Len-1,

Middle,middle_val;

while (start <= end) {

Middle = parseint ((start + end)/2);

Middle_val = Source_arr[middle];

if (Middle_val = = target) {

return middle;

}else if (Middle_val > target) {

end = Middle-1;

}else{

Start = middle + 1;

}

}

Return-1;

}

Time complexity: 2^x = n so time complexity is log2n, space complexity is n;


Hash table lookup (none)

Quick Sort

function Quick_sort (Source_arr, left, right) {

if (left < right) {

var key = Source_arr[left],

Start = left,

end = right;

while (Start < end) {

while (Start < end && Source_arr[end] > key) {

End--;

}

Source_arr[start] = Source_arr[end];

while (Start < end && Source_arr[start] < key) {

Start + +;

}

Source_arr[end] = Source_arr[start];

}

Source_arr[start] = key;

Quick_sort (Source_arr, left, start-1);

Quick_sort (Source_arr, start+1, right);

}

}

Time Complexity log2n~ n*n (n + n/2 + N/2 + N/4 + N/4 + N/4 + N/4 ...)

B-Tree, binary-tree traversal


JS implementation of binary search and fast sorting algorithm

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.