Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interesting sorting problem

There are ones, zeroes and ‘U’s in a particular order. (E.g. “1001UU0011”) The number of ones and zeroes are the same, and there’s always two ‘U’s next to each other. You can swap the pair of ‘U’s with any pair of adjacent digits. Here’s a sample move:

      __
     /  \
1100UU0011 --> 11001100UU

The task is to put all the zeroes before the ones.

Here's a sample solution:

First step:
  __
 /  \
1100UU0011

Second step:
  ____
 /    \
UU00110011

000011UU11  --> DONE

It’s pretty easy to create a brute-force algorithm. But with that it takes hundreds or even thousands of moves to solve a simple one like my example. So I’m looking for something more “clever” algorithm.


It's not homework; it was a task in a competition. The contest is over but I can’t find the solution for this.

Edit: The task here is the create an algorithm that can sort those 0s and 1s - not just output N 0s and N 1s and 2 Us. You have to show the steps somehow, like in my example.

Edit 2: The task didn't ask for the result with the least moves or anything like that. But personally I would love the see an algorithm that provides that :)

like image 747
KovBal Avatar asked Jun 22 '10 11:06

KovBal


People also ask

What are the issues in sorting?

Issues in SortingWe need to consider whether we need to sort the list in increasing or decreasing order. Clearly we can use the same algorithm in both cases. All we need to do is to change the comparison criteria from > to < or vice versa.

What is the most effective sorting method?

Quicksort. Quicksort is one of the most efficient sorting algorithms, and this makes of it one of the most used as well. The first thing to do is to select a pivot number, this number will separate the data, on its left are the numbers smaller than it and the greater numbers on the right.

Which sorting algorithm is the fastest?

Which is the best sorting algorithm? If you've observed, the time complexity of Quicksort is O(n logn) in the best and average case scenarios and O(n^2) in the worst case. But since it has the upper hand in the average cases for most inputs, Quicksort is generally considered the “fastest” sorting algorithm.

Where sorting is used in real life?

The contact list in your phone is sorted, which means you can easily access your desired contact from your phone since the data is arranged in that manner for you. In other words, “it is sorted”. While shopping on flip kart or amazon, you sort items based on your choice, that is, price low to high or high to low.


1 Answers

I think this should work:

  • Iterate once to find the position of the U's. If they don't occupy the last two spots, move them there by swapping with the last two.
  • Create a variable to track the currently sorted elements, initially set to array.length - 1, meaning anything after it is sorted.
  • Iterate backwards. Every time you encounter a 1:
    • swap the the one and its element before it with the U's.
    • swap the U's back to the the currently sorted elements tracker -1, update variable
  • Continue until the beginning of the array.
like image 188
JRL Avatar answered Nov 15 '22 20:11

JRL