1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
const int dummy=30;
void printem(double arr[],int nums, ofstream &outf)
{
int i, j=4;
for (i=0; i<nums; i++)
{
outf<<arr[i]<<" ";
if(i==j)
{
j=j+5;
outf<<endl;
}
}
}
void readem(double arr[], int &nums)
{
int i=0;
ifstream inf;
inf.open ("Q.data");
while(!inf.eof())
{
inf>>arr[i];
i++;
}
nums=i;
}
void swapem(double &a, double &b)
{
double temp;
temp=a;
a=b;
b=temp;
}
void quicksort(int left, int right, double a[])
{
int j=left;
int k=right;
double pivot=a[left];
if(left<right)
{
do
{
do
{
j++;
}
while(a[j]<=pivot && j<k);
do
{
k--;
}
while(a[k]>=pivot && k>0);
if(j<=k)swapem(a[j], a[k]);
}
while(j<k);
swapem(a[left], a[k]);
cout<<j<<endl;
quicksort(left, k, a);
quicksort(j+1, right, a);
}
}
void main()
{
ofstream outf;
outf.open("Q.out");
double arr[dummy+1];
int i, nums;
readem(arr, nums);
outf<<"--------------Data unsorted-----------"<<endl;
printem(arr, nums, outf);
int left=0, right=nums;
quicksort(left, right, arr);
outf<<"---------------data Sorted-----------------"<<endl;
printem(arr, nums, outf);
system("pause");
}
|