After the whole program executed correctly, I get munmap_chunk(): invalid pointer and Aborted (core dumped) message in the terminal.
Function for Radix Sort :
I've designed the function in the following steps:
for(int i = 0 ;i < A.size(); i++){
int t = (int(A[i]/pow(10,j))%10);
++C[t];
}
#include<bits/stdc++.h>
using namespace std;
void Print(vector<int> &A){
for(auto i =A.begin(); i!= A.end(); i++){
cout << *i << " ";
}
cout << endl;
}
void radixSort(vector<int> &A){
int k = A[0];
for(auto i = A.begin(); i!=A.end(); i++){
if(*i > k) k =*i;
}
int d =0;
while(k >0){
k = k/10;
d++;
}
vector<int> result;
for(int j =0; j <d; j++)
{
vector<int> C(10, 0); // Making the array Count of k elements with all 0's
vector<int> B(A.size()); // Output Array
for(int i = 0 ;i < A.size(); i++){
int t = (int(A[i]/pow(10,j))%10); // for taking ith value
++C[t]; // Counting Frequencies of elements in Input array
}
for(int i=1; i<= C.size(); i++){ // Calculating no of elements occuring before postion i
C[i]+= C[i-1]; // Or Calculating final postion of the value
}
for(int i = B.size()-1;i >= 0; i--){
int t = (int(A[i]/pow(10,j))%10);
B[C[t]-1] = A[i]; // Placing the elemsnts from Input Array in Output array accoring to their position
--C[t]; // Decrementing so as to place a same value on left side ( if it Exits)
}
result = A = B;
}
}
Main Function
int main(){
vector<int> A;
srand(time(NULL));
for(int i =0; i< 10; i++){
A.push_back(rand()%1000);
}
cout << "Input Array : " << endl;;
Print(A);
radixSort(A);
cout << "Output Sorted Array : " << endl;;
Print(A);
return 0;
}
I see an issue here, as C[10] is being used as an index, which is beyond last element of the vector. The easiest fix is to make C 1 element larger, and fix the count to index conversion loop:
vector<int> C(11, 0); // fix (11)
// ...
for(int i=1; i <= 10; i++){ // fix ( <= 10)
C[i]+= C[i-1];
}