• 0

Bubble sort using 1D array?


Question

Hi, i need some help to make a prograsm that will read from a file and sort the numbers in lowes to highest order. For example the file will contain 5 4 3 2 1 and i want to to come out as 1 2 3 4 5 . How will i do this? I have some idea where to start from as i have made it so it loads the numbers but i dont know how to properly sort them in order. I know i need to make it so that the first number check with the second number and it flips accordingly and so on.

This si what i have so far.

//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()
{
//Declaration 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 message if 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);

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

			cout << Array1[Index]<<endl;

			}
system("pause");
	 return(0);
}

Link to comment
https://www.neowin.net/forum/topic/773828-bubble-sort-using-1d-array/
Share on other sites

4 answers to this question

Recommended Posts

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

    • No registered users viewing this page.