读取文件内的数据(数字)并进行三种排序,1(快速排序)2(归并排序)3(希尔排序)。
来源:互联网 发布:网络登山鞋 编辑:程序博客网 时间:2024/06/09 23:51
#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
阅读全文
1 0
- 读取文件内的数据(数字)并进行三种排序,1(快速排序)2(归并排序)3(希尔排序)。
- 排序(希尔排序,堆排序,归并排序,快速排序)
- 排序(插入排序,希尔排序,归并排序,快速排序)
- 排序算法二(归并排序、快速排序、希尔排序)
- JavaScript排序算法(希尔排序、快速排序、归并排序)
- 常见比较排序算法的实现(归并排序、快速排序、堆排序、选择排序、插入排序、希尔排序)
- Java高级排序(希尔排序,快速排序,堆排序,归并排序,桶排序)
- 几种常用的排序算法的分析及java实现(希尔排序,堆排序,归并排序,快速排序,选择排序,插入排序,冒泡排序)
- 排序(三)堆排序、归并排序、快速排序
- 常见排序集合(冒泡排序,选择排序,直接插入排序,二分插入排序,快速排序,希尔排序,归并排序)
- java五种内部排序(直接插入排序、希尔排序、快速排序、堆排序、归并排序)
- 排序(希尔、 堆 归并 快速)
- 各类排序算法实现(堆排序、希尔排序、快速排序、归并排序等)
- 经典排序算法(希尔排序,归并排序,快速排序,插入排序)
- 数据结构-排序算法详解(插入排序,希尔排序,堆排序,归并排序,快速排序,桶式排序)
- 排序(2)二分排序、快速排序、归并排序
- 内排序(插入排序、冒泡排序、选择排序、shell排序、快速排序、归并排序、堆排序)
- 数据结构与算法:七种排序算法总结(冒泡排序、选择排序、直接插入排序、希尔排序、堆排序、归并排序、快速排序)
- 集合总结
- PowerDesigner导出Excel
- Java爬虫——代理IP
- mac电脑修改网卡mac地址
- MyBatis中order by排序无效的问题
- 读取文件内的数据(数字)并进行三种排序,1(快速排序)2(归并排序)3(希尔排序)。
- SpringBoot中启动HTTPS
- windows查询文件中是否存在字符串
- 大数据量的算法面试题
- mybatis if test 字符串判断问题
- vue.js 监控 视频播放
- DAY02
- UE4学习笔记(2)
- 内容提供者的使用(消除通话记录)