#include "string.h"
#include "stdlib.h"
#include "stdio.h"
#include "time.h"
int a[1000000];
void load(char filename[]);
int partitions(int arr[], int low, int high);
void qsort(int arr[], int low, int high);
void load(char filename[])
{
int i;
FILE * fp;
fp = fopen(filename, "w");
if(fp == NULL)
{
printf("cannot open file!\n");
return ;
}
for(i=0; i<1000000; i++)
fprintf(fp,"%d ", a[i]);
}
void qsort(int arr[], int low, int high)
{
int pivotkey;
if(low <high)
{
pivotkey = partitions(arr, low, high);
qsort(arr, low, pivotkey-1);
qsort(arr, pivotkey+1, high);
}
}
int partitions(int arr[], int low, int high)
{
int pivotkey = arr[low];
while(low < high)
{
while(low<high && arr[high] >= pivotkey)
--high;
arr[low] = arr[high];
while(low<high && arr[low] <= pivotkey)
++low;
arr[high] = arr[low];
}
arr[low] = pivotkey;
return low;
}
int main(void)
{
int i;
char filename[20];
srand((unsigned)time(NULL));
for(i=0;i<1000000;i++)
a[i] = rand();
strcpy(filename,"out1.txt");
load(filename);
load(filename);
qsort(a, 0, 1000000);
strcpy(filename, "out2.txt");
load(filename);
system("pause");
return 0;
}
#include "stdlib.h"
#include "stdio.h"
#include "time.h"
int a[1000000];
void load(char filename[]);
int partitions(int arr[], int low, int high);
void qsort(int arr[], int low, int high);
void load(char filename[])
{
int i;
FILE * fp;
fp = fopen(filename, "w");
if(fp == NULL)
{
printf("cannot open file!\n");
return ;
}
for(i=0; i<1000000; i++)
fprintf(fp,"%d ", a[i]);
}
void qsort(int arr[], int low, int high)
{
int pivotkey;
if(low <high)
{
pivotkey = partitions(arr, low, high);
qsort(arr, low, pivotkey-1);
qsort(arr, pivotkey+1, high);
}
}
int partitions(int arr[], int low, int high)
{
int pivotkey = arr[low];
while(low < high)
{
while(low<high && arr[high] >= pivotkey)
--high;
arr[low] = arr[high];
while(low<high && arr[low] <= pivotkey)
++low;
arr[high] = arr[low];
}
arr[low] = pivotkey;
return low;
}
int main(void)
{
int i;
char filename[20];
srand((unsigned)time(NULL));
for(i=0;i<1000000;i++)
a[i] = rand();
strcpy(filename,"out1.txt");
load(filename);
load(filename);
qsort(a, 0, 1000000);
strcpy(filename, "out2.txt");
load(filename);
system("pause");
return 0;
}