Analysis of two basic common sorting algorithms in JavaScript

Source: Internet
Author: User

Note: Most of the content is copied from the Internet, the code for their own handwriting. Only the restudying of knowledge is not original.

1. Bubble sort (Bubble sort)

(1) Algorithm description

Bubble sort is a simple sort algorithm. It repeatedly visits the sequence to sort, compares two elements at a time, and swaps them if they are in the wrong order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The algorithm is named because the smaller elements will slowly "float" through the switch to the top of the sequence.

(2) algorithm description and implementation

The specific algorithm is described as follows:

    • <1> compare adjacent elements. If the first one is larger than the second, swap them two;
    • <2> For each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair, so that the final element should be the largest number;
    • <3> Repeat the above steps for all elements except the last one;
    • <4>: Repeat steps until the sort is complete.

JavaScript code implementation:

Improved bubble sorting: Sets a flag variable pos, which records the last swap position in each order. Since the POS location after the record has been exchanged in place, so in the next sequencing as long as the scan to POS location.

The improved algorithm is as follows:

The traditional bubble sort operation can only find a maximum or minimum value, we consider using the method of forward and reverse two-pass bubbling in each order to get two final values at a time (maximum and minimum), thus reducing the number of sort passes by almost half.

The improved algorithm is:

The three algorithms run at the following times:

By running the results you can see that the complexity of the time is lower, and it takes much shorter. You can try it yourself, it is best to run the three algorithms written in a file, otherwise it will cause errors due to browser and other reasons.

Dynamic diagram of bubbling sort demo:

(3) algorithm analysis

    • Best case: T (n) = O (n)

When the input data is already in the positive order

    • Worst case: T (n) = O (n2)

When the input data is in reverse order

    • Average condition: T (n) = O (n2)

2. Select sort (Selection sort)

One of the most stable sorting algorithms, because no matter what data goes in is the time complexity of O (n²) ... so when it's used, the smaller the data, the better. The only advantage might be that you don't take up extra memory space. In theory, choosing a sort might also be the most common sort of sorting method that people usually think of.

(1) Introduction to Algorithms

Select Sort (selection-sort) is a simple and intuitive sorting algorithm. It works by first finding the smallest (large) element in the unordered sequence, holding it to the starting position of the sort sequence, and then continuing to find the smallest (large) element from the remaining unsorted elements, and then dropping it to the end of the sorted sequence. And so on until all elements are sorted.

(2) algorithm description and implementation

The direct selection of n records can be sorted by n-1 direct selection to get an ordered result. The specific algorithm is described as follows:

    • <1> Initial state: Unordered area is R[1..N], ordered area is empty;
    • <2>. At the beginning of the first order (i=1,2,3...n-1), the current ordered and unordered regions are r[1..i-1] and R (I.) respectively. N). The sequencing from the current unordered area-Select the smallest key record r[k], and the 1th record of the unordered zone R Exchange, so that r[1..i] and R[I+1..N) respectively to increase the number of records added to a new ordered area and the number of records reduced by 1 new unordered area;
    • <3>.n-1 the end of the trip, the array is ordered.

JavaScript code implementation:

(3) algorithm analysis

    • Best case: T (n) = O (n2)
    • Worst case: T (n) = O (n2)
    • Average condition: T (n) = O (n2)

Analysis of two basic common sorting algorithms in JavaScript

Related Article

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.