-
2 Attachment(s)
Sorting strings in C
So I got the sorting program to work when I set the array without any numbers. However when reading from a file which has numbers before the word it is not behaving the way I want it to.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmpstr(void* v1, void* v2)
{
char *a1 = *(char**)v1;
char *a2 = *(char**)v2;
return strcmp(a1, a2);
}
// function for comparing two strings
void swap(void* v1, void* v2, int size)
{
char buffer[size];
memcpy(buffer, v1, size);
memcpy(v1, v2, size);
memcpy(v2, buffer, size);
}
void _qsort(void* v, int size, int left, int right,
int (*comp)(void*, void*))
{
void *vt, *v3;
int i, last, mid = (left + right) / 2;
if (left >= right)
return;
// casting void* to char* so that operations
// can be done.
void* vl = (char*)(v + (left * size));
void* vr = (char*)(v + (mid * size));
swap(vl, vr, size);
last = left;
for (i = left + 1; i <= right; i++) {
vt = (char*)(v + (i * size));
if ((*comp)(vl, vt) > 0) {
++last;
v3 = (char*)(v + (last * size));
swap(vt, v3, size);
}
}
v3 = (char*)(v + (last * size));
swap(vl, v3, size);
_qsort(v, size, left, last - 1, comp);
_qsort(v, size, last + 1, right, comp);
}
int main()
{
char* a[] = {"bbc", "xcd", "ede", "def",
"afg", "hello", "hmmm", "okay", "how", "welcome" };
_qsort(a, sizeof(char*), 0, 8, (int (*)(void*, void*))(cmpstr));
for (int i = 0; i < 10; i++)
printf("%s ", a[i]);
printf("\n");
}
The desired output for the input is shown.
Attachment 16275
Attachment 16276
-
Well yeah, you need to show how you're reading from the file, how you're parsing the data and how you're constructing the array to be sorted.
-
Made some changes
@salem I made a few tweaks. Now it comes to segmentation fault.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int cmpstr(void* v1, void* v2)
{
char *a1 = *(char**)v1;
char *a2 = *(char**)v2;
return strcmp(a1, a2);
}
void swap(void* v1, void* v2, int size)
{
char buffer[size];
memcpy(buffer, v1, size);
memcpy(v1, v2, size);
memcpy(v2, buffer, size);
}
void char_sort(void* v, char size, char left, char right,
char (*comp)(void*, void*))
{
void *vt, *v3;
int i, last, mid = (left + right) / 2;
if (left >= right)
return;
void* vl = (char*)(v + (left * size));
void* vr = (char*)(v + (mid * size));
swap(vl, vr, size);
last = left;
for (i = left + 1; i <= right; i++) {
vt = (char*)(v + (i * size));
if ((*comp)(vl, vt) > 0) {
++last;
v3 = (char*)(v + (last * size));
swap(vt, v3, size);
}
}
v3 = (char*)(v + (last * size));
swap(vl, v3, size);
char_sort(v, size, left, last - 1, comp);
char_sort(v, size, last + 1, right, comp);
}
int main()
{
char a[10];
FILE *in, *out;
in = fopen("alphabetic_sort.dat", "r");
out = fopen("sort.out", "w");
while (1)
{
fscanf(in, "%d", &a);
char_sort(a, sizeof(char*), 0, 10, (char (*)(void*, void*))(cmpstr));
int i;
for (i = 0; i < 9; i++)
fprintf(out, "%s \n", a[i]);
}
return 0;
}
-
Post it again, this time without the awful fonts from your IDE.
-
Sorry @salem. Posting it again
-
@salem
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int cmpstr(void* v1, void* v2)
{
char *a1 = *(char**)v1;
char *a2 = *(char**)v2;
return strcmp(a1, a2);
}
void swap(void* v1, void* v2, int size)
{
char buffer[size];
memcpy(buffer, v1, size);
memcpy(v1, v2, size);
memcpy(v2, buffer, size);
}
void char_sort(void* v, char size, char left, char right,
char (*comp)(void*, void*))
{
void *vt, *v3;
int i, last, mid = (left + right) / 2;
if (left >= right)
return;
void* vl = (char*)(v + (left * size));
void* vr = (char*)(v + (mid * size));
swap(vl, vr, size);
last = left;
for (i = left + 1; i <= right; i++) {
vt = (char*)(v + (i * size));
if ((*comp)(vl, vt) > 0) {
++last;
v3 = (char*)(v + (last * size));
swap(vt, v3, size);
}
}
v3 = (char*)(v + (last * size));
swap(vl, v3, size);
char_sort(v, size, left, last - 1, comp);
char_sort(v, size, last + 1, right, comp);
}
int main()
{
char a[10];
FILE *in, *out;
in = fopen("alphabetic_sort.dat", "r");
out = fopen("sort.out", "w");
while (1)
{
fscanf(in, "%d", &a);
char_sort(a, sizeof(char*), 0, 10, (char (*)(void*, void*))(cmpstr));
int i;
for (i = 0; i < 9; i++)
fprintf(out, "%s \n", a[i]);
}
return 0;
}
-
Why are you saving the integer inside the char array? Why are you passing "sizeof(char*)" as the size?
Btw, you don't want to use "char" for the types of size and left/right. "int" would be much better.
Also btw, the way I see it, the number in front of each line is the amount of characters you should allocate on the heap(malloc/free etc), inside which you'd read the string.
-
> However when reading from a file which has numbers before the word it is not behaving the way I want it to.
This would suggest your input should be
Code:
int a;
char b[100];
fscanf(in, "%d %s", &a, b);
Also, before you do any file reading at all, make sure this works.
Code:
char a[10] = "qawsedrftg";
char_sort(a, sizeof(char), 0, 10, (char (*)(void*, void*))(cmpstr));
for (i = 0; i < 9; i++)
printf("%d = %c\n", i, a[i] );