• 0

Selection Sort with 1D Arrays


Question

I have a small problem. I am trying to read in numbers from a text file and using Selection Sort with them. I have some code so far, but I don't know what to do now. I need some help. Where do I go from here in order to start my Selection Sort?

//Program that demonstrates how to use input and output files

//Libraries used in the program
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <conio.h>

using namespace std;

int main()
{
	//Declaraton of Variable And Arrays
	int Index = 0;
	int NumData = 1;
	vector<string> Array1(NumData);
	vector<string> Array2(NumData);

	//Specifies the Files that are used for Input and Output
	ifstream InFile1("InData.txt");

	//Displays error messageif File can't be opened
	if (InFile1.fail())
	{
		cout << "File could not be opened";
		return(0);
	}

	//Loads Array from Input File - READS ENTIRE LINE
	while (getline(InFile1,Array1[Array1.size()-1]))
	{
		Array1.resize(Array1.size()+1);
	}
	InFile1.close();

	//Removes extra space at the end
	Array1.resize(Array1.size()-1);

	//**********************************************************************
	//Specifies the Files that are used for Input and Output
	ifstream InFile2("InData.txt");

	//Displays error messageif File can't be opened
	if (InFile2.fail())
	{
		cout << "Fle could not be opened";
		return(0);
	}

	//Loads Array from Input File - READS ENTIRE LINE
	while (InFile2 >>Array2[Array2.size()-1])
	{
		Array2.resize(Array2.size()+1);
	}
	InFile2.close();

	//Removes extra space at the end
	Array2.resize(Array2.size()-1);

	//Displays array and outputs it to outfile
	ofstream OutFile("InData.txt");
	cout << "Array: ";
	for (Index = 0; Index < Array2.size(); Index++)
	{
		OutFile << Array2[Index] << endl;

		cout << Array2[Index] << endl;
	}

	return(0);
}

Edited by C++ Guy
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Well now you've read the array into memory, so you need to perform the actual sort. This is the kind of thing that you can find very well explained on the internet by doing a simple Google search, so unless you have any more specific question I see no point in trying to teach it to you here. http://en.wikipedia.org/wiki/Selection_sort .

I'd like to comment on the following code :

//Loads Array from Input File - READS ENTIRE LINE
	while (getline(InFile1,Array1[Array1.size()-1]))
	{
		Array1.resize(Array1.size()+1);
	}
	InFile1.close();

	//Removes extra space at the end
	Array1.resize(Array1.size()-1);

You are declaring Array1 as being a vector of strings. Do you really want to perform a sort on strings, i.e. in alphabetical order? If you have numbers stored as text in a file, you rather declare your arrays as being vector<int>, use a temporary string as a buffer and convert each value to int, one by one as you read the file.

In any case, calling resize to add items is not very good style, as insert automatically expands the vector and is more explicit about your intent.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.