How do I sort int without stl liberaries [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
I have a problem where I need to sort buses arriving at a bus station on the basis of time of arrival without using STL (standard template library) in ascending order

You may first want to read about sorting algorithms in general. A good staring point is here.
There you see many of them.
The recommendation for newbies is to start with bubble sort.
Please see here for an example including source code.
Then, you need to store your bus data in a struct. Along with the timing information. All those struct shoulb be stored in an array, best a std::vector.
Then you need to write a compare function for times. The complexity of this depends, if you have one varaible that stores the complete time, like in a unix timestamp, or in a struct, for example tm. Then you need to compare hours, minutes and seconds and some boolean relation.
But first, you need to read a lot, then think even longer on how to implement, and then write the code.

Related

C++ sort() function algorithm [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Some days ago I wanted to use C++ sort() function to sort an array of strings, but I had a problem!
What algorithm does it use to sort the array? Is it a deterministic one or may it use different algorithms based on the type of the array?
Also, is there a clear time complexity analysis about it?
Does this function use the same algorithm for sorting numbers array and strings array?
It might or it might not. That is not specified by the standard.
And if we use it to sort an array of strings which the total size of them is less than 100,000 characters, would it work in less than 1 second(in the worst case)?
It might or it might not. It depends on the machine you're running the program on. Even if it will work in less than 1 second in worst case on a particular machine, it would be difficult to prove. But you can get a decent estimation by measuring. A measurement only applies to the machine it was performed, of course.

Should I use Priority Queues for scheduling tasks (functions, etc.) in a highly dynamic system? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am a hobby programmer interested in dynamic systems like game engines etc. and I had the Idea of a task scheduling system using something like a priority queue to sort different tasks dynamically and maybe include a parallel feature to use multiple cores efficiently. My explicit idea was to use some kind of Task class that itself stores a function pointer and two queue parameters, one being the gravity of the task and one being the time since it was pushed onto the queue, which then would be multiplied to archieve the position in the listing.
Now here comes my question. Would such a system be more efficient in general or at least pay up in any way in comparisation to a hard-coded system (like some 'main loop')?
e.g. is it a better solution / is it faster?
Thanx for the replies.
This is exactly what priority queue's where designed for. Start your design with priority queues and see how well it goes. Then you may want to tweak it if specific issues come up.

Efficiency vs Memory tradeoff [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am creating an interactive sudoku board in c++. Whenever the user changes a value, I would like to check if the board is completed. The board will be completed when all spaces on the board are filled. My two ideas of how to do this are:
Create a private data member that holds the amount of filled spaces. To check if the board is completed I will simply have to check if this value equals boardLength^2
Create a member function that iterates through the board and returns false when a blank space is found and true if it goes through the board without finding any blank spaces
Is this a matter of preference, or is there a more accepted/correct way to do this?
Is this a matter of preference, or is there a more accepted/correct way to do this?
There is an accepted and correct way of optimizing, in general:
Optimize for speed or memory footprint when you actually need to, when you identify an actual problem. Your project's unique requirements will govern what constitutes a "problem".
Otherwise, optimize your code for readability and maintainability.
In your particular case:
Chances are that no matter which algorithm you choose, your check will happen so quickly that you will not be able to measure it, and the user will never notice the difference between the simple solution and the "fast" solution. Any attempts to optimize this (at the cost of complexity or readability or time spent writing code) are poor trade-offs.
Use the simplest possible solution. Once finished, if there is a noticeable delay on user input, and you can confirm that it's caused by an inefficient check for board completion, consider ways to improve your algorithm.

Using a Linked List or Array for Monopoly? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm making a Monopoly game for a school project, written in C++. The first thing I'm working on is implementing the board. It's intuitive to me that each tile will be an object holding information and functions and whatnot, but I cannot decide whether these should be contained in a linked list or array.
It made sense for a linked list because i could simply have the last tile point to the first, but it also seems more efficient to use an array since I can instantly access Tile[5] for example.
Can anybody clarify as to which would be better for this purpose?
It's a fixed size. That negates about 90% of the advantages of a linked list.
You will NEVER be accessing it sequentially (unless, instead of dice, everybody just moves one square each time), but always randomly. That's about 90% of the advantage of an array.
The one reason you cite for using a linked-list is trivially handled differently. (new_position = (current_position + roll) % 40;)
Therefore: You unquestionably want to use an array.

Is it efficient to have 85620 object of a class? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am writing a program which reads from a file and processes the data in the file. Each line in the file is an entity. There are 85620 lines in the file. Is it efficient to define a class of the entity and have 85620 instances of that class?
Depends a bit on the class. But in general, 80k objects is a non-concern.
Yes, especially if you're storing them in a memory-efficient container like std::vector (hint: reserve some space up front if you know you always need at least a thousand or so).
It depends on a lot of factor, and the target software/hardware requirements.
On modern computer with 8GB+ memory, 80k object with even 1K each would not be a big deal, however it may be an issue on mobile phone or embedded systems.
Also note that if you are doing single pass processing and do not need the data afterward, there is no reason to store them.