Skip to content

Commit 75e6cdc

Browse files
author
Bhrigu Kansra
authored
Merge pull request #10 from Shagunaawasthi/master
Radix Sort
2 parents 73d648a + 8ae1a4a commit 75e6cdc

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

contributors.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
4. [Naman Manchanda](https://github.com/namanmanchanda09)
77
5. [Harsh Arora](https://github.com/aroraharsh010)
88
6. [Ujjval Patel](https://github.com/Ujjval-Patel)
9-
7. [Your Name](https://github.com/yourprofile)
9+
7. [Shaguna Awasthi](https://github.com/Shagunaawasthi)
10+
8. [Your Name](https://github.com/yourprofile)
1011

sorting/radixsort.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
/* A function to do counting sort of array according to
5+
the digit represented by exp.*/
6+
int countSort(int arr[], int n, int exp)
7+
{
8+
9+
int output[n];
10+
int i, count[n] ;
11+
for (int i=0; i < n; i++)
12+
count[i] = 0;
13+
14+
// Storing count of occurrences in count[]
15+
for (i = 0; i < n; i++)
16+
count[ (arr[i]/exp)%n ]++;
17+
18+
// count[i] now contains actual position of this digit in output[]
19+
20+
for (i = 1; i < n; i++)
21+
count[i] += count[i - 1];
22+
23+
// output array
24+
for (i = n - 1; i >= 0; i--)
25+
{
26+
output[count[ (arr[i]/exp)%n] - 1] = arr[i];
27+
count[(arr[i]/exp)%n]--;
28+
}
29+
30+
31+
for (i = 0; i < n; i++)
32+
arr[i] = output[i];
33+
}
34+
35+
36+
// The main function to that sorts arr[] of size n using Radix Sort
37+
void sort(int arr[], int n)
38+
{
39+
40+
countSort(arr, n, 1);
41+
countSort(arr, n, n);
42+
}
43+
44+
// Function to print an array
45+
void printArr(int arr[], int n)
46+
{
47+
for (int i = 0; i < n; i++)
48+
cout << arr[i] << " ";
49+
}
50+
51+
// Driver program to test above functions
52+
int main()
53+
{ int n;
54+
cout<<"enter size of array"<<endl;
55+
cin>>n;
56+
int * arr=new int [n];
57+
58+
for(int i=0;i<n;i++)
59+
{
60+
cin>>arr[i];
61+
}
62+
cout << "Given array :";
63+
printArr(arr, n);
64+
65+
sort(arr, n);
66+
67+
cout << "Sorted array :";
68+
printArr(arr, n);
69+
return 0;
70+
}

0 commit comments

Comments
 (0)