#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 classic_quick_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);
// classic_quick_sort(list, 0, size - 1);
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, carry;
int left, left_low, left_high, right, right_low, right_high;
int *partition_low, *partition_high;
int partition_size, partition_start, partition_end;
partition_size = 200000; //(int) ceil(log2((float) size) * 3);
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;
// printf("List: ");
// for (int index = 0; index < size; index +=1) {
// printf("%d ", list[index]);
// }
// printf("\n");
while(partition_start < partition_end) {
partition_start += 1;
// printf("Start: %d End: %d\n", partition_start, partition_end);
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);
// printf("Insertion: %d %d\n", left_low, right_high);
//
// printf("List: ");
// for (int index = 0; index < size; index +=1) {
// printf("%d ", list[index]);
// }
// printf("\n");
continue;
}
pivot_position = select_pivot(list, left_low, right_high);
// printf("Position: %d Pivot: %d\n", pivot_position, list[pivot_position]);
left = left_low;
right = right_high;
pivot = list[pivot_position];
// printf("Range: %d - %d\n", left, right);
while (left <= right) {
while(list[left] < pivot && left < right_high) left += 1;
while(list[right] > pivot && right > left_low) right -= 1;
// printf("Left: %d Right: %d Position: %d\n", left, right, pivot_position);
//
// printf("List Before: ");
// for (int index = 0; index < size; index +=1) {
// printf("%d ", list[index]);
// }
// printf("\n");
if (left <= right) {
swap_values(&list[left], &list[right]);
left += 1;
right -= 1;
};
// printf("Pivot Position: %d\n", pivot_position);
//
// printf("List After: ");
// for (int index = 0; index < size; index +=1) {
// printf("%d ", list[index]);
// }
// printf("\n");
}
if (left_low < right) {
partition_end += 1;
partition_high[partition_end] = right;
partition_low[partition_end] = left_low;
// printf("Added: %d %d\n", partition_high[partition_end], partition_low[partition_end]);
}
if (left < right_high) {
partition_end += 1;
partition_high[partition_end] = right_high;
partition_low[partition_end] = left;
// printf("Added: %d %d\n", partition_high[partition_end], partition_low[partition_end]);
}
}
}
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]) {
carry = list[first];
list[first] = list[second];
list[second] = carry;
}
}
}
}
void classic_quick_sort(int *list, int low, int high) {
int left, right, pivot;
left = low;
right = high;
pivot = list[(low + high) >> 1];
while(left <= right) {
while(list[left] < pivot && left < high) left += 1;
while(list[right] > pivot && right > low) right -= 1;
if(left <= right) {
swap_values(&list[left], &list[right]);
left += 1;
right -= 1;
}
}
if(low < right) classic_quick_sort(list, low, right);
if(left < high) classic_quick_sort(list, left, high);
}
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);
}