55502f40dc8b7c769880b10874abc9d0

just to show.

Count Sort in c++


#include < iostream.h >
#include < stdio.h >
#include < conio.h >

    void print(int a[]) {
  for (int i = 0; i < 10; i++) {
    cout < < a[i] < < "-";
  }
  cout < < endl;
}

int max(int a[]) {
  int mac = 0;
  for (int i = 0; i < 10; i++) {
    if (a[i] > mac) {
      mac = a[i];
    }
  }
  cout < < "ths is mac " < < mac < < endl;
  return mac;
}

void countTimes(int a[], int b[]) {
  int maxi = max(a);

  for (int i = 0; i < = maxi; i++) {
    b[i] = 0;

  }
  for (i = 0; i < 10; i++) {
    b[a[i]] = b[a[i]] + 1;
  }
}

void countRanks(int b[]) {
  for (int i = 1; i < = 7; i++) {
    b[i] += b[i - 1];
  }
}
void countSort(int a[], int b[], int c[]) {
  int l = 0;
  for (int i = 9; i >= 0; i--) {
    cout < < a[i] < < "-" < < b[a[i]] < < "\n";
    c[b[a[i]] - 1] = a[i];
    b[a[i]] = b[a[i]] - 1;
  }
  print(c);
}

void main() {
  clrscr();

  int a[] = {
      1, 2, 4, 6, 7, 7, 0, 6, 3, 5};
  print(a);
  int * b;
  int mac = max(a);
  int c[] = {
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  b = new int[mac];

  countTimes(a, b);
  countRanks(b);
  countSort(a, b, c);
  print(c);
}

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

KangOl

November 3, 2007, November 03, 2007 22:27, permalink

No rating. Login to rate!

I don't understand what you're trying to do...

776160fbb8bda99799a240df27a2b548

Fluxus

November 7, 2007, November 07, 2007 10:37, permalink

No rating. Login to rate!

You can use template to implicitly pass to the function the size of the array.

#include < iostream.h >
#include < stdio.h >
#include < conio.h >

template <class T, int Size>
void print(T (&a)[Size]) {
  for (int i = 0; i < Size; i++) {
    cout < < a[i] < < "-";
  }
  cout < < endl;
}

template <class T, int Size>
int max(T (&a)[Size]) {
  int mac = 0;
  for (int i = 0; i < Size; i++) {
    if (a[i] > mac) {
      mac = a[i];
    }
  }
  cout < < "ths is mac " < < mac < < endl;
  return mac;
}

template <class T, int Size>
void countRanks(T (&b)[Size]) {
  for (int i = 1; i < = Size; i++) {
    b[i] += b[i - 1];
  }
}

void main() {
  clrscr();

  int a[] = {
      1, 2, 4, 6, 7, 7, 0, 6, 3, 5};
  print(a);
  int * b;
  int mac = max(a);
  int c[] = {
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  b = new int[mac];

  countRanks(b);
  print(c);
}
0bd5c1d9900046057ef1a31a12734a3a

evilteach.blogspot.com

September 1, 2008, September 01, 2008 13:18, permalink

No rating. Login to rate!

I note that your code does not compile, and there are are some array subscript defects, but here are a few 'untested' changes that might be better.

You are using a radix sort. Look it up in Wikipedia.

What class did you do this for?

// Count Sort in c++


#include < iostream.h >
#include < stdio.h >
#include < conio.h >
#include < string.h >       // for memset

void print(int a[]) {
  for (int i = 0; i < 10; i++) {
    cout << a[i] << "-";
  }
  cout << endl;
}

int max(int a[]) {
  int mac = a[0];                   // Simply start with a[0] as your initial max value
  for (int i = 1; i < 10; i++) {    // then loop from 1
    if (a[i] > mac) {
      mac = a[i];
    }
  }
  cout << "ths is mac " << mac << endl;
  return mac;
}

void countTimes(int a[], int b[]) {

  for (int i = 0; i < 10; i++) {
    b[a[i]]++;                      // less typing to add one to the value
  }
}

void countRanks(int b[]) {
  for (int i = 1; i <= 7; i++) {
    b[i] += b[i - 1];
  }
}
void countSort(int a[], int b[], int c[]) {
  int l = 0;
  for (int i = 9; i >= 0; i--) {
    cout << a[i] << "-" << b[a[i]] << "\n";
    c[b[a[i]] - 1] = a[i];
    b[a[i]] = b[a[i]] - 1;
  }
  print(c);
}

void main() {
  clrscr();

  int a[] = {
      1, 2, 4, 6, 7, 7, 0, 6, 3, 5};
  print(a);
  int * b;
  int mac = max(a);
  int c[] = {
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  b = new int[mac];
  memset(b, 0, mac * sizeof(int));  // This is a faster way to set the elements to zero

  countTimes(a, b);
  countRanks(b);
  countSort(a, b, c);
  print(c);

  delete[] b;                     // always delete dynamically allocated memory
}
D41d8cd98f00b204e9800998ecf8427e

pardus1

November 21, 2008, November 21, 2008 07:32, permalink

No rating. Login to rate!

I did not understand why the code is categorized as C++ code. It is in C except the I/O functions.

6b9405b1e8b2bf2e9de1b55f1804b103

Asharaf khan

September 23, 2011, September 23, 2011 03:23, permalink

No rating. Login to rate!
#include<stdio.h>
#include<conio.h>
void countsort(int [],int [], int);
int n;
void main()
{
int a[50],b[50],i,k;
clrscr();
printf("Enter the size of array: ");
scanf("%d",&n);
printf("Enter the elements of array: ");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
k=a[1];
for(i=1;i<=n;i++)
{
if(k<a[i])
k=a[i];
}
countsort(a,b,k);
printf("\n array after sorting the array is :");
for(i=1;i<=n;i++)
{
printf("%d\t",b[i]);
}
getch();
}
void countsort(int a[], int b[], int k)
{
int c[50], i,j;
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;
}
}

Your refactoring





Format Copy from initial code

or Cancel