-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradix.cpp
More file actions
126 lines (107 loc) · 2.95 KB
/
radix.cpp
File metadata and controls
126 lines (107 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// #include <iostream>
// #include "print.h"
// using namespace std;
// int maximum(int arr[], int size)
// {
// int max = arr[0];
// for (int i = 0; i < size; i++)
// {
// if (arr[i] > max)
// max = arr[i];
// }
// return max;
// }
// void count(int arr[], int size, int place)
// {
// int *count = new int[size]();
// for (int i = 0; i < size; i++)
// count[((arr[i]) / place) % 10]++;
// for (int i = 0; i < size; i++)
// count[i] += count[i - 1];
// // int *sortedArr = new int[10];
// for(int i = 9 ; i >=0 ; i--)
// --count[(count[i]/place) % 10 ] = count[i];
// }
// void sort(int arr[], int size)
// {
// int max = maximum(arr, size);
// for (int place = 1; max / place > 0; place *= 10)
// {
// count(arr, size, place);
// }
// }
// int main()
// {
// int arr[10] = {0, 5, 3, 2, 9, 12, 34, 65, 43, 50};
// sort(arr, 10);
// print(arr, 10);
// return 0;
// }
#include <iostream>
#include <cmath>
#include "print.h" // Assuming you have a print function for printing arrays
using namespace std;
// Function to find the maximum value in an array
int findMax(int arr[], int size)
{
int max = arr[0];
for (int i = 1; i < size; i++)
{
if (arr[i] > max)
max = arr[i];
}
return max;
}
// Function to perform counting sort on the specified digit place
void countingSort(int arr[], int size, int place)
{
const int radix = 10; // Assuming decimal numbers (0-9)
int *output = new int[size];
int count[radix] = {0};
// Count occurrences of each digit at the specified place
for (int i = 0; i < size; i++)
count[(arr[i] / place) % radix]++;
// Update count to store the actual position of each digit in the output array
for (int i = 1; i < radix; i++)
count[i] += count[i - 1];
// Build the output array
for (int i = size - 1; i >= 0; i--)
{
output[count[(arr[i] / place) % radix] - 1] = arr[i];
count[(arr[i] / place) % radix]--;
}
// Copy the sorted elements back to the original array
for (int i = 0; i < size; i++)
arr[i] = output[i];
delete[] output;
}
// Main function to perform Radix Sort
void radixSort(int arr[], int size)
{
// Find the maximum number to determine the number of digits
int max = findMax(arr, size);
// Perform counting sort for every digit place
for (int place = 1; max / place > 0; place *= 10)
{
// Step 1: Apply counting sort on the current digit place
countingSort(arr, size, place);
// Step 2: Print the array after sorting on the current digit place
// cout << "Array after sorting on digit place " << place << ": ";
// print(arr, size);
}
}
// Driver's code
int main()
{
int arr[10] = {170, 45, 75, 90, 802, 24, 2, 66, 78, 100};
int size = sizeof(arr) / sizeof(arr[0]);
// Step 0: Print the original array
cout << "Original array: ";
print(arr, size);
// Perform Radix Sort
radixSort(arr, size);
// Step 3: Print the final sorted array
cout << "Sorted array: ";
print(arr, size);
return 0;
}