C++ Sorting Algorithms [closed] - algorithm

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 5 years ago.
Improve this question
A question has arisen on an old exam paper used for revision which asks about a type of sorting that I cannot find the name of anywhere. Hopefully somebody here can help, please?
b. Produce an algorithm which will sort an array so that the largest
items are on the ends and the smallest in the middle. For example:
[2,6,5,9,12] might become [12,6,2,5,9]

Make one pass through the sequence to find the largest value, the second largest value, and the smallest value. Swap the largest to one end, the second largest to the other end, and the smallest to the middle. Voila: largest items are on the ends and the smallest is in the middle. Calling this a "sort" is silly.

I guess the point is to create the algo yourself:
Just an idea:
biggest = value of first element
smallest= value of first element
For all elements of the array do:
If value of current element > biggest
biggest = value of current element
Add biggest as last element of the array
If value of current element < smallest
smallest = value of current element
End of for loop
Move last element of the the array at first position
#now the biggest is the first element, the second bigger number is the last one
Put smallest at middle position of the array [idx max / 2 rounded up]
# now the smallest is in the middle
I hope it helps.
Thomas

Related

Delete every odd index in array until number of elements in array become 1 [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
You have an Array of size n=10;
arr[]={1,2,3,4,5,6,7,8,9,10}
Operation 1:- Delete every odd index
:- 1,3,5,7,9
Now arr[]={2,4,6,8,10}
Repeat Operation 1 Unless size of array become one
in this case Answer is :-8
Is there any formula threw which i can directly fine out this.
1) Assuming indices are starting from 1.
then,
Mathematical interpretation:
Index of the element that will stay up to the end = 2^(⌊log2 n⌋)
where ⌊n⌋ is: floor of log of n base 2.
And, n is the size of the array.
Index of the element at the end will always be the highest 2^k <= n possible, where k is a positive integer.
For example: If n=20 then the index of the element which will make up to the last will be 16.
that is, 2^4
If n=40, the answer will be 32 ==> 2^5
2) If Indices starts from zero, then the 0th element will stay until the end.

