读取文件内的数据(数字)并进行三种排序,1(快速排序)2(归并排序)3(希尔排序)。
#include<iostream>
#include<fstream>
#include<stdlib.h>
int n1=0;
using namespace std;
void Merge(int a[], int low, int mid, int high, int * temp,int size)
{
int i,j,k;
i = low;
j = mid + 1;
k = 0;
while (i <= mid && j <= high)
{
if(a[i] <= a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
while(i <= mid)
temp[k++] = a[i++];
while(j <= high)
temp[k++] = a[j++];
for (i = 0; i < k; i++)
{
a[low+i] = temp[i];
}
cout<<"第"<<n1<<"次排序结果:"<<endl;
n1++;
for(int j=0;j<size;j++)
{
cout<<"\t"<<a[j];
}
cout<<endl;
}
void MergeSort(int a[], int low, int high, int * temp,int size)
{
if (low < high)
{ int mid = (low+high)/2;
MergeSort(a,mid+1,high,temp,size);
MergeSort(a,low,mid,temp,size) ;
Merge(a,low,mid,high,temp,size);
}
}
void shellsort(int a[], int n)
{
int j, gap;
for (gap = n / 2; gap > 0; gap /= 2)
{ n1++;
for (j = gap; j < n; j++)
if (a[j] < a[j - gap])
{
int temp = a[j];
int k = j - gap;
while (k >= 0 && a[k] > temp)
{
a[k + gap] = a[k];
k -= gap;
}
a[k + gap] = temp;
}
cout<<"第"<<n1<<"次排序结果为:"<<endl;
for(int i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
cout<<endl;
}
}
void QuickSort( int array[], int left, int right,int size)
{
int i, j, x;
if (left < right)
{
i = left;
j = right;
x = array[i];
while (i < j)
{
while(i < j && array[j] > x)
j--; // 从右向左找第一个小于x的数
if(i < j)
array[i++]=array[j];
while(i < j && array[i] < x)
i++; // 从左向右找第一个大于x的数
if(i < j)
array[j--]=array[i];
}
array[i] = x;
cout<<"第"<<n1<<"次排序结果:"<<endl;
n1++;
for(int j=0;j<size;j++)
{
cout<<"\t"<<array[j];
}
cout<<endl;
QuickSort(array, left, i-1,size); //递归调用
QuickSort(array, i+1, right,size);
}
}
int main()
{
int n;
cout<<"**********************************************"<<endl;
cout<<" welcone "<<endl;
cout<<" author:毛红晶 郭文博"<<endl;
cout<<endl;
cout<<"操作说明:选择排序的方法:"<<endl;
cout<<"(1:快速排序) (2:归并排序) (3:shell排序)"<<endl;
cout<<"**********************************************"<<endl;
cout<<endl;
while(1)
{
FILE *fp;
char filename[100];
int num[100];
int count=0;
cout<<"please enter the filename:";
gets(filename);
cout<<endl;
fp = fopen(filename,"r");
if(fp == NULL)
{
cout<<"opening the file is error!";
getchar();
exit(0);
}
while(fscanf(fp,"%d",&num[count])!= EOF)
{
count++;
}
fclose(fp);
cout<<"文件中的数据为:";
for(int j=0;j<count;j++)
{
cout<<"\t"<<num[j];
}
cout<<endl;
cout<<endl;
int temp[count];
cout<<"please choose the method of sort:";
cin>>n;
cout<<endl;
switch(n)
{
case 0:
exit(0);
break;
case 1:
QuickSort(num,0,count,count);
break;
case 2:
MergeSort(num,0,count,temp,count);
break;
case 3:
shellsort(num,count);
break;
}
cout<<endl;
cout<<"排序结果:"<<endl;
for(int j=0;j<count;j++)
{
cout<<"\t"<<num[j];
}
cout<<endl;
n1 = 0;
getchar();
}
#include<fstream>
#include<stdlib.h>
int n1=0;
using namespace std;
void Merge(int a[], int low, int mid, int high, int * temp,int size)
{
int i,j,k;
i = low;
j = mid + 1;
k = 0;
while (i <= mid && j <= high)
{
if(a[i] <= a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
while(i <= mid)
temp[k++] = a[i++];
while(j <= high)
temp[k++] = a[j++];
for (i = 0; i < k; i++)
{
a[low+i] = temp[i];
}
cout<<"第"<<n1<<"次排序结果:"<<endl;
n1++;
for(int j=0;j<size;j++)
{
cout<<"\t"<<a[j];
}
cout<<endl;
}
void MergeSort(int a[], int low, int high, int * temp,int size)
{
if (low < high)
{ int mid = (low+high)/2;
MergeSort(a,mid+1,high,temp,size);
MergeSort(a,low,mid,temp,size) ;
Merge(a,low,mid,high,temp,size);
}
}
void shellsort(int a[], int n)
{
int j, gap;
for (gap = n / 2; gap > 0; gap /= 2)
{ n1++;
for (j = gap; j < n; j++)
if (a[j] < a[j - gap])
{
int temp = a[j];
int k = j - gap;
while (k >= 0 && a[k] > temp)
{
a[k + gap] = a[k];
k -= gap;
}
a[k + gap] = temp;
}
cout<<"第"<<n1<<"次排序结果为:"<<endl;
for(int i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
cout<<endl;
}
}
void QuickSort( int array[], int left, int right,int size)
{
int i, j, x;
if (left < right)
{
i = left;
j = right;
x = array[i];
while (i < j)
{
while(i < j && array[j] > x)
j--; // 从右向左找第一个小于x的数
if(i < j)
array[i++]=array[j];
while(i < j && array[i] < x)
i++; // 从左向右找第一个大于x的数
if(i < j)
array[j--]=array[i];
}
array[i] = x;
cout<<"第"<<n1<<"次排序结果:"<<endl;
n1++;
for(int j=0;j<size;j++)
{
cout<<"\t"<<array[j];
}
cout<<endl;
QuickSort(array, left, i-1,size); //递归调用
QuickSort(array, i+1, right,size);
}
}
int main()
{
int n;
cout<<"**********************************************"<<endl;
cout<<" welcone "<<endl;
cout<<" author:毛红晶 郭文博"<<endl;
cout<<endl;
cout<<"操作说明:选择排序的方法:"<<endl;
cout<<"(1:快速排序) (2:归并排序) (3:shell排序)"<<endl;
cout<<"**********************************************"<<endl;
cout<<endl;
while(1)
{
FILE *fp;
char filename[100];
int num[100];
int count=0;
cout<<"please enter the filename:";
gets(filename);
cout<<endl;
fp = fopen(filename,"r");
if(fp == NULL)
{
cout<<"opening the file is error!";
getchar();
exit(0);
}
while(fscanf(fp,"%d",&num[count])!= EOF)
{
count++;
}
fclose(fp);
cout<<"文件中的数据为:";
for(int j=0;j<count;j++)
{
cout<<"\t"<<num[j];
}
cout<<endl;
cout<<endl;
int temp[count];
cout<<"please choose the method of sort:";
cin>>n;
cout<<endl;
switch(n)
{
case 0:
exit(0);
break;
case 1:
QuickSort(num,0,count,count);
break;
case 2:
MergeSort(num,0,count,temp,count);
break;
case 3:
shellsort(num,count);
break;
}
cout<<endl;
cout<<"排序结果:"<<endl;
for(int j=0;j<count;j++)
{
cout<<"\t"<<num[j];
}
cout<<endl;
n1 = 0;
getchar();
}
}
测试数据:
46
32
32
37
35
24
29
41
29
28