1. 程式人生 > >讀取檔案內的資料(數字)並進行三種排序,1(快速排序)2(歸併排序)3(希爾排序)。

讀取檔案內的資料(數字)並進行三種排序,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();
}

}


測試資料:

46
32
32
37
35
24
29
41
29
28