Question: Lexicographic sort. I am trying to sort a text file lexicographically using Insertion sort, but I do not seem to find. I am able to
Lexicographic sort. I am trying to sort a text file lexicographically using Insertion sort, but I do not seem to find. I am able to sort it using the regular sort(), but I can't seem to get it with insertion sort(). I am also using vectors. I was also able to sort lines of independent strings, but I can't sort a regular text (if that make sense). I am sorting pointers to strings instead of strings themselves. Language: c++
Code:
#include
#include
#include
#include
#include
using namespace std;
void printArray(string *array, int n)
{
for (int i = 0; i
cout
}
void insertionSort(string *arr, int n)
{
int i,j;
string key;
for (i = 1; i
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main(int argc, char *argv[])
{
string input_line;
string temp;
vector value;
ifstream file("TextFile3.txt");
if(file.is_open())
{
while(getline(file, input_line))
{
value.push_back(input_line);
insertionSort(value);
}
for(int size = 0; size
{
cout
}
}
}
TextFile3.txt
//Can't sort this
Once upon a time, there was a king named King James the Great. He ruled over many lands, had many soldiers and many people honored him. But, he was very unsatisfied with his life.
TextFile2.txt
//but can sort this one
Cement is just dangerous
Caio bella
Alexander the great
Alejandro is his name
Call me by your name
That is the end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
