-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionSort.cpp
124 lines (103 loc) · 2.47 KB
/
SelectionSort.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
#include <iostream>
#include <fstream>
using namespace std;
//declaring functions
void readFile(int nums[], int size);
void selectionSort(int array[], int size);
int maxNumber(int array[], int size);
int minNumber(int array[], int size);
void removeDuplicates(int array[], int newArray[], int size);
int main()
{
//declare our arrays and array size
const int ARRAY_SIZE = 1024;
int numbers[ARRAY_SIZE];
int newNumbers[ARRAY_SIZE];
//open file, store values into number array
readFile(numbers, ARRAY_SIZE);
//Use a selection sort to sort the array
selectionSort(numbers, ARRAY_SIZE);
//Find the maximum number
int maximumNumber = maxNumber(numbers, ARRAY_SIZE);
cout << "The maximum number is: " << maximumNumber << endl;
//Find the minimum number
int minimumNumber = minNumber(numbers, ARRAY_SIZE);
cout << "The minimum number is: " << minimumNumber << endl;
//Remove the duplicates from the array
removeDuplicates(numbers, newNumbers, ARRAY_SIZE);
//Output the sorted, non-duplicated array
cout << "The numbers are (in order, without duplicates): ";
for (int count = 0; count < ARRAY_SIZE; count++)
{
if (newNumbers[count]!=NULL)
{
cout << newNumbers[count] << " ";
}
}
cout << endl;
return 0;
}
void readFile(int nums[], int size)
{
ifstream inputFile;
inputFile.open("numbers.txt");
int count = 0;
while (count < size && inputFile >> nums[count])
{
count++;
}
inputFile.close();
}
void selectionSort(int array[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size-1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
int maxNumber(int array[], int size)
{
int count;
int highest = array[0];
for (count = 1; count < size; count++)
{
if (array[count] > highest)
highest = array[count];
}
return highest;
}
int minNumber(int array[], int size)
{
int count;
int lowest = array[0];
for (count = 1; count < size; count++)
{
if (array[count] < lowest)
lowest = array[count];
}
return lowest;
}
void removeDuplicates(int array[], int newArray[], int size)
{
int j = 0;
for (int i = 0; i < size; i++)
{
if (array[i] != array[i+1])
{
newArray[j] = array[i];
j++;
}
}
}