Pagini recente » Rating Stangaciu Sofian (Straton) | Rating Rebenciuc Paul-Florin (Paul-Florin) | Rating Victor (viman39) | Cod sursa (job #357791) | Cod sursa (job #1694200)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
void read_input(int **list, int *size);
void flash_sort(int *list, int size);
void hulk_sort(int *list, int low, int high);
void swap_values(int *first, int *second);
int select_pivot(int *list, int low, int high);
void write_output(int *list, int size);
int main() {
int size;
int *list;
srand(time(NULL));
read_input(&list, &size);
flash_sort(list, size);
write_output(list, size);
return 0;
}
void read_input(int **list, int *size) {
FILE *input = fopen("algsort.in", "r");
fscanf(input, "%d\n", size);
*list = (int*) malloc(sizeof(int) * (*size));
for (int index = 0; index < *size; index +=1) {
fscanf(input, "%d", *list + index);
}
fclose(input);
}
void flash_sort(int *list, int size) {
int pivot, pivot_position, sort_threshold;
int left, left_low, right, right_high;
int *partition_low, *partition_high;
int partition_size, partition_start, partition_end;
partition_size = 200000;
partition_low = (int*) malloc(sizeof(int) * (partition_size));
partition_high = (int*) malloc(sizeof(int) * (partition_size));
sort_threshold = 10;
left_low = 0;
right_high = size -1;
partition_start = -1;
partition_end = 0;
partition_low[partition_end] = left_low;
partition_high[partition_end] = right_high;
while(partition_start < partition_end) {
partition_start += 1;
left_low = partition_low[partition_start];
right_high = partition_high[partition_start];
if (right_high - left_low <= sort_threshold) {
hulk_sort(list, left_low, right_high);
continue;
}
pivot_position = select_pivot(list, left_low, right_high);
left = left_low;
right = right_high;
pivot = list[pivot_position];
while (left <= right) {
while(list[left] < pivot && left < right_high) left += 1;
while(list[right] > pivot && right > left_low) right -= 1;
if (left <= right) {
// if (list[left] < list[right]) {
swap_values(&list[left], &list[right]);
// }
left += 1;
right -= 1;
};
}
if (left_low < right) {
partition_end += 1;
partition_high[partition_end] = right;
partition_low[partition_end] = left_low;
}
if (left < right_high) {
partition_end += 1;
partition_high[partition_end] = right_high;
partition_low[partition_end] = left;
}
}
}
void hulk_sort(int *list, int low, int high) {
int carry;
for (int first = low; first < high; first += 1) {
for (int second = first + 1; second <= high; second += 1) {
if (list[first] > list[second]) {
swap_values(&list[first], &list[second]);
}
}
}
}
void swap_values(int *first, int *second) {
int carry;
carry = *first;
*first = *second;
*second = carry;
}
int select_pivot(int *list, int low, int high) {
return rand() % (high - low) + low;
}
void write_output(int *list, int size) {
FILE *output = fopen("algsort.out", "w");
int last = size - 1;
for (int index = 0; index < size; index +=1) {
if (index < last) {
fprintf(output, "%d ", list[index]);
} else {
fprintf(output, "%d", list[index]);
}
}
fprintf(output, "\n");
fclose(output);
}