Skip to content

Commit 8da50d4

Browse files
committed
adding the code
1 parent 9851ef4 commit 8da50d4

13 files changed

+564
-0
lines changed

221-12c-A2-code/Makefile

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# a simple makefile for compiling C++ files
2+
3+
# optimization is on
4+
CC = c++ -03
5+
# for debuging use this
6+
#CC = c++ -g -O0
7+
8+
all: sort
9+
10+
sort: bubble-sort.o insertion-sort.o selection-sort.o shell-sort.o \
11+
option.o sort.o radix-sort.o
12+
$(CC) -o sort bubble-sort.o insertion-sort.o selection-sort.o \
13+
shell-sort.o option.o sort.o radix-sort.o
14+
15+
option.o: option.h option.cpp
16+
$(CC) -c option.cpp
17+
18+
bubble-sort.o: sort.h bubble-sort.cpp
19+
$(CC) -c bubble-sort.cpp
20+
21+
insertion-sort.o: sort.h insertion-sort.cpp
22+
$(CC) -c insertion-sort.cpp
23+
24+
selection-sort.o: sort.h selection-sort.cpp
25+
$(CC) -c selection-sort.cpp
26+
27+
shell-sort.o: sort.h shell-sort.cpp
28+
$(CC) -c shell-sort.cpp
29+
30+
radix-sort.o: sort.h radix-sort.cpp
31+
$(CC) -c radix-sort.cpp
32+
33+
sort.o: sort.h option.h sort.cpp
34+
$(CC) -c sort.cpp
35+
36+
clean:
37+
/bin/rm -f *.o sort

221-12c-A2-code/bubble-sort.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//============================================================================
2+
// Name : bubble-sort.cpp
3+
// Author :
4+
// Date :
5+
// Copyright :
6+
// Description : Implementation of bubble sort in C++
7+
//============================================================================
8+
9+
#include "sort.h"
10+
11+
void
12+
BubbleSort::sort(int A[], int size) // main entry point
13+
{
14+
num_cmps = 0;
15+
int num_swaps = 1;
16+
int num_sorted = 1;
17+
while(num_swaps > 0 && num_sorted < size)
18+
{
19+
num_swaps = 0;
20+
num_cmps += 2;
21+
for(int n = num_sorted; n < size ; ++n)
22+
{
23+
num_swaps = 0;
24+
num_cmps++;
25+
if(A[n] < A[n-1])
26+
{
27+
int temp = A[n];
28+
A[n] = A[n-1];
29+
A[n-1] = temp;
30+
num_swaps++;
31+
}
32+
}
33+
num_cmps++;
34+
num_sorted++;
35+
}
36+
num_cmps+=2;
37+
}

221-12c-A2-code/insertion-sort.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//============================================================================
2+
// Name : insertion-sort.cpp
3+
// Author :
4+
// Date :
5+
// Copyright :
6+
// Description : Implementation of insertion sort in C++
7+
//============================================================================
8+
9+
#include "sort.h"
10+
11+
void
12+
InsertionSort::sort(int A[], int size) // main entry point
13+
{
14+
/* Complete this function with the implementation of insertion sort algorithm
15+
Record number of comparisons in variable num_cmps of class Sort
16+
*/
17+
}

221-12c-A2-code/option.cpp

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//============================================================================
2+
// Name : option.cpp
3+
// Author : TA's
4+
// Date :
5+
// Copyright :
6+
// Description : Command line processing for the sort program
7+
//============================================================================
8+
9+
#include <iostream>
10+
#include "option.h"
11+
12+
/* initialize options according to command line */
13+
void Option::init(int argc, char** argv)
14+
throw (InvalidArgument)
15+
{
16+
// loop through all the arguments except for the program name itself
17+
for (int i = 1; i < argc; i++)
18+
{
19+
char* p = argv[i];
20+
21+
// if option does not start from dash, report an error
22+
if (*p != '-') throw InvalidArgument();
23+
24+
switch (p[1]) // switch on whatever comes after dash
25+
{
26+
case 'a': if (i+1 < argc) alg = argv[++i][0]; break;
27+
case 'f': if (i+1 < argc) input_file = argv[++i]; break;
28+
case 'o': if (i+1 < argc) output_file = argv[++i]; break;
29+
case 'h': show_help = true; break;
30+
case 'd': show_input = true; break;
31+
case 't': show_time = true; break;
32+
case 'c': show_num_cmps = true; break;
33+
case 'p': show_output = true; break;
34+
default : throw InvalidArgument();
35+
}
36+
}
37+
38+
if (!show_help && alg!='S' && alg!='B' &&
39+
alg!='I' && alg!='H' && alg!= 'R')
40+
throw InvalidArgument();
41+
}
42+
43+
44+
void Option::printUsage() const {
45+
std::cout <<
46+
"Usage: ./sort [-a ALGORITHM] [-f INPUTFILE] [-o OUTPUTFILE] [-h] "
47+
"[-d] [-p] [-t] [-c]\n"
48+
"Read an integer sequence and display the sorted sequence, "
49+
"a program written by\n"
50+
"Author1, Author2 and Author3\n"
51+
"Example: ./sort -a S -f input.txt -o output.txt -t -c -p\n"
52+
" ./sort -h\n"
53+
"Options:\n"
54+
" -a ALGORITHM Use ALGORITHM to sort.\n"
55+
" ALGORITHM is a single character representing an algorithm:\n"
56+
" S (selection sort)\n"
57+
" B (bubble sort)\n"
58+
" I (insertion sort)\n"
59+
" H (shell sort)\n"
60+
" R (radix sort)\n"
61+
" -f INPUTFILE Obtain integers from INPUTFILE instead of STDIN\n"
62+
" -o OUTPUTFILE Place output message into OUTPUTFILE "
63+
"instead of STDOUT\n"
64+
" -h Display this help and exit\n"
65+
" -d Display input: unsorted integer sequence\n"
66+
" -p Display output: sorted integer sequence\n"
67+
" -t Display running time of the chosen algorithm in milliseconds\n"
68+
" -c Display number of element comparisons (excluding radix sort)\n";
69+
}
70+

