0% found this document useful (0 votes)
62 views12 pages

Group5 Radixsort-Combsort

Radix sort is a sorting algorithm that works by sorting the elements based on their individual digits in a specific place value. It requires multiple passes where each pass sorts the elements based on a different digit place value, from least to most significant. Comb sort is an improvement on bubble sort that uses a gap size greater than 1 to compare elements, with the gap shrinking on each pass until it reaches 1. Both algorithms have a worst case time complexity of O(n2) but radix sort has better performance on pre-sorted or partially sorted data.

Uploaded by

Mariebeth Seno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views12 pages

Group5 Radixsort-Combsort

Radix sort is a sorting algorithm that works by sorting the elements based on their individual digits in a specific place value. It requires multiple passes where each pass sorts the elements based on a different digit place value, from least to most significant. Comb sort is an improvement on bubble sort that uses a gap size greater than 1 to compare elements, with the gap shrinking on each pass until it reaches 1. Both algorithms have a worst case time complexity of O(n2) but radix sort has better performance on pre-sorted or partially sorted data.

Uploaded by

Mariebeth Seno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

RADIX SORT

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.

Complexity of Radix Sort

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 3 Sort the elements based on digits at tens place

STEP 4 Finally, sort the elements based on the digits at hundreds place.

STEP 5 Print the sorted array.

Let the elements of array are –


FLOW CHART:
Sample code:

#include <stdio.h>

int print(int *a, int n) {


int i;
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
}

void radix_sort(int *a, int n) {


int i, b[10], m = 0, exp = 1;
for (i = 0; i < n; i++) {
if (a[i] > m)
m = a[i];
}

while (m / exp > 0) {


int box[10] = { 0 };
for (i = 0; i < n; i++)
box[a[i] / exp % 10]++;
for (i = 1; i < 10; i++)
box[i] += box[i - 1];
for (i = n - 1; i >= 0; i--)
b[--box[a[i] / exp % 10]] = a[i];
for (i = 0; i < n; i++)
a[i] = b[i];
exp *= 10;
}
}

int main() {
int arr[10];
int i, num;

printf("Input number of elements: ");


scanf("%d", &num);

printf("\nInput array elements one by one : ");


for (i = 0; i < num; i++)
scanf("%d", &arr[i]);

printf("\nArray elements : ");


print(&arr[0], num);

radix_sort(&arr[0], num);

printf("\nSorted elements : ");


print(&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.

It is a comparison-based sorting algorithm that is mainly an improvement in bubble


sort. In bubble sort, there is a comparison between the adjacent elements to sort the
given array. So, in bubble sort, the gap size between the elements that are compared is
1. Comb sort improves the bubble sort by using a gap of size more than 1. The gap in
the comb sort starts with the larger value and then shrinks by a factor of 1.3. It means
that after the completion of each phase, the gap is divided by the shrink factor 1.3.
The iteration continues until the gap is 1.

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).

Complexity of Comb Sort

1. Time Complexity

Case Time Complexity


Best Case θ(n log n)
Average Case Ω(n2/2p) where p is a number of increments.
Worst Case O(n2)

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

Space Complexity O(1)


Stable YES

o The space complexity of comb sort is O(1).

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 4 Swap the element if require else go to step 2

STEP 5 Print the sorted array.

STEP 6 STOP

Let the elements of array are –

Now, initialize

n = 8 // size of array
gap = n
shrink = 1.3
swapped = true

First iteration

gap = floor(gap/shrink) = floor(8/1.3) = 6

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 –

Now, move to iteration 2.

Second iteration

gap = floor(gap/shrink) = floor(6/1.3) = 4

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 –

Now, move to iteration 3.

Third iteration

gap = floor(gap/shrink) = floor(4/1.3) = 3

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 –

Now, move to iteration 4.

Fourth iteration

gap = floor(gap/shrink) = floor(3/1.3) = 2

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

gap = floor(gap/shrink) = floor(2/1.3) = 1

swapped = false

After the fifth iteration, the sorted array is –

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

You might also like