Sorting on pairs in c++ [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
I am writing a code for activity schedule problem. If i store the starting time and finishing time in an array of pairs like pair<int,int>p[10] and after this i need to sort the activities as per finishing time with increasing finishing time,omitted the part where it asks user to input and then make pair for simplicity,hope it won't be a problem for the guy to answer so i apply sort(p,p+n) where n means no of activities ,i don't get like will it sort all starting activities this way or the finishing activities this way.So this is the code.pair <int,int>p[10]
Sort(p,p+6)

When sorting pair, the first items are compared and only when they are equal, the second ones are compared. So this won't sort it by the finishing time but by the starting time. But sort can have a third argument which is a method used for sorting (it returns whether the first argument is before the the second). Pass there a lambda that will compare second items (finishing time).

It sorts the activities in the increasing order of their starting time i.e the first elements in every pair is compared to the first element in second pair and so on. .Suppose (7,9),(0,10),(4,5),(8,9),(4,10),(5,17) are the starting time and finishing time then after sorting the result would be (0,10)(4,5),(4,10),(5,17),(7,9),(8,9).

Related

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.

Whats the difference between the two approaches? [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 3 years ago.
Improve this question
I have this question in my assignment, and the solution states that we must have two pointing indices one at the start and one at the last element and we can progress from last element to the middle and respectively the first element to the middle. However, my solution was to have one index at the middle and another at last, and the last index will just move until the middle while the middle index will move until it reaches the start. But i am confused with what's wrong with my solution
Question:
Two stacks of positive integers are needed: one containing elements with values less than or equal to 1000 and the other containing elements with values larger than 1000. The total number of elements in the small-value stack and the large-value stack combined is never more than 500 at any time, but we cannot predict how many will be in each stack. (Initially both will be empty; later on the stacks could be evenly divided, or all the elements could be in the small-value stack, and so on.) For efficiency reasons, we want to implement both stacks using a single array of size 500. Can you think of a way to do this
Take the number 10 (less than 1000) and 400 numbers from 2001 to 2400 (greater than 1000) and try to put them on the two stacks. You will see that the solution with pointers on the two ends will work. But the solution with one pointer in the middle will not work, one of the stacks will be too small or the small value will be overwritten.

Best possible way to search for a given value among N unsorted numbers [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 6 years ago.
Improve this question
One of my friend has been asked with a question in an interview:
The best possible way to search for a given value among N unsorted numbers in a array.
If the array is unsorted, you need to perform a linear scan of the list. This examines (worst case) every element in the array. Such a search is O(n).
Sorting won't help here, since the best sorts run in O(n log n).
If it was sorted I'd say std::binary_search, but for unsorted, just go with std::find (unless the container you use has a member find; if it does, then use that as it is probably faster).
It depends:
-if you just want to search a single value, the answer given by bush is enough.
-if you know what you want to perform repeated "query", it could be better to perform before some kind of preprocessing,in order to find fastly these value.If you want to know only if a value is contained in the array, you can use structures like hashset,bloom filter,etc..
In other cases,you would like to know also position of items inside the array. In this scenario,you can consider to use an hashmap

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.

Using induction to prove bubble sort is correct [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How can we show, using induction, that bubble sort is correct? How do we choose the invariant to follow throughout the formulation of the proof (this step seems like an arbitrary task to me, so if it can be explained more deeply I would greatly appreciate it)?
I understand that the largest elements will always end up at the end of the list after each iteration, but I don't know how to use this fact to show that the algorithm is correct.
Thanks for the help!
I am not sure if this is what you want, but his is how I see it.
The idea behind bubble sort is that you go though the vector of values (left to right). I am calling this a pass. During the pass pairs of values are checked and swapped to be in correct order (higher right).
During first pass the maximum value will be reached. When reached the max will be higher then value next to it, so they will be swapped. This means that max will become part of next pair in the pass. This repeats until pass is completed and max is left at the right end of the vector.
During second pass the same is true for the second highest value in the vector. Only difference is it will not be swapped with the max at the end. Now two most right values are correctly set.
I every next pass one value will be sorted out to the right.
There are N values and N passes. This means that after N passes all N values will be sorted