2012 Shares

# Sorting - standard algorithms?

Topic: Best case scenario for insertion sort time
July 19, 2019 / By Chanelle
Question: I have a big list to sort in VB, which is the fastest method or the best? •Bubble sort •Selection sort •Insertion sort •Quick sort •Merge sort •Heap sort •Preliminary Results •BubbleExit Sort •Comb Sort •Shell Sort •Shaker Sort •Radix Sort •Results •Project Files •Visual Sorting I had a look and Radix seems to be the fastest. I just want to know what type is the best =D any help cheers. the big list i am sorting is String and is to be A to Z style Haha i wasnt sure, thanks =) I knew radix was fast, its the only one i have used in the past you see xD bubbly bubbles yaay fun! i will use that for my program.

## Best Answers: Sorting - standard algorithms?

Anora | 2 days ago
There are no fastest sort method. For a specific sample of data, one type of sorting will always be faster than the other. Generally though, Bubble sort is only good for small number of data that is almost nearly sorted. Its advantage is its working is simple to describe and understand, and is often taught to Algorithm 101. Quicksort is considered one of the fastest sorting algorithms, having an O(n log(n)) performance; however it has a worst case scenario of O(n^2), on certain type of initial arrangements could make it run very slow. Heap sort is also considered as one of the fastest sorting algorithm and it has no worst case scenario. However, it has large overhead, making QuickSort sometimes faster. Radix/bucket sort is lexicographic sort, avoiding the O(n log(n)) speed limit of comparison-based sort (such as Quicksort). However, due to not being a comparison sort, it is not capable of sorting certain sorts of data (pun not intended). Radix sort also have relatively high overhead. In general though, you should just use the built-in sorting library in whatever programming language you uses. Most of the time, writing a your own sorting function is just not worth the slight speed gain; and often half-baked sorting function would just be slower than the generic built-in one. Using built-in sort is also more bug-proof. In general, the fastest sorting algorithm would be one that mixes several algorithms together. Quicksort and Radix sort are fast for sorting large amount of data, but they are recursive sorting, and as the part/bucket gets smaller other sorting methods that have lower overhead is often used. Also, sometimes your problem really is not algorithm. Sometimes it is much easier, faster, and better to use the correct data structure for the problem. With the correct data structure (such as sorted binary tree), sorting often becomes unnecessary.
👍 252 | 👎 2
Did you like the answer? Sorting - standard algorithms? Share with your friends

We found more questions related to the topic: Best case scenario for insertion sort time

Dear Jimbo, (a) Using weights w[1] = 1g, w[2] = 2g, and w[3] = 4g, you can combine them to balance items on the other side of the scale as follows: 1g item versus w[1], 2g item versus w[2], 3g item versus w[2] and w[1], 4g item versus w[3], 5g item versus w[3] and w[1], 6g item versus w[3] and w[2], and 7g item versus w[3], w[2], and w[1]. (b) Notice that the numbers in part (a) are using base-2 (i.e., binary) integer representation. To see this, form a binary number B having three bits ("binary digits"), such that B = [b[3], b[2], b[1]], where each b[i] is either 1 or 0 whenever the corresponding w[i] is on the scale or off of it, respectively. Then the decimal weight on the balance D = 2^2 b[3] + 2^1 b[2] + 2^0 b[1], or more compactly, D = ∑ 2^(i - 1) b[i], for i in {1, 2, 3}, which is equivalent to ∑ w[i] b[i]. Observe how the table below matches up with part (a). D, B - , - - - 1, 001 2, 010 3, 011 4, 100 5, 101 6, 110 7, 111 The binary-based weightings can be extended to weigh arbitrarily heavy items by including additional weights which are successive powers of 2 (i.e., 8g, 16g, 32g, etc.). With eight such weights, the heaviest article that could be weighed is ∑ 2^(i - 1) , for i in {1, 2, 3, . . . , 8} = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255g. So how can eight weights be used to weigh items up to 3280g? The key idea is to realize that it may be possible to balance the scale by putting weights onto both sides, so that weights can be added or subtracted. This makes for an unusual base-3 integer representation known as balanced (or signed) ternary, where each "trit" ("trinary digit") is either -1, 0, or +1. In this case, form a balanced ternary number T having eight trits, such that T = [t[8], t[7], t[6], t[5], t[4], t[3], t[2], t[1]], where each t[i] is 1 when the corresponding w[i] is on the scale opposite the item being weighed, -1 when the corresponding w[i] is on the same side of the scale as the weighed item, and 0 if w[i] is off the scale entirely. The weights themselves are sequential powers of 3, with w[i] = 3^(i - 1), for i in {1, 2, 3, . . . , 8}. That is, the weights are 1g, 3g, 9g, 27g, 81g, 243g, 729g, and 2187g. (c) Notice that this part doesn't require writing code, or that the algorithm be efficient, although that could be done. However, in the present situation, I think descriptive language is easier to understand. Step 1. Input integer N, with 1 ≤ N ≤ 3280, and initialize w[i] = 3^(i - 1), for i in {1, 2, 3, . . . , 8}. For convenience, define cumulative weights W[i] = ∑ w[k], for k in {1, . . . , i}, and i in {1, 2, 3, . . . , 8}. That is, W[i] is the sum of the lightest i weights. Step 2. Search W to find the smallest m such that N ≤ W[m]. This m is indicates the most-significant trit of T, meaning that w[i] = 0 for all i > m, and that w[m] = 1. Practically speaking, this is equivalent to placing weights opposite the article, one at a time starting with the lightest, until the weights on the scale total at least N. Step 3. Systematically check all weight combinations of w[i] and t[i], for all i < m, until finding N = w[m] + ∑ w[i] t[i]. One way to systematically examine all weight combinations is to make nested loops. The outermost loop would vary t[m - 1] over {-1, 0, 1}, within which the next loop would vary t[m - 2] over {-1, 0, 1}, and so forth, with the innermost loop varying t[1] over {-1, 0, 1}. If the weights are placed on the scale in the order and positions which match these loops, then sum w[m] + ∑ w[i] t[i] will increment by 1 for each step through the loops. As a consequence, to find any particular N, start with m = 1 to find N = 1, then move on to m = 2 and loop over t[1] equalling -1, 0, and +1 to get the weights needed for N = 2, N = 3, and N = 4, respectively. Continue this process using m = 3, m = 4, m = 5, . . . , and m = 8, in sequence, which generates the rest of the integers in sequence, finally reaching ∑ w[i] = ∑ 3^(i - 1) = 3280g. Note that the trinary-based weightings can be extended to weight arbitrarily heavy items, similar to the like observation for binary-based weightings, given that additional weights are successively higher powers of 3. Also, as stated above, this algorithm isn't intended to minimize the number of weighings needed to find an article's weight. It should be clear that a cleverer approach would be to choose the combination of weights on each weighing so as to eliminate multiple possibilities at once, rather than checking sequentially. That might be preferred for a practical implementation, but the complexity would likely interfere with understanding the basic principles of the algorithm above. As a practical issue, real items will usually not weigh precise integer amounts, so on a real scale a small sliding weight to allow shifting up to half an ounce in either direction could bring the scale into balance.