C++ sort() function algorithm [closed] - c++

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.

Related

Is there any optimal way to implement N byte integer and it's arithmetic operations in c++? [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 months ago.
Improve this question
I'm trying to think of some interesting, reusable way to implement big integers using passed amount of bytes or resizing themselves when needed. I have no idea how to make it optimal in any way tho. Are there any tricks I could use, or do I have to simply work on those numbers bit by bit while adding/multiplying/dividing?
edit: if it is important, I need it to safe text as number in base 10 so I can play with some ideas for encrypting it
Use The GNU Multiple Precision Arithmetic Library. If you try to reinvent the wheel you will end up with a square.

How do I sort int without stl liberaries [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 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.

Suppose you have an array of N elements. You need to find for how many i, Ai + A(i+1) is a square number. Is this question trivial? If so how? [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 2 years ago.
Improve this question
I just wanted to know if the above question is trivial or not. More importantly, how can you recognize if an algorithm is trivial?
It depends what do you mean by trivial. If you talk about complexity, it is O(n*M(N)) where M(N) is the complexity of the underlying multiplication algorithm with N maximum of the array's values and n is the length of the array.
If you talk about implementation, it is one loop with one check that the sum of the neighbors is a perfect square. If the elements fit into int, double etc. you have sqrt function in the standard library. If your elements are arbitrary length integers or float point numbers, you either need to use an appropriate library or implement the handling of these numbers on your own, which might be not trivial.
This understanding should help you to answer your last question

Why do we need to learn different Sorting algorithms when the STL sort function is already available to us in C++? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Yesterday, this question came in my mind. Although I have neither read all the sorting algorithms like Quicksort, Merge Sort, Heapsort
Insertion Sort,
Selection Sort, and
Bubble Sort nor I have read the Introduction to Algorithms by CLRS but still, I am curious to know why there is a need to learn all such algorithms when the pre-defined sort function is already available to us in many languages.
Because
Simply sorting only may not be always the requirement. The requirement can be different. You may need to modify / integrate a sorting algorithm in order to develop a completely different thing.
The predefined sorting methods may not be the efficient at all cases.
Its always not about the sorted result but the approach of sorting in order to improve time and space complexity. Efficiency is the key.
There is no particular algorithm that is guaranteed to work best at all cases. Pros and cons may differ for different algorithms.
Need to understand which algorithm to be applied at what scenarios.
Sorting may not always done with numbers. It can be applied on other different complex types / structures. (There may not be pre-defined methods for complex cases )
There is always scope for a better approach.

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.