Sort a deck of cards [duplicate] - algorithm

This question already has answers here:
Sorting a deque using limited operations?
(3 answers)
Closed 6 years ago.
Given a deck of N cards. You have to sort them using following permissible operations:
You can see top 2 cards.
You can swap them.
You can insert top at the bottom.
Any ideas?

This seems to be a simple case of bubblesort, a very inefficient sorting algorithm that is described by smaller elements bubbling to the top (assuming that you are sorting in ascending order). The modified algorithm I'm about to present is very similar to the original bubblesort algorithm, so I am going to quickly explain the original first. Bubblesort (for ascending order) works as follows,
The first element in a list is marked.
If the element to the right of the marked element is less than the marked element, then the two elements are swapped.
The marker moves to the right one spot regardless of the outcome of step two
Steps two and three are repeated until the marked element becomes the last element in the list. Just for clarification, this means that when step 3 causes the last element to be marked, the iteration is over and a new iteration starts.
The four steps above are repeated until an iteration occurs where the marker goes through each element in the list without a single swap occurring. Here's an example from wikipedia, https://en.wikipedia.org/wiki/Bubble_sort#Step-by-step_example
So let's modify bubblesort so that a couple things change to fit the deck of cards scenario. Let's not think of the deck of cards as a deck, but more as a list. Yes, the first card in the deck will constantly be changing with each iteration of our modified bubblesort but can we make it so that cards are shifting while still maintaining the position of the first card in the deck? This question is to key to solving the problem. What I am saying is that moving the cards to the bottom of the deck will not change in the initial order of the cards, only swapping will. For example, consider this deck of cards where leftmost is the top and rightmost is the bottom:
NOTE: (*) signifies marked card
*5 3 1 2 6
In the algorithm that will later be explained, moving 5 to the bottom of the deck will make the deck look like this
3 1 2 6 *5
Notice how 5 is now at the bottom of the deck, but the order is still preserved. The * symbol signifies the first card in the list/deck, so if read from left to right, starting from 5 and looping back to 3, the order is preserved.
Now to the algorithm, how do we use what I just said to make this modified version of bubblesort as similar to the original? Simple, use this marking mechanism to make it so that we're not really sorting a deck, but rather a list of numbers. Before starting the algorithm, mark the top card in the deck. Here are the rest of the steps for each iteration of bubblesort:
Compare the top card with the card below it. If the top card is greater, then swap with the card below. If the marked card was swapped, then unmark the previously marked card and mark the new card at the top of the deck.
Place the top card of the deck to the bottom.
Steps 1 and 2 are repeated until the marked card resurfaces to being the second card in the deck (card below the top card)
Put the top card to the bottom of the deck, making the marked card the top card in the deck.
These steps are repeated for each iteration of the algorithm until an iteration occurs where no swaps were made. Here is an example to showcase the modified bubblesort:
NOTE: (*) signifies marked card
Iteration One:
5 3 1 2 6 //Mark the first card in the deck before starting
*5 3 1 2 6 //Compare 5(top card) with 3(card below it)
3 *5 1 2 6 //5 > 3 so swap
*3 5 1 2 6 //Since the marked card (5) was swapped, 3 becomes the new marked
//card to preserve original order of the deck
5 1 2 6 *3 //Top card is placed at the bottom
1 5 2 6 *3 //5 > 1 so swap
5 2 6 *3 1 //Put 1 at the bottom
2 5 6 *3 1 //5 > 2 so swap
5 6 *3 1 2 //Put 2 at the bottom
5 6 *3 1 2 //5 < 6 so no swap
6 *3 1 2 5 //Put 5 at the bottom
*3 1 2 5 6 //Marked card is second to top card, so put 6 at the bottom
//Marked card is now at the top, so new iteration begins
Before going into iteration two, I would like to point out that if you ran the original bubblesort, the resulting sequence from one iteration would be the same as the resulting sequence from one iteration of our modified algorithm.
Iteration Two:
*3 1 2 5 6 //3 > 1, so swap
*1 3 2 5 6 //Remark accordingly since the former marked card was swapped
3 2 5 6 *1 //Place 1 at the bottom
2 3 5 6 *1 //3 > 2, so swap
3 5 6 *1 2 //Place 2 at the bottom
3 5 6 *1 2 //3 < 5 so no swap
5 6 *1 2 3 //Place 3 at the bottom
5 6 *1 2 3 //5 < 6 so no swap
6 *1 2 3 5 //Place 5 at the bottom.
*1 2 3 5 6 //Since marked card is second to top card, place 6 at the bottom and end iteration
Iteration Three:
*1 2 3 5 6 //1 < 2 so no swap
2 3 5 6 *1 //Place 1 at the bottom
3 5 6 *1 2 //2 < 3 so no swap and place 2 at the bottom
5 6 *1 2 3 //3 < 5 so no swap and place 3 at the bottom
6 *1 2 3 5 //5 < 6 so no swap and place 5 at the bottom
*1 2 3 5 6 //Since marked card is second to top card, place 6 at the bottom and end iteration.
We now know to end the algorithm because an entire iteration has occurred without any swaps occurring, so the deck is now sorted. As for runtime, the worst case occurs, in terms of swaps, when the max number of iterations occurs which is n (size of the deck) times. And for each iteration, the worst case number of swaps occurs, which is also n times. So the big O is n*n or O(n^2).

