Sort Problems

so i have this code with a couple of sorts in them, the sorts where working fine up until the end when i work with a list that is already in descending order. for some reason the list gets all screwy and i have no idea why.
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;
}
}

here is the out-put
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
 Original data: H
10000  9750  9500  9250  9000  8750  8500  8250  8000  7750  7500  7250  7000  6750  6500  6250  6000  5750  5500  5250  5000  4750  4500  4250  4000  3750  3500  3250  3000  2750  2500  2250  2000  1750  1500  1250  1000  750  500  250
Insert sorted data: Descending
The elapsed time was 0.28 seconds
2  252  502  752  1002  1252  1502  1752  2002  2252  2502  2752  3002  3252  3502  3752  4002  4252  4502  4752  5002  5252  5502  5752  6002  6252  6502  6752  7002  7252  7502  7752  8002  8252  8502  8752  9002  9252  9502  9752

Quick sort data: Descending
The elapsed time was 0.05 seconds
0  250  500  750  1000  1250  1500  1750  2000  2250  2500  2750  3000  3250  3500  3750  4000  4250  4500  4750  5000  5250  5500  5750  6000  6250  6500  6750  7000  7250  7500  7750  8000  8250  8500  8750  9000  9250  9500  9750

Selection sort data: Descending
The elapsed time was 0.16 seconds
0  250  500  750  1000  1250  1500  1750  2000  2250  2500  2750  3000  3250  3500  3750  4000  4250  4500  4750  5000  5250  5500  5750  6000  6250  6500  6750  7000  7250  7500  7750  8000  8250  8500  8750  9000  9250  9500  9750

Original data: A
92  54  42  38  40  73  81  6  97  66  59  24  71  16  74  45  45  68  78  93  91  62  77  4  29  21  60  78  50  69  39  66  1  52  98  95  54  41  18  40
Insert sorted data: Random
The elapsed time was 0.14 seconds
0  2  5  7  10  12  15  17  20  22  25  27  30  32  35  38  40  42  44  47  49  52  54  57  59  62  64  67  70  72  75  78  80  83  86  88  90  92  95  97

Quick sort data: Random
The elapsed time was 0.01 seconds
0  2  5  7  10  12  15  17  20  22  25  27  30  32  35  38  40  42  44  47  49  52  54  57  59  62  64  67  70  72  75  78  80  83  86  88  90  92  95  97

Selection sort data: Random
The elapsed time was 0.16 seconds
0  2  5  7  10  12  15  17  20  22  25  27  30  32  35  38  40  42  44  47  49  52  54  57  59  62  64  67  70  72  75  78  80  83  86  88  90  92  95  97
Original data: B
10000  250  500  750  1000  1250  1500  1750  2000  2250  2500  2750  3000  3250  3500  3750  4000  4250  4500  4750  5000  5250  5500  5750  6000  6250  6500  6750  7000  7250  7500  7750  8000  8250  8500  8750  9000  9250  9500  9750
Insert sorted data: Ascending
The elapsed time was 0.29 seconds
9998  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000

Quick sort data: Ascending
The elapsed time was 0.13 seconds
0  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000

Selection sort data: Ascending
The elapsed time was 0.17 seconds
0  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000  10000

That's all, folks!
  

Topic archived. No new replies allowed.