Perform 3 operations on stack [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
I am given an empty stack , I need to support three operations :
PUSH x : Push element x onto the stack
POP : Pop the top element
INC L R x : Increment the L to R elements by x
After each query I need to tell the top element of array. How to do this question if their can be 10^6 queries.
We can't update all the elements again and again. So please provide an efficient solution.
We can use a segment tree that supports your required operations in O(log n):
Increment all elements in a given range
For each node in your segment tree associated with an interval included in your given range, increment a counter num_increments for it: this counter will tell you how many times the elements in this range were all incremented. Only do this for the topmost such nodes, do not recursively go down to their children once you've done this.
Query the value at a given index
The answer to this is v[index] + number_of_increments. You can find the number of increments by finding the node associated with the index in the segment tree and keeping track of its parents' num_increments values as you walk down to it.
There are a couple of things to consider, depending on your exact problem:
For a given L, R, maybe set R = min(R, stack.Size), as it makes no sense to increment elements not yet in the stack. Or maybe it does for your problem, I don't know. If it does make sense for your problem, it makes things easier, and it invalidates my second point below;
What happens when you pop an element from the stack? This method will still mark its position as incremented, so if you push one back, it will consider it incremented by 1. Think about how you can also support decrement for a given index (it's similar to the query operation).
Incrementation by x instead of 1 should be easy to achieve.
There will be more push than pop operations, otherwise the stack would be empty in the end. Look for the last push that doesn't have a corresponding pop, this is the element that will be on top of the stack in the end. Now simply increment this element for each appropriate inc operation.
Complexity for this method:
O(2n) computation
O(queries) memory
n = total number of operations of all queries

What's the minimal column sums difference? [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
Imagine you are given a matrix of positive integer numbers (maximum 25*15, value of number does not exceed 3000000). When you do column sums and pick the smallest and the largest one, the difference between them must be the smallest possible.
You can swap numbers in every row (permute rows), not in column, how many times you want.
How would you solve this task?
I'm not asking for your code but your ideas.
Thanks in advance
I would make an attempt to solve the problem using Simulated Annealing. Here is a sketch of the plan:
Let the distance to optimize the difference between the max and min column sums.
Set the goal to be 0 (i.e., try to reach as close as possible to a matrix with no difference between sums)
Initialize the problem by calculating the array of sums of all columns to their current value.
Let a neighbor of the current matrix be the matrix that results from swapping two entries in the same row of the matrix.
Represent neighbors by their row index and two swapping column indexes.
When accepting a neighbor, do not compute all sums again. Just adjust the array of sums in the columns that have been swapped and by the difference of the swap (which you can deduce from the swapped row index)
Step 6 is essential for the sake of performance (large matrices).
The bad news is that this problem without the limits is NP-hard, and exact dynamic programming at scale seems out of the question. I think that my first approach would be large-neighborhood local search: repeatedly choose a random submatrix (rows and columns) small enough to be amenable to brute force and choose the optimal permutations while leaving the rest of the matrix undisturbed.

Pseudocode - Largest to smallest integer [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Write a program that will read in five positive integers (one at a time) and print out the largest and smallest number of the five numbers. The program should read the numbers one at a time.
Mind you this is pseudocode and not to be done in any language.
My question is how would I go about setting this up so that the 5 integers save as values so I can display them.
Don't want the answer, just a start.
How would you solve the same problem if you had to only report the largest number? The pseudocode would be something like the following
consider the first number to be largest
for each of the rest of the number
if it is larger then the current largest
assign to largest
How would you do it if there were two?
consider the first number to be largest
if second number is larger then the largest
consider the second number to be largest, first to be 2nd largest
else
consider the first number to be largest, second to be 2nd largest
for each of the rest of the numbers
if it is larger then the largest
consider current largest to be 2nd largest and this number to be largest
else if it is larger then the 2nd largest
consider it to be 2nd largest
But if there are three or more this can get ugly. How do we keep N largest number? Clearly, we need a list of N sorted number. I will leave it to you how to maintain that list, but here's a pseudocode using that approach
populate the top-list with first N numbers from input, ensure the top-list is sorted
for each of the rest of the numbers
if the number is larger then any number in the top-list
insert it at the right place in top list, pushing out the smallest element of the top list
The question now is: is this better than sorting the list and picking up the top N and bottom N elements?
The answer is that "it depends". Can you figure out some circumstances where one approach is better then the other?
As you read the numbers, keep track of the currently largest and smallest numbers, and update the values as the input is coming in. This has the advantage that it works for even long sequences of numbers. I mean something like this:
min = 0
max = 0
while input:
read number from input
if number < min:
min = number
if number > max:
max = number

Proposing an O(logm) algorithm for the following [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 8 years ago.
Improve this question
I need to propose an algorithm for the following: let us assume that we have an array consisting of zeros and ones. The array is filled with zeros from the beginning of the array to the index m, and all remaining indexes are filled with ones. I need to find this index m in O(logm) time. Here is what i thought: I think this is like binary search, first i look at the middle element of the array, if that is zero, then i forget about the left part of the array and do the same for the right part, and continue like this until i encounter a one. If the middle element is one, then i forget about the right part and do the same for left part of the array. Is this a correct O(logm) solution? Thanks
It is not "like" a binary search - it is a binary search. Unfortunately, it is O(logN), not O(logM).
To find the borderline in O(logM), start from the other end: try positions {1, 2, 4, 8, 16, ... 2^i} and so on, until you hit a 1. Then do a binary search on the interval between 2^i and 2^(i+1), where 2^i+1 is the first position where you discovered a 1.
Finding the first 1 takes O(logM), because the index is doubled on each iteration. After that, the binary search takes another O(logM), because the length of the interval 2^i..2^(i+1) is less than M as well.

Resources