sortstrings: done with no libraries
This commit is contained in:
parent
7fb7197e75
commit
7a757657e6
2 changed files with 63 additions and 16 deletions
|
@ -1,30 +1,47 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#define MAX_N 1000
|
||||
const unsigned int MAX_N = 1000;
|
||||
|
||||
int compar(const void* string1, const void* string2) {
|
||||
const char** s1 = (const char**) string1;
|
||||
const char** s2 = (const char**) string2;
|
||||
return strcmp(*s1, *s2);
|
||||
unsigned int c_strlen(const char* s) {
|
||||
unsigned int i = 0;
|
||||
for (; *s; s++) i++;
|
||||
return i;
|
||||
}
|
||||
|
||||
void sort_strings(const char* input, const char* output, unsigned int n) {
|
||||
unsigned int less_than(const char *s1, const char* s2) {
|
||||
for(; *s1 == *s2 && *s1; s1++) s2++;
|
||||
return (*s1 < *s2);
|
||||
}
|
||||
|
||||
void insertion_sort(const char* strs[], unsigned int n) {
|
||||
for (unsigned int i = 0; i < n-1; i++) {
|
||||
for (unsigned int j = i+1; j > 0; j--) {
|
||||
if (less_than(strs[j], strs[j-1])) {
|
||||
const char* tmp = strs[j-1];
|
||||
strs[j-1] = strs[j];
|
||||
strs[j] = tmp;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sort_strings(const char* input, char* output, unsigned int n) {
|
||||
const char* strings[MAX_N];
|
||||
size_t i = 0;
|
||||
size_t offset = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int offset = 0;
|
||||
for (i = 0; i < n; i++) {
|
||||
strings[i] = input + offset;
|
||||
offset += strlen(input + offset) + 1; // + 1 for NUL
|
||||
offset += c_strlen(input + offset) + 1; // + 1 for NUL
|
||||
}
|
||||
|
||||
qsort(strings, n, sizeof(const char*), &compar);
|
||||
insertion_sort(strings, n);
|
||||
|
||||
offset = 0;
|
||||
for (i = 0; i < n; i++) {
|
||||
const size_t len = strlen(strings[i]);
|
||||
strncpy((char*) output + offset, (char*) strings[i], len + 1);
|
||||
const unsigned int len = c_strlen(strings[i]);
|
||||
for (unsigned int j = 0; j <= len; j++) {
|
||||
output[offset + j] = strings[i][j];
|
||||
}
|
||||
offset += len + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
30
sortstrings/sortstrings_lib.c
Normal file
30
sortstrings/sortstrings_lib.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#define MAX_N 1000
|
||||
|
||||
int compar(const void* string1, const void* string2) {
|
||||
const char** s1 = (const char**) string1;
|
||||
const char** s2 = (const char**) string2;
|
||||
return strcmp(*s1, *s2);
|
||||
}
|
||||
|
||||
void sort_strings(const char* input, const char* output, unsigned int n) {
|
||||
const char* strings[MAX_N];
|
||||
size_t i = 0;
|
||||
size_t offset = 0;
|
||||
for (i = 0; i < n; i++) {
|
||||
strings[i] = input + offset;
|
||||
offset += strlen(input + offset) + 1; // + 1 for NUL
|
||||
}
|
||||
|
||||
qsort(strings, n, sizeof(const char*), &compar);
|
||||
|
||||
offset = 0;
|
||||
for (i = 0; i < n; i++) {
|
||||
const size_t len = strlen(strings[i]);
|
||||
strncpy((char*) output + offset, (char*) strings[i], len + 1);
|
||||
offset += len + 1;
|
||||
}
|
||||
}
|
||||
|
Reference in a new issue