writing first runs

This commit is contained in:
bronku 2025-10-21 19:09:33 +02:00
parent 5d9fc7d18d
commit 668d55aa5a
4 changed files with 77 additions and 8 deletions

View file

@ -41,3 +41,61 @@ int write_buffer(buffer* buff, FILE* out)
} }
return SUCCESS; return SUCCESS;
} }
int write_buffer_debug(buffer* buff, FILE* out)
{
int status;
record* current = buff->location;
for (int i = 0; i < buff->length; i++) {
fprintf(out, "%d\t", g(current));
status = print_record(out, current);
if (status < 0) {
return status;
}
fprintf(out, "\n");
current++;
}
return SUCCESS;
}
int partition(record* arr, int start, int end)
{
int i = start;
record* pivot = arr;
pivot += end;
for (int j = start; j < end; j++) {
record* current = arr;
current += j;
if (compare_records(current, pivot) > 0) {
continue;
}
record* i_rec = arr;
i_rec += i;
record tmp = *i_rec;
*i_rec = *current;
*current = tmp;
i++;
}
record* i_rec = arr;
i_rec += i;
record tmp = *i_rec;
*i_rec = *pivot;
*pivot = tmp;
return i;
}
void quicksort(record* arr, int start, int end, int depth)
{
if (start >= end || start < 0) {
return;
}
int p = partition(arr, start, end);
quicksort(arr, start, p - 1, depth + 1);
quicksort(arr, p + 1, end, depth + 1);
}
void sort_buffer(buffer* buff)
{
quicksort(buff->location, 0, buff->length - 1, 0);
}

View file

@ -10,3 +10,5 @@ typedef struct {
buffer create_buffer(int capacity); buffer create_buffer(int capacity);
int read_buffer(buffer* buff, FILE* in); int read_buffer(buffer* buff, FILE* in);
int write_buffer(buffer* buff, FILE* in); int write_buffer(buffer* buff, FILE* in);
int write_buffer_debug(buffer* buff, FILE* in);
void sort_buffer(buffer* buff);

18
main.c
View file

@ -23,20 +23,26 @@ int generate_file(int N, const char* filename)
int sort_file(Configuration* opts) int sort_file(Configuration* opts)
{ {
// create buffer
buffer buff = create_buffer(opts->b * opts->n); buffer buff = create_buffer(opts->b * opts->n);
// open file
FILE* in = fopen(opts->input_file, "r"); FILE* in = fopen(opts->input_file, "r");
// <stage 1> // <stage 1>
// while !eof:
for (int i = 0; true; i++) { for (int i = 0; true; i++) {
// read buffer // read buffer
int status = read_buffer(&buff, in); int status = read_buffer(&buff, in);
printf("buffer i:%d, status: %d\n", i, status); if (status != SUCCESS && status != EOF) {
fclose(in);
free(buff.location);
}
// sort buffer // sort buffer
sort_buffer(&buff);
// write run // write run
write_buffer(&buff, stdout); char* filename = malloc(256);
sprintf(filename, "%s/%d", opts->directory, i);
FILE* tmp = fopen(filename, "w");
write_buffer(&buff, tmp);
fclose(tmp);
free(filename);
// write_buffer_debug(&buff, stdout);
if (status != SUCCESS) { if (status != SUCCESS) {
break; break;
} }

View file

@ -18,14 +18,17 @@ build:
build/run: build build/run: build
mkdir -p build/run mkdir -p build/run
build/run/tmp: build/run
mkdir -p build/run/tmp
clean: clean:
rm -rf build rm -rf build
build/run/1.in: build/run build/main build/run/1.in: build/run build/main
build/main -g -o build/run/1.in build/main -g -o build/run/1.in
run: build/main build/run/1.in run: build/main build/run/1.in build/run/tmp
build/main -i build/run/1.in | less build/main -i build/run/1.in -d build/run/tmp | less
debug: build/main tests/1.in debug: build/main tests/1.in
lldb -- build/main lldb -- build/main