Here's the code of counting sort I implemented in C++:
#include<iostream>
#include<stdlib.h>
using namespace std;
void counting_sort(int [], int, int);
main()
{
int n,k = 0, a[15];
cout << "Enter the number of input: ";
cin >> n;
cout << "\nEnter the elements to be sorted: \n";
for ( int i = 1; i <= n; i++)
{
cin >> a[i];
if(a[i] > k)
{
k = a[i];
}
}
counting_sort(a, k, n);
system("pause");
//getch();
}
void counting_sort(int a[], int k, int n)
{
int i, j;
int b[15], c[100];
for(i = 0; i <= k; i++)
c[i] = 0;
for(j =1; j <= n; j++)
c[a[j]] = c[a[j]] + 1;
for(i = 1; i <= k; i++)
c[i] = c[i] + c[i-1];
for(j = n; j >= 1; j--)
{
b[c[a[j]]] = a[j];
c[a[j]] = c[a[j]] - 1;
}
cout << "\nThe Sorted array is: ";
for(i = 1; i <= n; i++)
cout << b[i] << " " ;
}
There is an error on compilation that states "Stray \1 in program" in Line 3 Col 1. I tried in on Dev-C++ and Ideone. Both show the same error. I also tried copying the code to a new file but in vain. How can I rectify it?
There is an (hidden) invalid character in your code (line:3) which copied along with your code on http://ideone.com/ALbZbr.
Try editing this code. You will see a red dot (invalid character) on the third line.
#include<iostream>
#include<stdlib.h>
using namespace std; . <--
void counting_sort(int [], int, int);
main()
{
Delete this invalid character and your code will eventually run.