etutorialspoint
  • Home
  • PHP
  • MySQL
  • MongoDB
  • HTML
  • Javascript
  • Node.js
  • Express.js
  • Python
  • Jquery
  • R
  • Kotlin
  • DS
  • Blogs
  • Theory of Computation

Radix sort program in C

In this post, you will learn how to write a radix sort program in the C programming language.

Radix sort is a linear sorting algorithm that is also called bucket sort. It uses the concept of sorting names into alphabetical order.

The Quick Sort, Merge Sort, Heap Sort, and Selection Sort are comparison-based algorithms. But the radix sort is not comparison-based algorithm. It is a linear sorting and better than comparison sorting.





Complexity of Radix Sort

Suppose n is the number of elements in an array.

Best Case Time Complexity - O(n)


Radix Sort Example

Suppose the input array is-

12, 72, 28, 69, 34, 101, 134

As per the radix sort algorithm, first sort the array according to the one's digit.

12, 72, 28, 69, 34, 101, 134
0: 
1: 101
2: 12, 72
3: 
4: 34, 134
5:
6:
7:
8: 28
9: 69
So, the array becomes- 
101, 12, 72, 34, 134, 28, 69




Again sort the array according to the ten's digit.

101, 12, 72, 34, 134, 28, 69
0: 101
1: 12
2: 28
3: 34, 134
4: 
5:
6: 69
7: 72
8: 
9: 
So, the array becomes- 
101, 12, 28, 34, 134, 69, 72

Again sort the array according to the hundred's digit.

101, 12, 28, 34, 134, 69, 72
0: 
1: 101, 134
2: 
4: 
5:
6: 
7: 
8: 
9: 
So, the array becomes- 
12, 28, 34, 69, 72, 101, 134




Radix Sort Program in C

#include<stdio.h>

int main()
{
	int i, n, a[10];
	printf("Enter the number of elements: ");
	scanf("%d",&n);
	printf("Enter %d numbers : \n",n);
	for(i = 0; i < n; i++)
	{
		scanf("%d",&a[i]);
	}
	radix_sort(a,n);
	printf("\n Sorted numbers using Radix Sort : \n");
	for(i = 0; i < n; i++)
		printf("  %d",a[i]);
		printf("\n");
		return 0;
}

int getMax(int a[], int n)
{
	int max_value = a[0], i;
	for(i = 1; i < n; i++)
	{
		if(max_value < a[i])
		max_value = a[i];
	}
	return max_value;
}
// radix sort
void radix_sort(int a[], int n)
{
	int bucket[10][10], bucket_count[10];
	int i, j, k, remainder, NOP=0, divisor=1, large, pass;
	large = getMax(a, n);
	printf("\n Largest element : %d\n",large);
	while(large > 0)
	{
		NOP++;
		large/=10;
	}
	for(pass = 0; pass < NOP; pass++)
	{
		for(i = 0; i < 10; i++)
		{
			  bucket_count[i] = 0;
		}
		for(i = 0; i < n; i++)
		{
			  remainder = (a[i] / divisor) % 10;
			  bucket[remainder][bucket_count[remainder]] = a[i];
			  bucket_count[remainder] += 1;
		}
		
		i = 0;
		for(k = 0; k < 10; k++)
		{
			  for(j = 0; j < bucket_count[k]; j++)
			  {
					a[i] = bucket[k][j];
					i++;
			  }
		}
		divisor *= 10;
		printf("\n PASS %d :\n",pass+1);
		for(i = 0; i < n; i++)
			printf("  %d",a[i]);
			printf("\n");
	}
}
Output of the above code:
Enter the number of elements: 10
Enter 10 numbers : 
09
23
15
52
142
163
12
15
62
72

Largest element : 163

PASS 1 :
52  142  12  62  72  23  163  15  15  9

PASS 2 :
9  12  15  15  23  142  52  62  163  72

PASS 3 :
9  12  15  15  23  52  62  72  142  163

Sorted numbers using Radix Sort : 
9  12  15  15  23  52  62  72  142  163




Related Articles

Quick sort program in C
Bubble sort program in C++
Bubble sort program in C
Selection sort program in Python
Python program for insertion sort
Quick sort program in Python
C program for selection sort
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative




