Algorithmic Tours | Select Sort method

Source: Internet
Author: User
Tags sorts

Algorithmic Tours | Select Sort method

HTML5 Academy-code Carpenter: The fast calculation and sequencing of data is directly related to the performance of the front page. Since there are many algorithms for sorting, in this "algorithm series" sharing, we first start with a simple and easy to choose the sorting method, the other sorting algorithm will be followed to share with you.

What is the basic concept algorithm of the algorithm and what does it do?

The methods and steps taken to solve a problem are called algorithms.

We can consider the algorithm as a "FU word paper-cut tutorial", wherein each algorithm is a paper-cut tutorial in a "fixed step" of the paper-cut method, users just follow the steps to cut paper, you can cut out good luck.

There are so many algorithms, the efficiency of different algorithms to solve the problem is different, suitable for different scenarios. As the scale of the problem increases, the gap between the algorithms can become inaccessible. Increasing the efficiency of problem solving depends not only on the choice of fast hardware, but also on the selection of an effective (suitable) algorithm.

Usage Scenarios for sorting

Sorts from large to small (or small to large) for the array. For example: in the management system according to the rank of the score, according to the reading quantity to the article sorting and so on.

The fast calculation and sequencing of data is directly related to the performance of the front page. (for example, there are 10,000 of the data on the page need to be sorted by JS, the time difference between different algorithms is very large, directly affect the user experience of the site)

Common sorting methods

The more common sort methods include: bubble sort, selection sort, quick sort, binary insertion sort, etc.

Since there are many algorithms for sorting, in this "algorithm series" sharing, we first start with a simple and easy to choose the sorting method, the other sorting algorithm will be followed to share with you.

The basic principle of choosing the sorting method

The smallest number in the sequence is first found, and it is swapped with the first number in the sequence;

Next, continue this operation in the remaining sequence: find the smallest number, and swap it with the second number in the sequence;

And so on, until the complete sequence is sorted.

In short, select Sort is----continuously selects the smallest person in the remaining sequence, and then exchanges the position with the "first" digit of the unsorted column.

Case description

In the following array, the black stands for sorting, and the blue represents the sorted

To implement the selection sort step decomposition sort count

Sort count: The sequence length –1 (note, not the number of comparisons);

Because the last number in the sequence does not need to be compared again, the number of sorts is the sequence length –1.

Find the smallest number

The smallest number is found in the sequence and the index value of the number is recorded;

Since Minindex starts at 0 by default, the first number does not need to be compared to itself, so j = i + 1;

    1. Traverse the sequence to find the smallest number
    2. for (var j = i + 1; j < Len; J + +) {
    3. if (Arr[j] < Arr[minindex]) {
    4. Index of minimum number of records
    5. Minindex = j;
    6. };
    7. };

Multiple traversal within a sort number finds the smallest number, so you need to use a For statement to control it.

    1. Number of orders
    2. for (var i = 0; i < len-1; i++) {
    3. The index of the default minimum number is I
    4. Minindex = i;
    5. Traverse the sequence to find the smallest number
    6. for (var j = i + 1; j < Len; J + +) {
    7. if (Arr[j] < Arr[minindex]) {
    8. Index of minimum number of records
    9. Minindex = j;
    10. };
    11. };
    12. };
Two-digit exchange position

Using the TEMP variable, the exchange of values between the two array elements is realized, i.e. the interaction position.

    1. temp = Arr[i];
    2. Arr[i] = Arr[minindex];
    3. Arr[minindex] = temp;
Select the sorting method complete code
  1. var arr = [9, 8, 3, 1, 2, 4],
  2. Len = Arr.length,
  3. Minindex, temp;
  4. Number of orders
  5. HTML5 Academy
  6. for (var i = 0; i < len-1; i++) {
  7. The index of the default minimum number is I
  8. Minindex = i;
  9. Traverse the remainder of the sequence to find the smallest number
  10. for (var j = i + 1; j < Len; J + +) {
  11. if (Arr[j] < Arr[minindex]) {
  12. Index of minimum number of records
  13. Minindex = j;
  14. };
  15. };
  16. HTML5 Academy Productions
  17. Two-digit interaction location
  18. temp = Arr[i];
  19. Arr[i] = Arr[minindex];
  20. Arr[minindex] = temp;
  21. };
  22. Console.log (arr);
The basic concept of the complexity of the efficiency algorithm of choosing the sorting method

The complexity of the algorithm is divided into time complexity and space complexity (time and space are the most important resources of computer, so the complexity is divided into time and space).

Time complexity: Refers to the computational effort required to perform the algorithm;

Spatial complexity: Refers to the amount of memory space required to execute the algorithm.

Complexity of Time: O (n*n)

Time complexity is the item (without coefficients) that is most affected by the change of n in the expression of total operation number.

The first cycle compares n-1 times, then n-2 times, n-3 times, and so on, the last cycle compared 1 times, the total number of comparisons and for (n-1 + 1) * N/2, that is, the time complexity of the comparison operation is O (n^2)

Tips: The number of comparisons selected for sorting is independent of the initial ordering of the sequence.

Space complexity: O (1)

The sorting algorithm requires an extra space (the TEMP variable) to swap the position of the element.

An algorithm for unstable ordering

Select Sort is an unstable sort algorithm.

For example: Sequence [3, 8, 3, 1, 9], the first loop 1th element 3 will and 1 exchange, become [1, 8, 3, 3, 9], at this time, the original sequence in the order of two 3 is destroyed.

Algorithmic Tours | Select Sort method

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.