I implemented merge sort and used it as a solution for this codechef problem. Here are the submissions. The code is placed below.
The problem that I think is causing slow execution is that my IO is slow in the main
function. I know the number of elements that are inputted so there has to be some faster way to read input instead of the way that I am doing.
Are there faster IO methods instead of the ones that I am using in the main
function? I have heard about using buffers, fgets
and sscanf
but I don't know if they are faster.
Any code examples would be helpful.
#include<stdio.h>
#include<stdlib.h>
void merge_parts(int arr[], int length)
{
int *ans;
int i, j, k;
int temp = length/2;
ans = malloc(sizeof(int) * length);
//This while and next if-else puts the merged array into temporary array ans
for (j = temp, i = k = 0; (i < temp && j < length); k++){
ans[k] = (arr[i] < arr[j]) ? arr[i++] : arr[j++];
}
if(i >= temp){
while(j < length){
ans[k++] = arr[j++];
}
}
else{
while(i < temp){
ans[k++] = arr[i++];
}
}
//This while loops puts array ans into original array arr
for(i = 0; i < length; i++){
arr[i] = ans[i];
}
free(ans);
}
void merge_sort(int arr[], int length)
{
if(length > 1)
{
merge_sort(&arr[0], (length/2));
merge_sort(&arr[length/2], (length - length/2));
merge_parts(arr, length);
}
}
int main()
{
int length;
int *arr;
scanf("%d", &length);
arr = malloc(sizeof(int) * length);
for(int i = 0; i < length; i++)
scanf("%d", &arr[i]);
merge_sort(arr, length);
for(int i = 0; i < length; i++)
printf("%d ", arr[i]);
free(arr);
return 0;
}
EDIT3:
[I deleted EDIT AND EDIT2 as they were no longer relevant]
merge_sort algorithm that I am using
void merge_parts(int arr[], int length)
{
int ans[length];
int i, j, k;
int temp = length/2;
//This while and next if-else puts the merged array into temporary array ans
for (j = temp, i = k = 0; (i < temp && j < length); k++){
ans[k] = (arr[i] < arr[j]) ? arr[i++] : arr[j++];
}
if(i >= temp){
while(j < length){
ans[k++] = arr[j++];
}
}
else{
while(i < temp){
ans[k++] = arr[i++];
}
}
//This while loops puts array ans into original array arr
for(i = 0; i < length; i++){
arr[i] = ans[i];
}
}
void merge_sort(int arr[], int length)
{
if(length > 1)
{
merge_sort(&arr[0], (length/2));
merge_sort(&arr[length/2], (length - length/2));
merge_parts(arr, length);
}
}
merge1.c
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
#define SORTING_ALGO_CALL merge_sort
char buffer[4096];
int bufcount;
int bufpos;
int get_next_char()
{
if (!bufcount)
{
bufcount = fread(buffer, 1, 4096, stdin);
bufpos = 0;
if (!bufcount){
return EOF;
}
}
bufcount--;
return buffer[bufpos++];
}
int readnum()
{
int res = 0;
char ch;
do
{
ch = get_next_char();
} while (!isdigit(ch) && ch != EOF);
if (ch == EOF){
return 0xbaadbeef; // Don't expect this to happen.
}
do
{
res = (res * 10) + ch - '0';
ch = get_next_char();
} while(isdigit(ch));
return res;
}
int main()
{
clock_t time1, time2;
double time_taken;
//FIRST READ
time1 = clock();
int length = readnum();
while (length < 1)
{
printf("\nYou entered length = %d\n", length);
printf("\nEnter a positive length: ");
length = readnum();
}
//SECOND READ, PRINT AND NEXT FIRST READ
time2 = clock();
time_taken = (double)(time2 - time1) / CLOCKS_PER_SEC;
printf("\nReading length = %f\n", time_taken);
time1 = clock();
int *arr;
if ((arr = malloc(sizeof(int) * length)) == NULL)
{
perror("The following error occurred");
exit(-1);
}
//SECOND READ, PRINT AND NEXT FIRST READ
time2 = clock();
time_taken = (double)(time2 - time1) / CLOCKS_PER_SEC;
printf("\nAllocating array = %f\n", time_taken);
time1 = clock();
for (int i = 0; i < length; i++){
arr[i] = readnum();
}
//SECOND READ, PRINT AND NEXT FIRST READ
time2 = clock();
time_taken = (double)(time2 - time1) / CLOCKS_PER_SEC;
printf("\nReading array = %f\n", time_taken);
time1 = clock();
SORTING_ALGO_CALL(arr, length);
//SECOND READ, PRINT AND NEXT FIRST READ
time2 = clock();
time_taken = (double)(time2 - time1) / CLOCKS_PER_SEC;
printf("\nSorting array = %f\n", time_taken);
time1 = clock();
/*
for (int i = 0; i < length; i++){
printf("%d ", arr[i]);
}
*/
//SECOND READ, PRINT AND NEXT FIRST READ
time2 = clock();
time_taken = (double)(time2 - time1) / CLOCKS_PER_SEC;
printf("\nPrinting Sorted array = %f\n", time_taken);
time1 = clock();
free(arr);
//SECOND READ, PRINT
time2 = clock();
time_taken = (double)(time2 - time1) / CLOCKS_PER_SEC;
printf("\nFreeing array = %f\n", time_taken);
return 0;
}
merge2.c
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SORTING_ALGO_CALL merge_sort
int main()
{
clock_t time1, time2;
double time_taken;
//FIRST READ
time1 = clock();
int length;
scanf("%d", &length);
while (length < 1)
{
printf("\nYou entered length = %d\n", length);
printf("\nEnter a positive length: ");
scanf("%d", &length);
}
//SECOND READ, PRINT AND NEXT FIRST READ
time2 = clock();
time_taken = (double)(time2 - time1) / CLOCKS_PER_SEC;
printf("\nReading length = %f\n", time_taken);
time1 = clock();
int *arr;
if ((arr = malloc(sizeof(int) * length)) == NULL)
{
perror("The following error occurred");
exit(-1);
}
//SECOND READ, PRINT AND NEXT FIRST READ
time2 = clock();
time_taken = (double)(time2 - time1) / CLOCKS_PER_SEC;
printf("\nAllocating array = %f\n", time_taken);
time1 = clock();
for (int i = 0; i < length; i++){
scanf("%d", &arr[i]);
}
//SECOND READ, PRINT AND NEXT FIRST READ
time2 = clock();
time_taken = (double)(time2 - time1) / CLOCKS_PER_SEC;
printf("\nReading array = %f\n", time_taken);
time1 = clock();
SORTING_ALGO_CALL(arr, length);
//SECOND READ, PRINT AND NEXT FIRST READ
time2 = clock();
time_taken = (double)(time2 - time1) / CLOCKS_PER_SEC;
printf("\nSorting array = %f\n", time_taken);
time1 = clock();
/*
for (int i = 0; i < length; i++){
printf("%d ", arr[i]);
}
*/
//SECOND READ, PRINT AND NEXT FIRST READ
time2 = clock();
time_taken = (double)(time2 - time1) / CLOCKS_PER_SEC;
printf("\nPrinting Sorted array = %f\n", time_taken);
time1 = clock();
free(arr);
//SECOND READ, PRINT
time2 = clock();
time_taken = (double)(time2 - time1) / CLOCKS_PER_SEC;
printf("\nFreeing array = %f\n", time_taken);
return 0;
}
Both merge1.c and merge2.c contain the 2 functions of merge-sort.
The file that I am using for generating worst-case(decreasing order) input for the 2 files.
#include<stdio.h>
int main()
{
int j = 100000;
printf("%d\n", j);
for(int i = j; i > 0; i--)
printf("%d\n", i);
return 0;
}
Timing result for merge1.c
Reading length = 23.055000
Allocating array = 0.000000
Reading array = 0.010000
Sorting array = 0.020000
Printing Sorted array = 0.000000
Freeing array = 0.000000
Timing result for merge2.c
Reading length = 22.763000
Allocating array = 0.000000
Reading array = 0.020000
Sorting array = 0.020000
Printing Sorted array = 0.000000
Freeing array = 0.000000