Tips for fast sorting

Source: Internet
Author: User

The recently designed code has such a requirement that there are a bunch of strings, such:

A [1], a [3], a [5], a [2]

(Assume that the prefix is A, and the values in the brackets are not repeated), use a program to combine all the arrays that can be merged and convert them:

A [1 .. 3], a [5]

The method I came up with is to extract the numbers in brackets, put them in a list, and then sort the list. That is to say, after sorting the above data, it becomes:

1, 2, 3, 5

Then, traverse the array from the beginning to the end to know which values are continuous.

After the design, the coding work was handed over to other colleagues. In order to quickly implement the Code function, he used Bubble sorting at the time. Later, he was busy with the work and never changed the code until recently, the user raised a question about the performance of this module (a lot slower than the previous version), so we can review this code. I think there is no problem with the design, you only need to change the Bubble sorting to quick sorting to greatly increase the speed. I am planning to do this. PM has come to understand our work, next, a simple and fast sorting method is provided:

Since the values to be sorted are unique and all are simple numbers, the method of returning the speed in space: first create an array that is large enough to clear it, when a value is read from the list, this bit of the array is set to 1, then according to the above data, our array will eventually become:

-----------------------------------

| 1 | 1 | 1 | 0 | 1 | ......

-----------------------------------

At this time, traverse the array and judge the status of 0 and 1 to conveniently know which values are continuous.

In addition, when we extract a value from a string such as a [1] in advance, we can know the values of all characters in advance. After determining the minimum and maximum values, you can allocate only enough memory, instead of a very large memory.

Second, because you only need to use the status to indicate whether a certain bit of the array is occupied, you can use a single bit to represent the status, then a byte, it can be used to indicate whether the first eight values are occupied, thus saving the memory to 1/8.

After the above algorithm is used, no sorting work is required. The quantify test shows that the execution time of this process in the entire module is changed from 30% to 2%, an amazing algorithm. (Sometimes the simpler the code, the more effective it is, but I did not think of it, Ginger is still old and hot ).

Graph Software:

Http://www.tonixsoft.com

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.