forked from dulalsaurab/sorts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.cpp
184 lines (160 loc) · 5.36 KB
/
sort.cpp
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
//============================================================================
// Name : sort.cpp
// Author : TA's
// Date :
// Copyright :
// Description : Read an integer sequence and display the sorted sequence
/*
* Usage: ./sort [-a ALGORITHM] [-f INPUTFILE] [-o OUTPUTFILE] [-h]
* [-d] [-p] [-t] [-c]
* Example: ./sort -a S -f input.txt -o output.txt -t -c -d
* ./sort -h
* Options:
* -a ALGORITHM Use ALGORITHM to sort.
* ALGORITHM is a single character representing an algorithm:
* S (selection sort)
* B (bubble sort)
* I (insertion sort)
* H (shell sort)
* R (radix sort)
* -f INPUTFILE Obtain integers from INPUTFILE instead of STDIN
* -o OUTPUTFILE Place output message into OUTPUTFILE instead of STDOUT
* -h Display this help and exit
* -d Display input: unsorted integer sequence
* -p Display output: sorted integer sequence
* -t Display running time of the chosen algorithm in milliseconds
* -c Display number of element comparisons (excluding radix sort)
*/
//============================================================================
#include <iostream>
#include <iterator>
#include <ctime>
#include <cstdio>
#include "option.h"
#include "sort.h"
using namespace std;
/* read input sequence from STDIN */
int readInput(int A[], int& size) {
/* read integers to sort */
for (int i = 0; i < size; i++)
if (!(cin >> A[i])) {
cerr << "sort: input data error" << endl;
return 1; //exit abnormally
}
return 0; //exit normally
}
/* display elements of array A seperated by new lines */
void printArray(const int A[], int size) {
copy(A, A+size, ostream_iterator<int>(cout,"\n"));// call standard function
// "copy" to display
// the array
cout << endl;
}
// returns true if sorted as increasing sequence
// returns false otherwise
bool Sort::testIfSorted(int A[], int size)
{
for (int i = 1; i < size; ++i)
if (A[i-1] > A[i]) return false;
return true;
}
int main(int argc, char** argv)
{
Option op;
bool radixsortQ = false;
/* initialize program options */
try {
op.init(argc,argv);
} catch (Option::InvalidArgument& ex) {
cerr << ex.what() << endl;
return 1;
}
/* show help message and exit */
if (op.showHelp()) {
op.printUsage();
return 0; //exit normally
}
const char *input_file, *output_file;
/* If provided input file, reopen standard input onto that file
so that we only need to deal with standard input. */
if ((input_file=op.getInputFile()) &&
freopen(input_file, "r", stdin) == 0) {
cerr << "sort: " << input_file << ": no such file" << endl;
return 1;
}
/* If provided output file, reopen standard output onto that file
so that we only need to deal with standard output. */
if ((output_file=op.getOutputFile()) &&
freopen(output_file, "w", stdout) == 0) {
cerr << "sort: " << output_file << ": No such file" << endl;
return 1; //exit abnormally
}
/* read number of integers */
cout << "Enter Size >>>";
int size; //number of integers
if (!(cin >> size)) return 1; //exit abnormally
/* read input integers */
int* A=new int[size]; //stores integers
if (readInput(A,size)) //call global function
return 1; //exit abnormally
/* show unsorted input sequence */
if (op.showInput()) {
cout << "Unsorted sequence:" << endl;
printArray(A,size); //call global function to display the array
}
/* create an sorting object with appropriate algorithm */
Sort* s;
switch(op.getAlg())
{
case 'S':
s=new SelectionSort();
break;
case 'I':
s=new InsertionSort();
break;
case 'B':
s=new BubbleSort();
break;
case 'H':
s=new ShellSort();
break;
case 'R':
s=new RadixSort();
radixsortQ = true;
break;
}
/* begin timing the chosen algorithm using time.h library*/
clock_t start = clock();
/* call sorting function to sort */
s->sort(A,size);
/* end timing */
clock_t finish = clock();
/* output sorted sequence */
if (op.showOutput()) {
cout << "Sorted sequence:" << endl;
printArray(A,size); //call global function to display the array
}
/* show running time of the algorithm to sort input data */
if (op.showTime())
cout << "Running time: "
<< (double)(finish-start)*1000/CLOCKS_PER_SEC
<< " ms" << endl;
/* show number of comparisons in the algorithm */
if (op.showNumCmps()) {
if (radixsortQ) {
cout << "No comparisons for radix sort"
<< endl;
} else {
cout << "# Comparisons: "
<< s->getNumCmps() << endl;
}
}
if (!s->testIfSorted(A, size)) {
cerr << "Warning: The sorted sequence IS NOT sorted!\n"
<< endl;
}
// it may be useful for Windows
// char ch;
// cin >> ch;
return 0;
}