My code is remaining unsorted when i run the program? Also where would i prompt the user to choose a number after or q to quit? -- line 57
I know i need a swap(), but i'm not sure how to implement it, i kept getting errors, a small explanation would be great!
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm> // for std::swap, use <utility> instead if C++11
#include <iterator>
usingnamespace std;
constint SIZE = 200;
int numbers[SIZE];
int count = 0;
int binarySearch(int array[], int size, int value);
void selectionSort(int array[], int size);
int main()
{
// prompt user to enter file name. also display if it exists or not
ifstream inputFile; // gives access to file
string name, filename;
int number;
// Get file name from the user
cout << "Enter the file name: ";
cin >> filename;
// Open the file
inputFile.open(filename.c_str());
// If the file opened successfully, process it
if (inputFile)
{
// Read the numbers from the file and display them.
while (inputFile >> number)
{
cout << number << endl;
}
// Close the file
inputFile.close();
}
else
{
// display an error message if the file was not found
cout << "Error opening the file.\n";
}
return 0;
}
// prompt the user to enter a # to search the array or Q to quit
// sort the array using selection sort
void selectionSort(int array[], int size) // ADD THE COMMENTS BELOW
{
int startScan, minIndex, minValue; // declaration
for(startScan = 0; startScan < (size - 1); startScan++) // search begins from one element below the actual starting point
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan + 1; index < size; index++) // incremented by one value at a time
{
if(array[index] < minValue)
{
minValue = array[index]; // locates element in position 0
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
void selectionSort(int array[], int size) //
{
int startScan, minIndex, minValue; // declaration
bool swap;
do
{
swap = false;
for(startScan = 0; startScan < (size - 1); startScan++) // search begins from one element below the actual starting point
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan + 1; index < size; index++) // incremented by one value at a time
{
if(array[index] < minValue)
{
minValue = array[index]; // locates element in position 0
minIndex = index;
swap = true;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
} while(swap);
}
you are not even calling the function, ¿how do you expect that to work?
> I know i need a swap(), but i'm not sure how to implement it std::swap(array[minIndex], array[startScan]);
1 2 3 4
def swap(a, b): //pseudocode
aux = a
a = b
b = a b = aux //thanks, doug4
try to understand the logic of the algorithm before coding (it's like some pages of your cookbook are stuck together)
in selection sort you have a bag of elements, at each step you extract the minimum element from the bag and so you obtain a sorted sequence
1 2 3 4 5 6 7
def selection_sort(bag):
result = []
while not is_empy?(bag):
min = min_element(bag) //extract the minimum element
bag.erase(min)
result = [result, min] //storing the sorted sequence
return result
so in your first code, lines 67--74 search for the minimum element and lines 75--76 is where you should extract that element from the bag and insert it into the sorted sequence (it's using the same container in this case, if that confuses you just create another array)
Your original code works find if you (1) actually call selectionSort; (2) print the array after sorting it, and (3) move int count; inside main so it doesn't conflict with std::count() in algorithm.
Point #3 demonstrates why it's a bad idea to add usingnamespace std; to your code. It's better to add using statements (directives? They aren't technically statements) for each symbol that you'll use.