I've got a problem in sorting. I have 2 functions. The first one insert a number into a sorted list and the second one takes in an unsorted list, named unsorted, creates a new list, and repeatedly calls the first one until the new list is a sorted version of the unsorted input. Then return the sorted list.
My understanding is the very first new list is an empty list, which is kind of "sorted", and insert every element into it one by one. But my code somehow doesn't work. The first function passed the test, but the second one did not. Here's my code:
void insertInOrder(list<int>& sorted, int number)
{
if (sorted.empty())
{sorted.push_back(number);
else
{
for(list<int>::iterator I = sorted.begin();I!= sorted.end();I++)
{
if (*I <=number)
{
sorted.insert((++I),number);// since I increases another time here
--I;//decrease once back
return;
}
else
{
sorted.insert(I,number);
return;
}
}
}
}
std::list<int> reorder(std::list<int>& unsorted)
{
std::list<int> ordered;
for (list<int>::iterator J = unsorted.begin(); J!=unsorted.end(); J++)
{
insertInOrder(ordered, *J);
}
return ordered;
}
I think you want is that if the value to insert is less than or equal to the current node, then do nothing and continue the loop, else insert and return. This can of course be written as: If the value to insert is larger than the current node in the list, insert it after the current node and exit loop.
So
if (number > *I)
{
sorted.insert(++I, number); // Insert after current node
break; // Exit loop
}
Related
I've been assigned this question for my lab (and yes I understand there will be backlash because it's homework). I've been working on this question for a couple of days to no avail and I feel like I'm missing something glaringly obvious.
My code:
int processSuitors(vector<int>& currentSuitors, list<int>& rekt)
{
int sizeSuitors = currentSuitors.size();
int eliminated = 2;
while(sizeSuitors != 1)
{
rekt.push_back(currentSuitors[eliminated]);
currentSuitors.erase(currentSuitors.begin() + eliminated);
sizeSuitors--;
if(eliminated > sizeSuitors)
{
eliminated -= sizeSuitors;
}
}
return currentSuitors[0];
}
Prompt:
In an ancient land, the beautiful princess Eve had many suitors. She decided on the following procedure to determine which suitor she would marry. First, all of the suitors would be lined up one after the other and be assigned numbers. The first suitor would be number 1, the second number 2, and so on up to the last suitor, number n. Starting at the first suitor she would then count three suitors down the line (because of the three letters in her name) and the third suitor would be eliminated from winning her hand and he would be removed from the line. Eve would then continue, counting three more suitors and eliminating every third suitor. When she reached the end of the line she would continue counting from the beginning.
Write a function named processSuitors that takes as arguments an STL vector of type int containing the suitors, and an STL list of type int that will collect all the suitors that are eliminated. The function returns an int storing the position a suitor should stand in to marry the princess if there are n suitors. The function that calls processSuitors will send the vector already filled with n suitors (1, 2, 3... n), and an empty list that needs to be filled with the position number of the suitors that were eliminated, in the order they were eliminated.
Restrictions: You may not create any containers (no arrays, no vectors, etc.); you need to use the vector and the list that are passed as parameters.
Use ONLY the following STL functions:
vector::size
vector::erase
vector::begin
ist::push_back
vector::operator[ ]
The adjacent files are hidden since we are to rely on what is given. Any clean-up of my code would be extremely appreciated as well.
What do you think of this solution.
Keep another vector that marks whether an index in your currentSuitors vector has been removed. Then have a helper function that will always find the next free index.
Instead of trying to reduce currentSuitors, you just keep marking elements in the taken list.
size_t findNextFreeSlot(const vector<bool>& taken, size_t pos)
{
// increment to the next candidate position
pos = (pos + 1) % taken.size();
// search for the first free slot
for (size_t i = 0; i < taken.size(); i++)
{
if (taken[pos] == false)
{
return next;
}
pos = (pos + 1) % taken.size();
}
// assert(false); // we should never get here as long as there's one free slot index in taken
return -1;
}
int processSuitors(vector<int>& currentSuitors, list<int>& rekt)
{
size_t len = currentSuitors.size();
vector<bool> taken(len); // keep a vector of eliminated indices from current
size_t index = len; // initialize one past the last valid element
size_t eliminated = 0;
if (len == 0)
{
return -1;
}
while (eliminated < (len-1))
{
// advance the index three times to the next "untaken" index
index = findNextFreeSlot(taken, index);
index = findNextFreeSlot(taken, index);
index = findNextFreeSlot(taken, index);
taken[index] = true; // claim this index as taken
rekt.push_back(currentSuitors[index]); // add the value at this index to the eliminated list
eliminated++;
}
index = findNextFreeSlot(taken, index); // find the last free index
return currentSuitors[index];
}
I am in the process of creating a C++/SFML game engine. Every "entity" in the game has a pointer to it stored in a static vector in the Entity class, called entityRenderList. This vector is sorted by the Bubble Sort algorithm on each iteration of the game loop so that the sprites are drawn in the correct order.
Whenever an entity is deleted, it replaces its pointer in the vector with a NULL pointer. My algorithm should, by default, cause any NULL pointers it finds to be sorted to the back of the vector, where they are subsequently removed.
Here is the code for the sorting algorithm:
bool Entity::depthSortFunction(Entity* a, Entity* b)
{
if (b==NULL) return false; //any NULL values are moved to the back
if (a==NULL) return true;
else return (a->depth_) < (b->depth_);
}
void Entity::sortEntityRenderList()
{
if (entityRenderList.size()>1) {
//Any NULL values are brought to the top to be stripped off.
bool passMade=false;
Entity* temp;
int n=entityRenderList.size()-1;
for(int i=0; i<n; i++)
{
passMade=false;
for(int j=0; j<n-1; j++)
{
if(depthSortFunction(entityRenderList[j],entityRenderList[j+1]))
{
//then swap them
temp = entityRenderList[j+1];
entityRenderList[j+1] = entityRenderList[j];
entityRenderList[j] = temp;
//and then notify the entities of the change
if (entityRenderList[j]!=NULL) {entityRenderList[j]->renderListID=j;}
if (entityRenderList[j+1]!=NULL) {entityRenderList[j+1]->renderListID=j+1;}
passMade=true;
//std::cout<<"Swapping entries "<<j<<" and "<<j+1<<"...\n";
}
}
if (!passMade) {
break; //then it is sorted, as we have not needed to modify the array.
}
}
}
//Now, we strip off any NULL values from the top.
while (!entityRenderList.empty() && entityRenderList.back()==NULL) {
entityRenderList.pop_back(); //strip off last one
}
}
What should be happening is that any NULL pointers are removed from the vector on each run of the algorithm. However, this is not the case, and any NULL pointers stay right where they are, and appear to not be sorted at all.
NB: The passMade boolean is there so that if a pass of the array is made and no swaps were made, the algorithm stops.
Any help would be appreciated. Thanks in advance.
EDIT: The sorting algorithm code is slightly modified from here.
There is a bug in the j loop limit. For example, if the list has 10 elements, n is 9, n-1 is 8, and the largest value of j is 7. The loop can exchange elements 7 and 8 of a 10 element list. It cannot exchange the last pair, elements 8 and 9.
As suggested by a comment, it would be better and simpler to use a library sort that is already tested and working. Rather than adjust the renderListID fields as you go along, you could do them all in a single pass through the list at the end. If you do it after popping the NULL elements, you would not need to test for NULL in that loop.
for(int i=0; i<entityRenderList.size(); i++)
{
entityRenderList[i]->renderListID=i;
}
I have a single linked phonebook list with last names, first names, and values.
I am able to print them out in the order they were created, but not by value. How can i modify this? If you need to see anything else in the code let me know, but this function is my main concern.
ostream& operator<<(ostream& out, const PhoneBook& p) // out stream
{
if(p.head==NULL)
{
cout << "is empty";
}else
{
PhoneBookItem* item = p.head;
for(int i=0; i < p.num; i++)
{
cout << item->lastname<< " ";
cout << item->firstname<< " : ";
cout << item->phone<<endl;
item = item->next;
}
}
return out;
Option 1: Sort the list, then print
Option 2: For every loop, search which item should be printed next. (Expensive)
Option 3: Use Hash/Dictionary approach instead of linked-list. Hash/Dictionary is
combination of fixed array and link list. They are good for
searching items faster than fixed array and linked-list.
Option 4: Use other data structure other than link list that has ability to access your data in order/alphabetically.
Sorting a linked list can be done in several ways.
Temporary reference array: Allocate a temporary array or vector of pointers and traverse the linked list to fill it. Sort the pointers. Library std::sort or qsort is fine for this. Then traverse the sorted array to reset the "next" pointer of each node. Finally release the temporary array storage.
Insertion sort: Pop elements off the list and re-insert in a new list at the correct sorted location.
Mergesort: It's not too hard to implement, and this runs much faster on long lists than insertion sort. The algorithm is simple: Split the list in 2. Mergsort the halves recursively. Then merge the results by repeatedly removing the smallest head and appending to the tail of a new list.
Quicksort: This is a bit tricky to implement efficiently with lists, but it is possible. I won't discuss it because it's not a good early programming project, and mergesort is faster in many cases.
Here is some untested code for insertion sort:
PhoneBookItem* sorted = NULL;
while (p.head) {
// Pop
PhoneBookItem* head = p.head;
p.head = head->next;
head->next = NULL;
// Find the place to insert.
PhoneBookItem* lead = sorted;
PhoneBookItem* trail = NULL;
while (lead && lead->phone <= head->phone) {
trail = lead;
lead = lead->next;
}
// Insert either within the list or at the head.
head->next = lead;
if (trail)
trail->next = head;
else
sorted = head;
}
p.head = sorted;
// Now print the sorted list as before...
I have a list of Star structs. These structs are in a std::list
I am double looping this list and compairing there locations to detect a collision. When A collision is found I will delete Star with the lowest mass. But how can I delete the Star when I am in the double Loop, and keep the loop going to check for more collisions?
It's worth mentioning that the second loop is a reverse loop.
Here is some code
void UniverseManager::CheckCollisions()
{
std::list<Star>::iterator iStar1;
std::list<Star>::reverse_iterator iStar2;
bool totalbreak = false;
for (iStar1 = mStars.begin(); iStar1 != mStars.end(); iStar1++)
{
for (iStar2 = mStars.rbegin(); iStar2 != mStars.rend(); iStar2++)
{
if (*iStar1 == *iStar2)
break;
Star &star1 = *iStar1;
Star &star2 = *iStar2;
if (CalculateDistance(star1.mLocation, star2.mLocation) < 10)
{
// collision
// get heaviest star
if (star1.mMass > star2.mMass)
{
star1.mMass += star2.mMass;
// I need to delete the star2 and keep looping;
}
else
{
star2.mMass += star1.mMass;
// I need to delete the star1 and keep looping;
}
}
}
}
}
You need to utilize the return value of the erase method like so.
iStar1 = mStars.erase(iStar1);
erase = true;
if (iStar1 == mStars.end())
break; //or handle the end condition
//continue to bottom of loop
if (!erase)
iStar1++; //you will need to move the incrementation of the iterator out of the loop declaration, because you need to make it not increment when an element is erased.
if you don't increment the iterator if an item is erased and check if you deleted the last element then you should be fine.
Since modifying the list invalidates the iterators (so that you cannot increment them), you have to keep safe the iterators before the list is changed.
In the most of the implementation std::list is a dual-linked list, hence a iteration like
for(auto i=list.begin(), ii; i!=list.end(); i=ii)
{
ii = i; ++ii; //ii now is next-of-i
// do stuff with i
// call list.erasee(i).
// i is now invalid, but ii is already the "next of i"
}
The safest way, is to create a list containing all the "collided", then iterate on the "collided" calling list.remove(*iterator_on_collided)
(but inefficient, since has O2 complexity)
You want to use the result of erase() to get the next iterator and advance the loop differently:
If you erase using the outer iterator you clearly can abondon checking this Star against others and break out of the inner loop. Only if the inner loop was complete you'd want to advance the outer iterator because otherwise it would be advanced by the erase().
If you erase using the inner loop you already advanced the iteration, otherwise, i.e. if no star was erased, you need to advance.
Sample code would look somethimg like this:
for (auto oit(s.begin()), end(s.end()); oit != end; )
{
auto iit(s.begin());
while (iit != end)
{
if (need_to_delete_outer)
{
oit = s.erase(oit);
break;
}
else if (need_to_delete_inner)
{
iit = s.erase(iit);
}
else
{
++iit;
}
}
if (iit == end)
{
++oit;
}
}
Goal is, I've multiple lists of elements available, and I want to be able to store all of these elements into a resultant list in an ordered way.
Some of the ideas that comes to my mind are
a) Keep the result as a set (std::set), but the B-tree , needs to rebalanced every now and then.
b) Store all the elements in a list and sort the list at the end.
But, I thought, why not store them in a sorted fashion, as and when we add the items to the resultant list.
Here is my function, that does the job of maintaining the results in a sorted way. Is there an efficient way to do the same?
void findItemToInsertAt(std::list<int>& dataSet, int itemToInsert, std::list<int>::iterator& location)
{
std::list<int>::iterator fromBegin = dataSet.begin();
std::list<int>::iterator fromEnd = dataSet.end() ;
// Have two pointers namely end and begin
if ( !dataSet.empty() )
--fromEnd;
// Set the location to the beginning, so that if the dataset is empty, it can return the appropriate value
location = fromBegin;
while ( fromBegin != dataSet.end() )
{
// If the left pointer points to lesser value, move to the next element
if ( *fromBegin < itemToInsert )
{
++fromBegin;
// If the end is greater than the item to be inserted then move to the previous element
if ( *fromEnd > itemToInsert )
{
--fromEnd;
}
else
{
// We move only if the element to be inserted is greater than the end, so that end points to the
// right location
if ( *fromEnd < itemToInsert )
{
location = ++fromEnd;
}
else
{
location = fromEnd;
}
break;
}
}
else
{
location = fromBegin;
break;
}
}
}
And, here is the caller of the function
void storeListToResults(const std::list<int>& dataset, std::list<int>& resultset)
{
std::list<int>::const_iterator curloc;
std::list<int>::iterator insertAt;
// For each item in the data set, find the location to be inserted into
// and insert the item.
for (curloc = dataset.begin(); curloc != dataset.end() ; ++curloc)
{
// Find the iterator to be inserted at
findItemToInsertAt(resultset,*curloc,insertAt);
// If we have reached the end, then the element to be inserted is at the end
if ( insertAt == resultset.end() )
{
resultset.push_back(*curloc);
}
else if ( *insertAt != *curloc ) // If the elements do not exist already, then insert it.
{
resultset.insert(insertAt,*curloc);
}
}
}
At a glance, your code looks like it's doing a linear search of the list in order to find the place to insert the item. While it's true that std::set will have to balance its tree (I think it's a Red-Black Tree) in order to maintain efficiency, chances are it'll do so much more efficiently than what you're proposing.
Answering the question asked:
Is there an efficient way to do the same?
Yes. Use std::set.
I would sort the indivual lists and then use STL's list::merge to create the result list. Then, if the list is kind of big, you could pay to transfer the result to a vector.