Minimal swaps for insertion sort [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 2 years ago.
Improve this question
I was solving a question in codeforces,which was about insertion sort,
the link:the problem
I am unable to solve the question the editorial says
old - 2 * (di, ai + dj, aj - di, aj - dj, ai) - 1
I am not understanding the solution kindly help!
here is the editorial link:
https://codeforces.com/blog/entry/9584

For an insertion sort into a linked list, the number of "swaps" is maximum of 2, if you consider inserting a new item as "swapping pointers". The minimal number would be zero, if the key already exists; otherwise you'll swap one pointer when inserting before the head or after the tail.
The concept of an insertion sort is to keep the container sorted. Most of the time is spent searching for the proper place in the container to insert the new datum.
For an array container, you would need to move elements in order to make room for the new data; which doesn't involve swapping.

Related

O(n/2) search in linked list [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 6 months ago.
Improve this question
I'm supposed to make a search method for a linked list that has time complexity O(n/2). What would this look like? I heard that saying O(n/2) is the same as O(n). So is it just a search of all of the linked list's items? Or is there a specific sorting algorithm that would help me do this?
O(n/2) is indeed equivalent to O(n) so an O(n/2) search is just a linear search i.e. iterate over the list and test each item until you find the one you are looking for.
If you are confused why O(n) and O(n/2) are equivalent, see my answer here.

c++ stl containers:where can we use them [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 4 years ago.
Improve this question
So we know that the basic structures which form the backbone of C++ algorithms are:
trees set
queue
linkedlist
array
vector map
unordered_map and pair.
My question is which data structure is suitable for which application.For instance I know that for Database indexing and searching preferred choices are B+ tree and Hash table.Can anyone shed some more light on this,
This is not only a C++ problem, but also an algorithm question. It maybe too broad, but I can give you some advice.
set and map: They are ordered container, it is used for a both manytimes-insert-and-read structure. It can finish insert delete read in O(logn) time.
vector: used for something like dynamic array or a structure you will frequently push_back at it, and if no other reason, you should use it.
deque: much like vector, but it can also finish push_front in O(1) time
list: used for a structure you need to frequently insert, but less random access
unordered_map and unordered_set: look for hash table
array: used for a structure whose size is fixed.
pair and tuple: bind many object into one struct. Nothing special
Beside all of this, there are also some container meeting other requirement, you can serach them.
e.g. any and optional

looping through a deque using a recursive function in c++ [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
I am trying to create a recursive function in c++ that takes in a deque of integers as a parameter, loops through each element one by one, and returns the deque. I have found a few previous posts on StackOverflow that do something similar, but I am unable to understand what is happening in their answers. I am relatively new to C++. While it may be far easier and more efficient to do this by using an iterative algorithm, I am required to use recursion (it's an assignment question). Help is greatly appreciated.
it should be something like this
deque <int> x;
void Calc (deque <int> d){
if (d.empty()) return;
x.push_back(d.front());
d.pop_front();
Calc(d);
}

How do I know which the average search by value complexity of a data structure? [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
Which of the following containers have average search by value complexity equal to O(log(n))?
Which ones have O(n) and O(n^2)?
std::vector
std::list
std::deque
std::set
std::multiset
std::unordered_set
std::unordered_multiset sorted
std::vector sorted
std::list sorted
std::deque sorted
The standard specifies the complexity of the relevant functions. Get a copy, or read a good reference. (See e.g. http://en.cppreference.com/w/cpp/container/unordered_set/find.)
They're all pretty much what you'd expect.

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.