Skip to content

Commit 4b75080

Browse files
committed
shell sort fixed i think
1 parent 0b4b337 commit 4b75080

11 files changed

+666
-0
lines changed

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

bubble-sort.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
bool done = false;
16+
while(!done)
17+
{
18+
done = true;
19+
num_cmps ++;
20+
for(int n = 0; n < size ; ++n)
21+
{
22+
num_cmps++;
23+
if(A[n] < A[n-1])
24+
{
25+
int temp = A[n];
26+
A[n] = A[n-1];
27+
A[n-1] = temp;
28+
done = false;
29+
}
30+
}
31+
num_cmps++;
32+
}
33+
}

generator.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//generates the sequences
2+
3+
#include <fstream>
4+
#include <iostream>
5+
#include <sstream>
6+
#include <cstdlib>
7+
#include <ctime>
8+
#include <cstring>
9+
using namespace std;
10+
11+
main(int argc, char** argv)
12+
{
13+
int length = 100;
14+
bool descending = false;
15+
bool random = true;
16+
string path = "";
17+
if(argc > 1)
18+
stringstream(argv[1]) >> length;
19+
if(argc > 2)
20+
if(strcmp(argv[2], "d")==0)
21+
{
22+
random = false;
23+
descending = true;
24+
}
25+
else if(strcmp(argv[2], "r")==0)
26+
{
27+
random = true;
28+
descending = false;
29+
}
30+
else
31+
{
32+
random = false;
33+
descending = false;
34+
}
35+
if(argc > 3)
36+
{
37+
path = string(argv[3]);
38+
}
39+
ofstream file(path.c_str());
40+
srand(clock());
41+
file<<length<<" ";
42+
while(length > 0)
43+
{
44+
file << (rand())<<" "<< endl;
45+
--length;
46+
}
47+
file.close();
48+
}

insertion-sort.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
#include <ostream>
11+
12+
void
13+
InsertionSort::sort(int A[], int size) // main entry point
14+
{
15+
int j=0;
16+
int temp=0;
17+
int num=0;
18+
for (int i=1; i<size; ++i)
19+
{
20+
temp= A[i];
21+
j = i-1;
22+
for(num=0; (j >= 0) && (A[j] > temp); num+=2)
23+
{
24+
A[j+1] = A[j];
25+
j -= 1;
26+
}
27+
A[j+1]=temp;
28+
num_cmps+=num;
29+
}
30+
}
31+
32+
/*int i=0;
33+
int j=0;
34+
int key=0;
35+
{
36+
for (j=1; j<size; j++)
37+
{
38+
key= array[j];
39+
for (i=j-1; (i>=0) && (array[i]<key); i++)
40+
{
41+
array[i+1]= array[i];
42+
}
43+
array[i+1]=key;
44+
}
45+
return;
46+
}
47+
while(A[i]<A[i-1])
48+
{
49+
temp=A[i-1];
50+
A[i-temp] = A[i];
51+
A[i]=temp;
52+
}
53+
54+
55+
int temp=0;
56+
int j = 0;
57+
for(int i=1; i<size; ++i)
58+
{
59+
temp=A[i];
60+
for(j=i-1; j>=0 && A[j]<temp; ++j)
61+
A[j+1]=A[j];
62+
}
63+
A[j+1]=temp; */
64+

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+

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_

radix-sort.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//============================================================================
2+
// Name : radix-sort.cpp
3+
// Author : Derek Wene
4+
// Date :
5+
// Copyright : Sure, lets say its copyrighted.
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+
int digits=0;
16+
int x=0;
17+
for(int i =0; i<size; ++i)//Find largest number
18+
{
19+
if(A[i]>digits)
20+
digits=A[i];
21+
}
22+
digits=log10(digits);//find highest digit number
23+
int B[10];
24+
int C[size];
25+
int D[size];
26+
pow=1;
27+
while(true)//Get all the digits in the array
28+
{
29+
for(int j=0; j<size; ++j)//Set digits
30+
{
31+
D[j]=(A[j]/(pow))%10;
32+
}
33+
34+
//Counting sort starts here
35+
for(int k=0; k<size; ++k)//Set number of each digit.
36+
{
37+
B[D[k]]++;
38+
}
39+
for(int l=1; l<10; ++l)//Do the less than equal part.
40+
{
41+
B[l]=B[l]+B[l-1];
42+
}
43+
for(int m=0; m<size; ++m)//Actual Sorting Here
44+
{
45+
C[B[D[m]]-1]=A[m];
46+
--B[D[m]];
47+
}
48+
for(int n=0; n<size; ++n)//reset A with new sorted part
49+
{
50+
A[n]=C[n];
51+
}
52+
if(digits=pow/10)//Detects if done with digits
53+
break;
54+
pow*=10;
55+
}
56+
57+
}

0 commit comments

Comments
 (0)