Group5 Radixsort-Combsort
Group5 Radixsort-Combsort
Introduction
Radix Sort Algorithm is a unique sorting algorithm that works on the basic principle
of numbers being an ensemble of digits. Radix Sort works only on integer values
since integers have only a single mathematical component or digits. Radix sorts the
elements by first grouping the individual digits of the same place value. Then, sort the
elements according to their increasing/decreasing order. In mathematics, radix refers
to the base, the number of unique digits, where decimal is known as base 10. 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.
In this sorting algorithm, the numbers are initially arranged according to their least
significant digit, moving onto their most significant digit, while maintaining the
previous order. We have to find the largest element from the given array. Suppose 'x'
be the number of digits in max. The 'x' is calculated because we need to go through
the significant places of all elements. After that, go through one by one each
significant place. Here, we have to use any stable sorting algorithm to sort the digits
of each significant place. The algorithm of radix sort have different passes and it starts
at 0's place up to the significant places like 10th, 100th... and so on and so forth.
Step-by-step Procedure
STEP 1 Find the largest element in the array, i.e. max. Let x be the number of digits
in max. X is calculated because we have to go through all the significant places of all
elements.
STEP 2 Now, go through each significant place one by one. Use any stable sorting
technique to sort the digits at each significant place. We have used counting sort for
this.
STEP 4 Finally, sort the elements based on the digits at hundreds place.
#include <stdio.h>
int main() {
int arr[10];
int i, num;
radix_sort(&arr[0], num);
return 0;
}
Output:
https://www.programiz.com/dsa/radix-sort
https://www.javatpoint.com/radix-sort
https://www.alphacodingskills.com/php/pages/php-program-for-radix-sort.php
COMB SORT
Introduction
Comb Sort is the advanced form of bubble Sort. Bubble Sort compares all the
adjacent values while comb sort removes all the turtle values or small values near the
end of the list.
The shrink factor is found to be 1.3 by testing comb sort on 200,000 random lists.
Comb sort works better than the bubble sort, but its time complexity in average case
and worst case remains O(n2).
1. Time Complexity
o Best Case Complexity - It occurs when there is no sorting required, i.e. the
array is already sorted. The best-case time complexity of comb sort is θ(n log
n).
o Average Case Complexity - It occurs when the array elements are in jumbled
order that is not properly ascending and not properly descending. The average
case time complexity of comb sort is Ω(n2/2p), where p is a number of
increments..
o Worst Case Complexity - It occurs when the array elements are required to
be sorted in reverse order. That means suppose you have to sort the array
elements in ascending order, but its elements are in descending order. The
worst-case time complexity of comb sort is O(n2).
2. Space Complexity
Step-by-step Procedure
STEP 1 START
STEP 2 Calculate the gap value if gap value==1 go to step 5 else go to step 3
STEP 3 Iterate over data set and compare each item with gap item then go to step 4.
STEP 6 STOP
Now, initialize
n = 8 // size of array
gap = n
shrink = 1.3
swapped = true
First iteration
swapped = false
This iteration ends here, because at i =2, the value of i + gap = 2 + 6 = 8, and there is
no element at 8th position of the array. So, after first iteration, the elements of array
will be –
Second iteration
swapped = false
This iteration ends here, because at i =4, the value of i + gap = 4 + 4 = 8, and there is
no element at 8th position of the array. So, after second iteration, the elements of array
will be –
Third iteration
swapped = false
This iteration ends here, because at i =5, the value of i + gap = 5 + 3 = 8, and there is
no element at 8th position of the array. So, after third iteration, the elements of array
will be –
Fourth iteration
swapped = false
This iteration ends here, because at i =6, the value of i + gap = 6 + 2 = 8, and there is
no element at 8th position of the array. So, after fourth iteration, the elements of array
will be -
Now, move to iteration 5.
Fifth iteration
swapped = false
Hence, the iterations end here, and now the sorting is completed. Now, the final sorted
array is
Sample code:
#include <stdio.h>
#include<math.h>
int updatedGap(int gap)
{
// Shrink gap by Shrink factor
gap = floor(gap/1.3);
if (gap < 1)
return 1;
return gap;
}
// Function to sort array elements using Comb Sort
void combSort(int a[], int n)
{
int gap = n; /* Initialize gap size equal to the size of array */
int swapped = 1;
while (gap != 1 || swapped == 1)
{
gap = updatedGap(gap); // find updated gap
// Initialize swapped as false so that we can
// check if swap happened or not
swapped = 0;
for (int i = 0; i < n-gap; i++) /* Compare all elements with current
gap */
{
if (a[i] > a[i+gap]) //swap a[i] with a[i+gap]
{
int temp = a[i];
a[i] = a[i+gap];
a[i+gap] = temp;
swapped = 1;
}
}
}
}
void printArr(int a[], int n) /* function to print array elements */
{
for (int i=0; i<n; i++)
printf("%d ", a[i]);
}
int main()
{
int a[] = {49, 11, 24, 44, 29, 27, 2};
int n = sizeof(a)/sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
combSort(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
Output:
https://www.javatpoint.com/comb-sort