Sei sulla pagina 1di 6

Jerico S.

Garcia 2-E

Radix Sort
Radix sort is one of the sorting algorithms used to sort a list of integer numbers
in order. In radix sort algorithm, a list of integer numbers will be sorted based
on the digits of individual numbers. Sorting is performed from least significant
digit to the most significant digit.

Radix sort algorithm requires the number of passes which are equal to the
number of digits present in the largest number among the list of numbers. For
example, if the largest number is a 3-digit number then that list is sorted with
3 passes.

Consider the following list of unsorted integer number


62, 841, 230, 10, 454, 90, 55, 65
Define 10 queues each representing a bucket for each digit from 0 to 9.

Queue-0 Queue-1 Queue-2 Queue-3 Queue-4 Queue-5 Queue-6 Queue-7 Queue-8 Queue-9

Insert all the number of the number of the list into respective queue based on
the least significant digit (once place digit) of every number.
62, 841, 230, 10, 454, 90, 55, 65

90
10 65
230 841 62 454 55
Queue-0 Queue-1 Queue-2 Queue-3 Queue-4 Queue-5 Queue-6 Queue-7 Queue-8 Queue-9

Group all the numbers from queue-0 to queue-9 in the order they have
inserted and consider the list for step as input list.
230, 10, 90, 841, 62, 454, 55, 65
Insert each number into their respective queue based on the least significant
digit. (Tens placed digit)
230, 10, 90, 841, 62, 454, 55, 65

55 65
10 230 841 454 62 90
Queue-0 Queue-1 Queue-2 Queue-3 Queue-4 Queue-5 Queue-6 Queue-7 Queue-8 Queue-9
vc cv
vc cv
Group all the numbers from queue-0 to queue-9 in the order they have
vc
inserted and consider the list for step as input list. CV
v
10, 230, 841, 454, 55, 62, 65, 90
Insert each number into their respective queue based on the least significant
digit. (Hundred placed digit)
10, 230, 841, 454, 55, 62, 65, 90

90
65
62
55
cv
10
cv 230 454 841

CV Queue-1 Queue-2 Queue-3 Queue-4 Queue-5 Queue-6 Queue-7 Queue-8 Queue-9


Queue-0
vc
vc
Group all vc
the numbers from queue-0 to queue-9 in the order they have
inserted and
v consider the list for step as input list.

10, 55, 62, 65, 90, 230, 454, 841


List got sorted in the increasing order.

Real life Example of Radix Sort


Back in the era of punched cards, radix sorting was used for card sorting. To
short on a field, you’d begin at the last column and run the cards through the
machine. They would land in different stacks according to their Hollerith code
value in that column. You’d then remove the stacks in order and put them
back into the hopper for the next pass. Repeat until all column were sorted.
Merge sort
Merge sort is an efficient, general-purpose, comparison-based sorting
algorithm. Most implementation produce a stable sort, which means that the
implementation preserve the input order of equal elements in the sorted
output.
Given Array: [6, 4, 5, 1, 2, 7, 3]

First, as per step 1 above, we divide the array into 2 parts. As we can see, the
following are the subarrays left half and right half:
Left half: [6, 4, 5, 1]
Right half: [2, 7, 3]

Then, as per step 2 above, we recursively sort the left and right halves. Here is
how the sorted subarrays will look like:
Recursively sorted left half: [1, 4, 5, 6]
Recursively sorted right half: [2, 3, 7]

Finally, as per step 3, we will merge these 2 halves to create the final sorted
array. Final merged and sorted array: [1, 2, 3, 4, 5, 6, 7]

The left and the right halves can always be sorted recursively using the same
algorithm. The magic happens in creating the final merged and sorted array.
So, let us understand it well using the above example.

In the above example, we are given 2 arrays [1, 4, 5, 6] and [2, 3, 7]. We are
supposed to merge these 2 arrays into a single sorted array. Let us place a
pointer at the head of each array. We will depict the pointer by underlining the
corresponding element where the pointer points to.
Final merged array = []
Left array: [1, 4, 5, 6]
Right array: [2, 3, 7]