221-12c-A2-code/option.h

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//============================================================================
2+
// Name : option.h
3+
// Author : TA's
4+
// Date :
5+
// Copyright :
6+
// Description : Command line processing for the sort program
7+
//============================================================================
8+
9+
#ifndef OPTION_H_
10+
#define OPTION_H_
11+
12+
#include <stdexcept>
13+
14+
class Option {
15+
public:
16+
// user defined exception class
17+
struct InvalidArgument:public std::invalid_argument {
18+
InvalidArgument():std::invalid_argument(
19+
"Usage: ./sort [-a ALGORITHM] [-f INPUTFILE] [-o OUTPUTFILE] "
20+
"[-h] [-d] [-t] [-c]\n"
21+
"Try `./sort -h' for more information.") {}
22+
};
23+
24+
private:
25+
/// variables representing command line options provided by the user
26+
char alg; /// Algorithm selected
27+
const char* input_file; /// Name of the input file if provided
28+
const char* output_file; /// Name of the output file if provided
29+
bool show_help; /// Whether to show help or not
30+
bool show_input; /// Whether to display unsorted input sequence
31+
bool show_time; /// Whether to display running time
32+
bool show_num_cmps;/// Whether to display number of
33+
/// element comparisons
34+
bool show_output; /// Whether to display sorted input sequence
35+
36+
public:
37+
Option() : alg(0), input_file(NULL), output_file(NULL),
38+
show_help(false), show_input(false), show_time(false),
39+
show_num_cmps(false), show_output(false) {}
40+
41+
/* initialize options according to command line */
42+
void init(int argc, char** argv)
43+
throw (InvalidArgument);
44+
45+
/* accessor functions */
46+
char getAlg() const { return alg; }
47+
const char* getInputFile() const { return input_file; }
48+
const char* getOutputFile() const { return output_file; }
49+
50+
/* query functions */
51+
bool showHelp() const { return show_help; }
52+
bool showInput() const { return show_input; }
53+
bool showTime() const { return show_time; }
54+
bool showNumCmps() const { return show_num_cmps; }
55+
bool showOutput() const { return show_output; }
56+
57+
/* print help message */
58+
void printUsage() const;
59+
};
60+
61+
#endif // OPTION_H_

221-12c-A2-code/radix-sort.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//============================================================================
2+
// Name : radix-sort.cpp
3+
// Author :
4+
// Date :
5+
// Copyright :
6+
// Description : Implementation of radix sort in C++
7+
//============================================================================
8+
9+
#include "sort.h"
10+
#include <iostream>
11+
12+
void
13+
RadixSort::sort(int A[], int size)
14+
{
15+
/*
16+
Complete this function with the implementation of radix sort
17+
algorithm.
18+
*/
19+
}

221-12c-A2-code/selection-sort.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//============================================================================
2+
// Name : selection-sort.cpp
3+
// Author :
4+
// Date :
5+
// Copyright :
6+
// Description : Implementation of selection sort in C++
7+
//============================================================================
8+
9+
#include "sort.h"
10+
#include <iostream>
11+
12+
void
13+
SelectionSort::sort(int A[], int size) // main entry point
14+
{
15+
num_cmps = 0;
16+
int min = 0;
17+
int temp = 0;
18+
for(int n = 1; n < size; ++n)
19+
{
20+
num_cmps++;
21+
min = n-1;
22+
for(int j = n; j < size; ++n)
23+
{
24+
num_cmps++;
25+
if(A[j] < A[min])
26+
min = j;
27+
}
28+
num_cmps++;
29+
temp = A[n];
30+
A[n] = A[min];
31+
A[min] = temp;
32+
}
33+
}

221-12c-A2-code/shell-sort.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//============================================================================
2+
// Name : shell-sort.cpp
3+
// Author :
4+
// Date :
5+
// Copyright :
6+
// Description : Implementation of shell sort in C++
7+
//============================================================================
8+
9+
#include "sort.h"
10+
#include <iostream>
11+
12+
void
13+
ShellSort::sort(int A[], int size)
14+
{
15+
/* Complete this function with the implementation of shell sort algorithm
16+
Record number of comparisons in variable num_cmps of class Sort
17+
*/
18+
}

0 commit comments

Comments
 (0)