Selection and sorting of JavaScript sorting algorithms

Source: Internet
Author: User
Selectionsort is a simple and intuitive sorting algorithm. It works as follows. First, find the minimum element in the unordered sequence and store it to the starting position of the sorting sequence. Then, find the minimum element from the remaining unordered elements and put it at the end of the sorting sequence. And so on until all elements are sorted. The switch operation for sorting is between 0 and (n? 1. Select n (n? 1)/2

Selection sort is a simple and intuitive sorting algorithm. It works as follows. First, find the minimum element in the unordered sequence and store it to the starting position of the sorting sequence. Then, find the minimum element from the remaining unordered elements and put it at the end of the sorting sequence. And so on until all elements are sorted.

The switch operation for sorting is between 0 and (n? 1. Select n (n? 1. The value assignment operation for sorting is between 0 and 3 (n? 1.

Number of comparisons O (n ^ 2), the number of comparisons has nothing to do with the initial state of the keyword, the total number of comparisons N = (n-1) + (n-2) +... + 1 = n * (n-1)/2. The number of exchanges O (n). The best case is that it has been ordered and has been exchanged for 0 times. The worst case is reverse order, where it has been exchanged for N-1 times. The number of swapping times is much less than that of bubble sorting. because the CPU time required for switching is much longer than the CPU time required by the comparison, and the n value is small, the selection sorting is faster than bubble sorting.

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.