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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// Sort program.
// The numbers to be sorted will be random integers in the range 0..99. // The number of integers to be sorted is declared in constant K below.
// Function prototypes
void selsort (int [], int);
void quicksort(int ,int,int[]);
void insertsort(int [],int);
void swap(int [], int[]);
void print (int [], int);
// --------------------------------------------------------
// Here's the calling program to test the sort.
int main()
{int i;
const int K = 10000; // Number of items to be sorted.
int a[K];
int b[K];
int h[K];
unsigned seed = time(0);
// Seed the random number generator.
srand(seed);
for(int i=0;i<K-1;i++)
a[i]=rand()%100;
for(int i=K;i>=0;i--)
h[K-i]=i;
for(int i=0;i<K-1;i++)
b[i]=i;
double t1, t2,t3,t4,t5,t6,elapsed_time;
// Put the elements to be sorted in the array.
cout << "Original data: H" << endl;
print (h, K);
//sort the data when it is in descending order
t1=clock();
insertsort(h, K);
t2=clock();
cout << "Insert sorted data: Descending" <<endl;
elapsed_time = t2 - t1;
cout<<"The elapsed time was "<<elapsed_time/CLOCKS_PER_SEC<<" seconds"<<endl;
print(h,K);
cout<<endl;
t3=clock();
quicksort(0,K,h);
t4=clock();
cout<<"Quick sort data: Descending"<<endl;
elapsed_time = t4 - t3;
cout<<"The elapsed time was "<<elapsed_time/CLOCKS_PER_SEC<<" seconds"<<endl;
print(h,K);
cout<<endl;
t5=clock();
selsort(h,K);
t6=clock();
cout<<"Selection sort data: Descending"<<endl;
elapsed_time = t6 - t5;
cout<<"The elapsed time was "<<elapsed_time/CLOCKS_PER_SEC<<" seconds"<<endl;
print(h,K);
cout<<endl;
// Sort the data when the data is randomized
cout << "Original data: A" << endl;
print (a, K);
t1=clock();
insertsort(a, K);
t2=clock();
cout << "Insert sorted data: Random" <<endl;
elapsed_time = t2 - t1;
cout<<"The elapsed time was "<<elapsed_time/CLOCKS_PER_SEC<<" seconds"<<endl;
print(a,K);
cout<<endl;
t3=clock();
quicksort(0,K,a);
t4=clock();
cout<<"Quick sort data: Random"<<endl;
elapsed_time = t4 - t3;
cout<<"The elapsed time was "<<elapsed_time/CLOCKS_PER_SEC<<" seconds"<<endl;
print(a,K);
cout<<endl;
t5=clock();
selsort(a,K);
t6=clock();
cout<<"Selection sort data: Random"<<endl;
elapsed_time = t6 - t5;
cout<<"The elapsed time was "<<elapsed_time/CLOCKS_PER_SEC<<" seconds"<<endl;
print(a,K);
//sort the data when it is in ascending order
cout << "Original data: B" << endl;
print (b, K);
t1=clock();
insertsort(b, K);
t2=clock();
cout << "Insert sorted data: Ascending" <<endl;
elapsed_time = t2 - t1;
cout<<"The elapsed time was "<<elapsed_time/CLOCKS_PER_SEC<<" seconds"<<endl;
print(b,K);
cout<<endl;
t3=clock();
quicksort(0,K,b);
t4=clock();
cout<<"Quick sort data: Ascending"<<endl;
elapsed_time = t4 - t3;
cout<<"The elapsed time was "<<elapsed_time/CLOCKS_PER_SEC<<" seconds"<<endl;
print(b,K);
cout<<endl;
t5=clock();
selsort(b,K);
t6=clock();
cout<<"Selection sort data: Ascending"<<endl;
elapsed_time = t6 - t5;
cout<<"The elapsed time was "<<elapsed_time/CLOCKS_PER_SEC<<" seconds"<<endl;
print(b,K);
cout<<endl;
cout <<"That's all, folks!" << endl;
system("PAUSE");
return 0;
}
// Function to print a[0]..a[n-1], where a is an array of ints.
void print (int a[], int n)
{int i;
for (i = 0; i < n-1; i=i+250)
cout << a[i] << " ";
cout << endl;
}
// This is a selection sort.
void selsort (int a[], int n)
{ int i, j, small; // small is the location of the smallest element.
int temp;
for (i = 0; i < n-1; ++i)
{
small = i; // Find the smallest element in a[i]..a[n-1]
for (j = i+1; j<n-1; ++j)
if (a[j] < a[small])
small = j;
temp = a[i]; // and swap it with a[i]
a[i] = a[small];
a[small] = temp;
}
}
void quicksort(int l,int r,int a[])
{
int i,j,pivot;
if(l>=r)
return;
// cout<<"L - "<<l<<" R = "<<r<<endl;
i=l+1;
j=r;
pivot=a[l];
do{
while(a[i] <= pivot && i<r)
{
i++;
}
while(a[j] > pivot)
{
j--;
}
if(i<j)
{
int tmp;
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
while(i<j);
a[l] = a[j];
a[j] = pivot;
if(i==j){
i++;
}
if(l==j){
l--;
}
quicksort(l,j-1,a);
quicksort(i,r,a);
}
void insertsort(int a[],int n)
{
for(int i=0;i<n-1;i++)
{
int t;
t=a[i];
int j=i-1;
while(j>=0 && t<a[j])
{
a[j+1]=a[j];
j--;
}
if(j<=0)
a[0]=t;
else
a[j+1]=t;
}
}
|