Most Popular Development Resources
Retrieve Data From Database Without Page refresh Using AJAX, PHP and Javascript
-----------------
PHP Create Word Document from HTML
-----------------
How to get data from XML file in PHP
-----------------
Hypertext Transfer Protocol Overview
-----------------
PHP code to send email using SMTP
-----------------
Characteristics of a Good Computer Program
-----------------
How to encrypt password in PHP
-----------------
Create Dynamic Pie Chart using Google API, PHP and MySQL
-----------------
PHP MySQL PDO Database Connection and CRUD Operations
-----------------
Splitting MySQL Results Into Two Columns Using PHP
-----------------
Dynamically Add/Delete HTML Table Rows Using Javascript
-----------------
How to add multiple custom markers on google map
-----------------
How to get current directory, filename and code line number in PHP
-----------------
Fibonacci Series Program in PHP
-----------------
Get current visitor\'s location using HTML5 Geolocation API and PHP
-----------------
How to Sort Table Data in PHP and MySQL
-----------------
Simple star rating system using PHP, jQuery and Ajax
-----------------
Submit a form data using PHP, AJAX and Javascript
-----------------
jQuery loop over JSON result after AJAX Success
-----------------
How to generate QR Code in PHP
-----------------
Simple pagination in PHP
-----------------
Recover forgot password using PHP7 and MySQLi
-----------------
PHP MYSQL Advanced Search Feature
-----------------
PHP Server Side Form Validation
-----------------
PHP user registration and login/ logout with secure password encryption
-----------------
jQuery File upload progress bar with file size validation
-----------------
Simple PHP File Cache
-----------------
Simple File Upload Script in PHP
-----------------
Php file based authentication
-----------------
To check whether a year is a leap year or not in php
-----------------
Calculate distance between two locations using PHP
-----------------
PHP User Authentication by IP Address
-----------------
PHP Secure User Registration with Login/logout
-----------------
Simple way to send SMTP mail using Node.js
-----------------
How to print specific part of a web page in javascript
-----------------
Simple Show Hide Menu Navigation
-----------------
Detect Mobile Devices in PHP
-----------------
Polling system using PHP, Ajax and MySql
-----------------
PHP Sending HTML form data to an Email
-----------------
Google Street View API Example
-----------------
Get Visitor\'s location and TimeZone
-----------------
SQL Injection Prevention Techniques
-----------------
Preventing Cross Site Request Forgeries(CSRF) in PHP
-----------------
Driving route directions from source to destination using HTML5 and Javascript
-----------------
Convert MySQL to JSON using PHP
-----------------
Set and Get Cookies in PHP
-----------------
CSS Simple Menu Navigation Bar
-----------------
PHP Programming Error Types
-----------------
Date Timestamp Formats in PHP
-----------------
How to select/deselect all checkboxes using Javascript
-----------------
How to add google map on your website and display address on click marker
-----------------
Write a python program to print all even numbers between 1 to 100
-----------------
How to display PDF file in web page from Database in PHP
-----------------
PHP Getting Document of Remote Address
-----------------
File Upload Validation in PHP
-----------------


Most Popular Blogs
Most in demand programming languages
Best mvc PHP frameworks in 2019
MariaDB vs MySQL
Most in demand NoSQL databases for 2019
Best AI Startups In India
Kotlin : Android App Development Choice
Kotlin vs Java which one is better
Top Android App Development Languages in 2019
Web Robots
Data Science Recruitment of Freshers - 2019


Interview Questions Answers
Basic PHP Interview
Advanced PHP Interview
MySQL Interview
Javascript Interview
HTML Interview
CSS Interview
Programming C Interview
Programming C++ Interview
Java Interview
Computer Networking Interview
NodeJS Interview
ExpressJS Interview
R Interview


Popular Tutorials
PHP Tutorial (Basic & Advance)
MySQL Tutorial & Exercise
MongoDB Tutorial
Python Tutorial & Exercise
Kotlin Tutorial & Exercise
R Programming Tutorial
HTML Tutorial
jQuery Tutorial
NodeJS Tutorial
ExpressJS Tutorial
Theory of Computation Tutorial
Data Structure Tutorial
Javascript Tutorial






Learn Popular Language

listen
listen
listen
listen
listen

Blogs

  • Jan 3

    Stateful vs Stateless

    A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

  • Dec 29

    Best programming language to learn in 2021

    In this article, we have mentioned the analyzed results of the best programming language for 2021...

  • Dec 20

    How is Python best for mobile app development?

    Python has a set of useful Libraries and Packages that minimize the use of code...

  • July 18

    Learn all about Emoji

    In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

  • Jan 10

    Data Science Recruitment of Freshers

    In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

Follow us

  • etutorialspoint facebook
  • etutorialspoint twitter
  • etutorialspoint linkedin
etutorialspoint youtube
About Us      Contact Us


  • eTutorialsPoint©Copyright 2016-2023. All Rights Reserved.