This repository was archived by the owner on Jan 27, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathInsertionSort.cpp
More file actions
105 lines (87 loc) · 3.41 KB
/
Copy pathInsertionSort.cpp
File metadata and controls
105 lines (87 loc) · 3.41 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
#include "InsertionSort.h"
#include <fstream>
using namespace std;
//-------------------------------------------------------------------------------------------------
// Function Name: InsertionSort
// Purpose: InsertionSort 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
//-------------------------------------------------------------------------------------------------
InsertionSort::InsertionSort(int theCount, int* theList)
{
this->theCount = theCount;
this->theList = theList;
}
//-------------------------------------------------------------------------------------------------
// Function Name: ~InsertionSort
// Purpose: ~InsertionSort destructor performs house cleaning when the class is destroyed
// Parameters: none
// Returns: none
// Pre-conditions: none
// Post-conditions: none
//-------------------------------------------------------------------------------------------------
InsertionSort::~InsertionSort()
{
if (theList != nullptr)
delete[] theList;
theCount = 0;
theList = nullptr;
}
//-------------------------------------------------------------------------------------------------
// Function Name: sort
// Purpose: sort the list using the Insertion Sort algorithm
// Parameters: none
// Returns: none
// Pre-conditions: theList contains a valid pointer to an array of ints
// Post-conditions: theList is sorted
//-------------------------------------------------------------------------------------------------
void InsertionSort::sort()
{
if (theCount <= 1 || theList == nullptr)
return;
// consider the first position as already sorted
for (int insertPtr = 1; insertPtr < theCount; insertPtr++)
{
// save the location and value where the insertion will occur
int tmpPtr = insertPtr;
int tmpValue = theList[insertPtr];
// if the current value is less than the previous swap it
while (tmpPtr > 0 && tmpValue < theList[tmpPtr - 1])
{
// shift all values until either reaching the beging of the list,
// or the values are sorted
theList[tmpPtr] = theList[tmpPtr - 1];
tmpPtr--;
}
// insert the value
theList[tmpPtr] = tmpValue;
}
}
//-------------------------------------------------------------------------------------------------
// 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 InsertionSort::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;
}