Related

Can you check for duplicates by taking the sum of the array and then the product of the array?

Let's say we have an array of size N with values from 1 to N inside it. We want to check if this array has any duplicates. My friend suggested two ways that I showed him were wrong:
Take the sum of the array and check it against the sum 1+2+3+...+N. I gave the example 1,1,4,4 which proves that this way is wrong since 1+1+4+4 = 1+2+3+4 despite there being duplicates in the array.
Next he suggested the same thing but with multiplication. i.e. check if the product of the elements in the array is equal to N!, but again this fails with an array like 2,2,3,2, where 2x2x3x2 = 1x2x3x4.
Finally, he suggested doing both checks, and if one of them fails, then there is a duplicate in the array. I can't help but feel that this is still incorrect, but I can't prove it to him by giving him an example of an array with duplicates that passes both checks. I understand that the burden of proof lies with him, not me, but I can't help but want to find an example where this doesn't work.
P.S. I understand there are many more efficient ways to solve such a problem, but we are trying to discuss this particular approach.
Is there a way to prove that doing both checks doesn't necessarily mean there are no duplicates?
Here's a counterexample: 1,3,3,3,4,6,7,8,10,10
Found by looking for a pair of composite numbers with factorizations that change the sum & count by the same amount.
I.e., 9 -> 3, 3 reduces the sum by 3 and increases the count by 1, and 10 -> 2, 5 does the same. So by converting 2,5 to 10 and 9 to 3,3, I leave both the sum and count unchanged. Also of course the product, since I'm replacing numbers with their factors & vice versa.
Here's a much longer one.
24 -> 2*3*4 increases the count by 2 and decreases the sum by 15
2*11 -> 22 decreases the count by 1 and increases the sum by 9
2*8 -> 16 decreases the count by 1 and increases the sum by 6.
We have a second 2 available because of the factorization of 24.
This gives us:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
Has the same sum, product, and count of elements as
1,3,3,4,4,5,6,7,9,10,12,13,14,15,16,16,17,18,19,20,21,22,22,23
In general you can find these by finding all factorizations of composite numbers, seeing how they change the sum & count (as above), and choosing changes in both directions (composite <-> factors) that cancel out.
I've just wrote a simple not very effective brute-force function. And it shows that there is for example
1 2 4 4 4 5 7 9 9
sequence that has the same sum and product as
1 2 3 4 5 6 7 8 9
For n = 10 there are more such sequences:
1 2 3 4 6 6 6 7 10 10
1 2 4 4 4 5 7 9 9 10
1 3 3 3 4 6 7 8 10 10
1 3 3 4 4 4 7 9 10 10
2 2 2 3 4 6 7 9 10 10
My write-only c++ code is here: https://ideone.com/2oRCbh

