Sie sind auf Seite 1von 2

c 

c  is a sorting algorithm that is a generalization of insertion sort, with two observations:
 insertion sort is efficient if the input is "almost sorted", and
 insertion sort is typically inefficient because it moves values just one position at a time.


Implementation
he original implementation performs O(›2) comparisons and exchanges in the worst case. A minor change given in V. Pratt's book[3] improved
the bound to O(› log2 ›). his is worse than the optimal comparison sorts, which are O(› log ›).
Shell sort improves insertion sort by comparing elements separated by a gap of several positions. his lets an element take "bigger steps" toward
its expected position. Multiple passes over the data are taken with smaller and smaller gap sizes. he last step of Shell sort is a plain insertion
sort, but by then, the array of data is guaranteed to be almost sorted.
Consider a small value that is initially stored in the wrong end of the array. Using an O(›2) sort such as bubble sort or insertion sort, it will take
roughly › comparisons and exchanges to move this value all the way to the other end of the array. Shell sort first moves values using giant step
sizes, so a small value will move a long way towards its final position, with just a few comparisons and exchanges.
One can visualize Shell sort in the following way: arrange the list into a table and sort the columns (using an insertion sort). Repeat this process,
each time with smaller number of longer columns. At the end, the table has only one column. While transforming the list into a table makes it
easier to visualize, the algorithm itself does its sorting in-place (by incrementing the index by the step size, i.e. using m  m instead
of m).
For example, consider a list of numbers like

        
 . If we started with a
step-size of 5, we could visualize this as breaking the list of numbers into a table with 5 columns. his would look like this:



   
   


We then sort each column, which gives us


  


   

When read back as a single list of numbers, we get

  
      . Here, the 10
which was all the way at the end, has moved all the way to the beginning. his list is then again sorted using a 3-gap sort, and then 1-gap sort
(simple insertion sort). then we can split the list as 3-step sequence



 


 
 


º 
In computer science,   is a sorting algorithm that sorts integers by processing individual digits. Because integers can represent strings of
characters (e.g., names or dates) and specially formatted floating point numbers, radix sort is not limited to integers. Radix sort dates back as far
as 1887 to the work of Herman Hollerith on tabulating machines.[1]
Most digital computers internally represent all of their data as electronic representations of binary numbers, so processing the digits of integer
representations by groups of binary digit representations is most convenient. wo classifications of radix sorts are least significant digit (LSD)
radix sorts and most significant digit (MSD) radix sorts. LSD radix sorts process the integer representations starting from the least significant
digit and move towards the most significant digit. MSD radix sorts work the other way around.
1. ake the least significant digit (or group of bits, both being examples of radices) of each key.
2. Group the keys based on that digit, but otherwise keep the original order of keys. (his is what makes the LSD radix sort a stable sort).
3. Repeat the grouping process with each more significant digit.
he sort in step 2 is usually done using bucket sort or counting sort, which are efficient in this case since there are usually only a small number of
digits.

0
 
1. Original, unsorted list:
170, 45, 75, 90, 2, 24, 802, 66
1. Sorting by least significant digit (1s place) gives:
170, 90, 2, 802, 24, 45, 75, 66
Notice that we keep 2 before 802, because 2 occurred before 802 in the original list, and similarly for pairs 170 & 90 and 45 & 75.
1. Sorting by next digit (10s place) gives:
2, 802, 24, 45, 66, 170, 75, 90
1. Sorting by most significant digit (100s place) gives:
2, 24, 45, 66, 75, 90, 170, 802 It is important to realize that each of the above steps requires just a single pass over the data, since each item can
be placed in its correct bucket without having to be compared with other items.
Some LSD radix sort implementations allocate space for buckets by first counting the number of keys that belong in each bucket before moving
keys into those buckets. he number of times that each digit occurs is stored in an array. Consider the previous list of keys viewed in a different
way:
170, 045, 075, 090, 002, 024, 802, 066
he first counting pass starts on the least significant digit of each key, producing an array of bucket sizes:
2 (bucket size for digits of 0: 170, 090)
2 (bucket size for digits of 2: 002, 802)
1 (bucket size for digits of 4: 024)
2 (bucket size for digits of 5: 045, 075)
1 (bucket size for digits of 6: 066)
A second counting pass on the next more significant digit of each key will produce an array of bucket sizes:
2 (bucket size for digits of 0: 002, 802)
1 (bucket size for digits of 2: 024)
1 (bucket size for digits of 4: 045)
1 (bucket size for digits of 6: 066)
2 (bucket size for digits of 7: 170, 075)
1 (bucket size for digits of 9: 090)
A third and final counting pass on the most significant digit of each key will produce an array of bucket sizes:
6 (bucket size for digits of 0: 002, 024, 045, 066, 075, 090)
1 (bucket size for digits of 1: 170)
1 (bucket size for digits of 8: 802)
At least one LSD radix sort implementation now counts the number of times that each digit occurs in each column for all columns in a single
counting pass. (See the external links section.) Other LSD radix sort implementations allocate space for buckets dynamically as the space is
needed.
 
 
  
A simple version of an LSD radix sort can be achieved using queues as buckets. he following process is repeated for a number of times equal to
the length of the longest key:
1. he integers are enqueued into an array of ten separate queues based on their digits from right to left. Computers often represent
integers internally as fixed-length binary digits. Here, we will do something analogous with fixed-length decimal digits. So, using the
numbers from the previous example, the queues for the 1st pass would be:
0: 170, 090
1: none
2: 002, 802
3: none
4: 024
5: 045, 075
6: 066
7 - 9: none
2. he queues are dequeued back into an array of integers, in increasing order. Using the same numbers, the array will look like this after
the first pass:
170, 090, 002, 802, 024, 045, 075, 066
3. For the second pass:
Queues:
0: 002, 802
1: none
2: 024
3: none
4: 045
5: none
6: 066
7: 170, 075
8: none
9: 090
Array:
002, 802, 024, 045, 066, 170, 075, 090
(note that at this point only 802 and 170 are out of order)
4. For the third pass:
Queues:
0: 002, 024, 045, 066, 075, 090
1: 170
2 - 7: none
8: 802
9: none
Array:
002, 024, 045, 066, 075, 090, 170, 802 (sorted)
While this may not be the most efficient radix sort algorithm, it is relatively simple, and still quite efficient.

Das könnte Ihnen auch gefallen