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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#include <iostream>
#include <fstream>
using namespace std;
const int maxs = 10000;
void readem(double a[])
{
int id;
ifstream inf;
inf.open("indata.dat");
while(!inf.eof())
{
for(int i = 0; i <= maxs; i++)
{
inf >> a[i];
i++;
}
}
inf.close();
}
void printem(double a[])
{
int i;
ofstream outf;
outf.open("outdata.dat");
for(i = 0; i <= maxs; i++)
{
switch(i)
{
case 1000:
case 2000:
case 3000:
case 4000:
case 5000:
case 6000:
case 7000:
case 8000:
case 9000:
case 10000:
cout << a[i] << endl;
break;
}
}
}
void bubble(double a[])
{
int j, l;
for(l = 0; l <= maxs-1; l++)
{
for(j = 0; j >= maxs-1; j++)
{
if(a[j] > a[j+1]);
}
}
swap(a[j], a[j+1]);
}
void selection(double a[])
{
int j, k, small;
if(maxs > 1)
{
for(k = 1; k <= maxs -1; k++)
{
small = k;
for(j = k+1; j <= maxs; j++)
{
if(a[j] < a[small]) small = j;
}
if(k != small) swap(a[k], a[small]);
}
}
}
void insert(double a[])
{
int j, k, save;
for(k = maxs -1; k > 1; k++)
{
j = k+1;
save = a[k];
a[maxs + 1] = save;
while(save > a[j])
{
a[j-1] = a[j];
j = j + 1;
}
a[j-1] = save;
}
}
void quick(int left, int right)
{
int j, k;
double a[maxs];
if(left < right)
{
j = left;
k = right + 1;
do
{
do j = j + 1; while(a[j] <= a[left]);
do k = k - 1; while(a[k] >= a[left]);
if(j < k) swap(a[j], a[k]);
}
while(j <= k);
swap(a[left], a[k]);
quick(left, k - 1);
quick(k + 1, right);
}
}
void main()
{
double a[maxs + 1];
int i;
readem(a);
for(i = 0; i < 4; i++)
{
readem(a);
switch(i)
{
case 0: bubble(a);
cout << "Bubble Sort" << endl;
printem(a);
break;
case 1: selection(a);
cout << "Select Sort" << endl;
printem(a);
break;
case 2: insert(a);
cout << "Insert Sort" << endl;
printem(a);
break;
case 3: quick(0, maxs - 1);
cout << "Quick Sort" << endl;
printem(a);
break;
}
}
}
|