File tree Expand file tree Collapse file tree 2 files changed +72
-1
lines changed Expand file tree Collapse file tree 2 files changed +72
-1
lines changed Original file line number Diff line number Diff line change 664 . [ Naman Manchanda] ( https://github.com/namanmanchanda09 )
775 . [ Harsh Arora] ( https://github.com/aroraharsh010 )
886 . [ 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
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments