Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Count Sort

#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);
}

No comments:

Post a Comment

Search 24 Bytes