what does 'work through an algorithm by hand' mean?

I'm doing this assignment and I don't understand the wording. Do you think it means to write in pseudocode or write a paragraph? Does anyone have any ideas?
It means to describe the algorithm with words and draw the array values in each step. Here is an example: https://www.geeksforgeeks.org/bubble-sort/
Before executing the algorithm, the array is:
4 2 12 1 7 9 9
After executing the algorithm, the array is:
1 2 4 7 9 9 12
During the execution of the algorithm, the array slowly changes from what is was before, to what it will be after. Your assignment requires you to show all the intermediate steps.
For instance, the very first step of execution will be "compare element at position 0 with position 1; if element at position 1 is lower, then swap the two elements". The first two elements are 4 and 2; 2 is lower; hence they should be swapped; the resulting array is:
2 4 12 1 7 9 9
Then the second step will be "compare elements at position 1 and 2", which are 4 and 12; etc.

Determine given Sudoku is a magic square?

A magic square is a square in which:
1.There is one in the left upper cell.
2. There are no repeating numbers in any column.
3. There are no repeating numbers in any row.
4. There are no repeating numbers in any of the smaller squares.
5.If we swap two smaller squares having a common side, then we obtain square satisfying properties 2 to 4.
Ram has already written several numbers. Determine if it is possible to fill the remaining cells and obtain a magic square.
Example:
Given Matrix:
2 1 3 4
4 3 1 2
1 2 4 3
3 4 2 1
Step 1:
Swapping adjacent smaller squares
3 4 1 2
1 2 3 4
2 1 4 3
4 3 2 1
Step 2:
Swapping adjacent smaller squares(Finally obtaining the magic square)
1 2 3 4
3 4 1 2
2 1 4 3
4 3 2 1
Can anyone explain the logic behind this? Please do tell the easiest way to check whether the magic sudoku formation is possible or not for a given matrix?

smallest number greater than given number which is a palindrome

