This repository was archived by the owner on Jan 27, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathShellSort.cpp
More file actions
109 lines (90 loc) · 3.44 KB
/
Copy pathShellSort.cpp
File metadata and controls
109 lines (90 loc) · 3.44 KB
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
#include "ShellSort.h"
#include <fstream>
using namespace std;
//-------------------------------------------------------------------------------------------------
// Function Name: ShellSort
// Purpose: ShellSort constructor initializes the sort table
// Parameters: int* theValues in the unsorted list
// Returns: none
// Pre-conditions: none
// Post-conditions: theList contains a pointer to the unsorted list
//-------------------------------------------------------------------------------------------------
ShellSort::ShellSort(int theCount, int* theList)
{
this->theCount = theCount;
this->theList = theList;
}
//-------------------------------------------------------------------------------------------------
// Function Name: ~ShellSort
// Purpose: ~ShellSort destructor performs house cleaning when the class is destroyed
// Parameters: none
// Returns: none
// Pre-conditions: none
// Post-conditions: none
//-------------------------------------------------------------------------------------------------
ShellSort::~ShellSort()
{
if (theList != nullptr)
delete[] theList;
theCount = 0;
theList = nullptr;
}
//-------------------------------------------------------------------------------------------------
// Function Name: sort
// Purpose: sort the list using the Shell Sort algorithm
// Parameters: none
// Returns: none
// Pre-conditions: theList contains a valid pointer to an array of ints
// Post-conditions: theList is sorted
//-------------------------------------------------------------------------------------------------
void ShellSort::sort()
{
if (theCount <= 1 || theList == nullptr)
return;
int increment = theCount;
do
{
increment = increment / 3 + 1;
for (int insertPtr = 0; insertPtr < theCount; insertPtr++)
{
int tmpPtr = insertPtr;
int tmpValue = theList[insertPtr];
// if the current value is less than the previous swap it
while (tmpPtr >= increment && tmpValue < theList[tmpPtr - increment])
{
// shift all values until either reaching the beging of the list,
// or the values are sorted
theList[tmpPtr] = theList[tmpPtr - increment];
tmpPtr -= increment;
}
// insert the value
theList[tmpPtr] = tmpValue;
}
} while (increment > 1);
}
//-------------------------------------------------------------------------------------------------
// Function Name: saveList
// Purpose: saves the list (sorted or unsorted) to the specified filename
// Parameters: const char* filename is the filename to write the list to
// Returns: true if successful, false otherwise
// Pre-conditions: filename is not null
// Post-conditions: none
//-------------------------------------------------------------------------------------------------
bool ShellSort::saveList(const char* filename)
{
if (filename == nullptr)
return false;
ofstream outFile(filename);
//outFile.open(filename);
if (!outFile.good())
return false;
if (theList == nullptr)
{
theCount = 0;
outFile << "theList was null. nothing to save." << endl;
}
for (int x = 0; x < theCount; x++)
outFile << theList[x] << endl;
outFile.close();
return true;
}