As can be seen, the pointer of the left array is at 1 and the pointer of the right
array is at 2. We pick the smaller one and put it in the final merged array and
move the corresponding pointer. After doing this, we will have the following
state:
Final merged array = [1]
Left array: [4, 5, 6]
Right array: [2, 3, 7]
Here the pointers are now at 4 and 2 respectively. We again do what we did
above – pick the smaller one and put it in the final merged array and move the
corresponding pointer. We will get the following:
Final merged array = [1, 2]
Left array: [4, 5, 6]
Right array: [3, 7]
We repeat this again to get:
Final merged array = [1, 2, 3]
Left array: [4, 5, 6]
Right array: [7]
Continuing this exercise, we can see that we are successfully able to get the
final merged array in the sorted form:
Final merged array = [1, 2, 3, 4, 5, 6, 7]
Left array: []
Right array: []

So, as can be seen, we started with an unsorted array and we were successfully
able to get a sorted array. Another question that is to be answered – how were
the left and the right arrays sorted? Well, we sorted them recursively using the
same technique as above. For instance, consider the right array: [2, 7, 3]. To
sort it, we will break it again into 2 sub-arrays: [2, 7] and [3]. Both these sub-
arrays are already sorted so we can simply merge them by using the technique
explained above to get the sorted array [2, 3, 7].

Take a look at the following image to understand how this same procedure is
recursively applied on the subarrays:

Real Life Example of Merge Sort

Let me give an example of Merge Sort from my real life. I have two sets of
graded papers from the same class and both sets are alphabetized. If I want to
merge the two piles into one, I don't have to start all over again, I can use the
work that's already been down alphabetizing them separately. The first paper
on the merged list has to be either the top paper from list #1 or the top from
list #2. I take that paper and put it on a new stack face down. Now I make the
comparison between the new two papers on top of the stacks and put it face
down on the new stack, again and again, until all the papers are on the new
stack face down. Once one of the original stacks is depleted, there are no more
comparisons to be made and I can put the rest of the remaining stack face
down on top of the merged stack, turn it back face up and it is completely
sorted.
Selection Sort

Selection sort is sorting algorithm, works on the principle of FIND-minimum (or


maximum) method. A pass through the array is made to find the minimum
element. Once it is found, place it on the first position of the array. To find
second smallest element the entire array scanned again, which is placed at the
second position. Repeat this process for remaining elements in the array. Once
second last element compared with last element, the whole array is sorted in
ascending order.

Example:

8 5 4 3 2

2 5 4 3 8

2 3 4 5 8

2 3 4 5 8

Consider the above example of the selection sort, initially, the first element
assumed to be minimum element is 8.

Now 8 is compared with 5,4,3 and 2 and since 2 is a minimum element in the
list therefore 8 is replaced with element 2.

After one iteration, indexing starts from the second element and compare with
remaining unsorted element in the list. Now element 5 will be compared with
4,3,8 and replace with 3 as a minimum element. Element 4 will be compared
with 5,8 and since 4 is already the smaller element compared to 5 and 8, no
need to replace the element. this loop continues till the last element.

and finally, the sorted elements are 2,3,4,5,8.

Real Life Example of Selection Sort

Consider you have 8 shoes pairs with sizes 6 to 13. You find them not in order.
Their arrangement from smallest to largest will be an example of selection sort.
This is an easy one example.

Now consider another example if you have 200 glasses of different volume 100
ml, 110 ml, 120 ml, 130 ml, …., 2080 ml and 2090 ml. You find them spread
on a room floor and want to place them such that the 2090 glass, then 2080
glass inside it, then 2070 glass inside it and so on. This sort method is
inefficient on large lists because searching for the exact size will take a lot of
time but this is the way selection sort works.

Potrebbero piacerti anche