This was asked in an interview. Given a number, say 900, output the smallest palindrome greater than the number, 909 in this case. I gave a brute force solution that checks every number but I'm assuming there's a better way to go about this
Just for fun, here's a simple implementation in Python (using essentially the same algorithm as described by Guntram Blohm).
def next_palindrome(n):
"""
Given a non-negative integer n, return the first integer strictly
greater than n whose decimal representation is palindromic.
"""
s = str(n + 1)
l = len(s)
if s[:l//2][::-1] < s[(l+1)//2:]:
head = str(int(s[:(l+1)//2])+1)
else:
head = s[:(l+1)//2]
return int(head + head[:l//2][::-1])
And some sample output:
>>> next_palindrome(123)
131
>>> next_palindrome(4321)
4334
>>> next_palindrome(999)
1001
Copy the first digit to the last, second digit to the second-last etc until you reach the center digit (or center 2 digits if there is an even number of digits).
If the resulting number is smaller than the original number, increase the center digit/center 2 digits by one. If they are 9, set them to zero and retry with the 2 digits next to them, moving outwards until you hit a non-9.
Edit:
If the loop that moves outwards never hits a non-9, prepend a 1 to the string, set all digits except the last one to 0, and the last one to 1. This is the same as adding 2 to the number.
Though what has been answered above is absolutely correct. Just to add more understanding :)
There can be three different types of inputs that need to be handled separately.
1) The input number is palindrome and has all 9s. For example “9 9
9″. Output should be “1 0 0 1″
2) The input number is not palindrome. For example “1 2 3 4″. Output
should be “1 3 3 1″
3) The input number is palindrome and doesn’t have all 9s. For example
“1 2 2 1″. Output should be “1 3 3 1″.
Solution for input type 1
is easy. The output contains n + 1 digits where the corner digits are 1, and all digits between corner digits are 0.
Now let us first talk about input type 2 and 3. Let us first define the following two terms:
Left Side: The left half of given number. Left side of “1 2 3 4 5 6″ is “1 2 3″ and left side of “1 2 3 4 5″ is “1 2″
Right Side: The right half of given number. Right side of “1 2 3 4 5 6″ is “4 5 6″ and right side of “1 2 3 4 5″ is “4 5″
To convert to palindrome, we can either take the mirror of its left side or take mirror of its right side. However, if we take the mirror of the right side, then the palindrome so formed is not guaranteed to be next larger palindrome.
So, we must take the mirror of left side and copy it to right side. But there are some cases that must be handled in different ways. See the following steps.
We will start with two indices i and j. i pointing to the two middle elements (or pointing to two elements around the middle element in case of n being odd). We one by one move i and j away from each other.
Step 1. Initially, ignore the part of left side which is same as the corresponding part of right side. For example, if the number is “8 3 4 2 2 4 6 9″, we ignore the middle four elements. i now points to element 3 and j now points to element 6.
Step 2. After step 1, following cases arise:
Case 1: Indices i & j cross the boundary.
This case occurs when the input number is palindrome. In this case, we just add 1 to the middle digit (or digits in case n is even) propagate the carry towards MSB digit of left side and simultaneously copy mirror of the left side to the right side.
For example, if the given number is “1 2 9 2 1″, we increment 9 to 10 and propagate the carry. So the number becomes “1 3 0 3 1″
Case 2: There are digits left between left side and right side which are not same. So, we just mirror the left side to the right side & try to minimize the number formed to guarantee the next smallest palindrome.
In this case, there can be two sub-cases.
2.1) Copying the left side to the right side is sufficient, we don’t need to increment any digits and the result is just mirror of left side. Following are some examples of this sub-case.
Next palindrome for “7 8 3 3 2 2″ is “7 8 3 3 8 7″
Next palindrome for “1 2 5 3 2 2″ is “1 2 5 5 2 1″
Next palindrome for “1 4 5 8 7 6 7 8 3 2 2″ is “1 4 5 8 7 6 7 8 5 4 1″
How do we check for this sub-case? All we need to check is the digit just after the ignored part in step 1. This digit is highlighted in above examples. If this digit is greater than the corresponding digit in right side digit, then copying the left side to the right side is sufficient and we don’t need to do anything else.
2.2) Copying the left side to the right side is NOT sufficient. This happens when the above defined digit of left side is smaller. Following are some examples of this case.
Next palindrome for “7 1 3 3 2 2″ is “7 1 4 4 1 7″
Next palindrome for “1 2 3 4 6 2 8″ is “1 2 3 5 3 2 1″
Next palindrome for “9 4 1 8 7 9 7 8 3 2 2″ is “9 4 1 8 8 0 8 8 1 4 9″
We handle this subcase like Case 1. We just add 1 to the middle digit (or digits in ase n is even) propagate the carry towards MSB digit of left side and simultaneously copy mirror of the left side to the right side.
SOURCE: http://www.geeksforgeeks.org/given-a-number-find-next-smallest-palindrome-larger-than-this-number/

Selection sort count number of swaps

http://www.cs.pitt.edu/~kirk/cs1501/animations/Sort1.html is this applet counting right? Selection sort for 5 4 3 2 1, I see 2 swaps, but the applet is counting 4 exchanges....
I guess it's a matter of definition. He is doing a swap in the end of every loop, even if he is swapping one element against itself. In his case, the swaps will be:
Original: 5 4 3 2 1
Swap pos 1 and 5: 1 4 3 2 5
Swap pos 2 and 4: 1 2 3 4 5
Swap pos 3 and 3: 1 2 3 4 5
Swap pos 4 and 4: 1 2 3 4 5
(No swap is done for the last element since that will always be in the correct place)
A simple if statement could be used to eliminate the two last swaps.

Resources