How to rearrange data in array so that two similar items are not next to each other?

后端 未结 8 1890
暗喜
暗喜 2020-12-31 12:00

Just want to rearrange the data in array so that similar items are not next to each. The data should not be removed from the array, if it can\'t be rearranged it can be put

相关标签:
8条回答
  • 2020-12-31 12:06

    Populate your array into a class var, then you can run your custom sort methods on it without changing it. Congratulations, you just created a new nosql database.

    What is scaring everyone is losing the original order.

    This is why the key in a hash is called 'index', think about it.

    class Dingleberry
      {
    
       private $shiz= array();
    
        function __construct(array $a) { $this->shiz = $a; }
    
    ##### PUBLIC
    
        public function unmolested() { return $this->shiz; }
    
        /* @see http://www.php.net/manual/en/ref.array.php */
        public function sort($type)
            {
            switch ($type)
                {
                case 'key_reverse': return krsort($this->shiz); break;
                # Check all the php built in array sorting methods to 
                        # make sure there is not already one you want
                        # then build this out with your own
                }
            }
    
        
      
    

    }

    0 讨论(0)
  • 2020-12-31 12:11

    After I grasped what you're after, here's a possible solution

    1. Partition your array

      [1,1,1,8,8,8,2,3,3,4,1,1,1,2,2] -> [[3,1],[3,8],[1,2],[2,3],[1,4],[3,1],[2,2]]
      

      (read 3 times 1, 3 times 8, and so on)

    2. For each partition entry i with p[i][0] >1 (times >1):

      • Choose a "valid" position j (so p[j][1] != p[i][1] && p[j+1][1] != p[i][1])

      • Decrement p[i][0] element and insert [p[i][1],1] in partition at position j

      or leave it out if there is no such position.

    This should have linear time complexity (book-keep valid positions for each number).

    0 讨论(0)
  • 2020-12-31 12:12

    Java: Something like this?

    void resortArray(ArrayList<Integer> arr) {
      for(int i = 0; i < arr.size(); i++) //loop trough array
        if(arr.get(i) == arr.get(i + 1)) { //if the next value is the same as current one
          for(int j = i+2; j < arr.size(); j++) { //loop again trough array from start point i+2
            if(arr.get(i+1) != arr.get(j)) { //swap values when you got a value that is different
              int temp = arr.get(i+1);
              arr.set(i+1, arr.get(j));
              arr.set(j, temp);
              break;
            }  
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-31 12:12

    Take the entire array and scan it for duplicates. When you encounter dupes, remember where they are. So for something like 2 1 2 2* 3 3* 3* 4 4* 2 2* 5. The ones with stars should be remembered.

    Now look at the "Remembered" stuff, you have 2 2's, 2 3's and a 4

    Now I'd sort those LISTS the most numerous first (2's and 3's) to the least numerous (4's)

    Now just take the most numerous that doesn't duplicate the current "Front" (which would be 3 because 2 duplicates) and move it to the front, then remove it from your list.

    repeat until the lists are empty. The second time through your list will start with "3" and you will have 2 2's a 3 and a 4, so you'll put one of the 2's in the front...

    If you have any left (it can only be one number) put it at the end..

    done, cake.

    0 讨论(0)
  • 2020-12-31 12:15

    Hmm. Bubblesort comes to mind, but with a three-element comparison; that is, if item[x] and item[x + 1] are the same and item[x + 2] is different, swap item[x + 1] and item[x + 2]. Repeat iterating through the list until no swaps occur. Execution order is, of course, horrible, but that should meet your needs.

    0 讨论(0)
  • 2020-12-31 12:19
    1. Sort your array
    2. Swap elements at small even indexes with their higher antipodal counterparts:

      for ( i=0; i < arr.length/2; i+=2 )
          arr.swap(i,arr.length-1-i);
      

    Edit: Okay, we should redefine the antipodal counterparts. Maybe this one is better: mixing the first and third quartile (denoted x, y in illustration), and mixing the second and third quartile (denoted u, v, w). Let the counterparts ascend parallel.

            25%  50%  75%
             |    |    |
        -----[----[----[----
        11122334455667788999
         x y u v w x y u v w  <-- u, v, w, x, y indicate swap positions
        16172839495161738495
    
    0 讨论(0)
提交回复
热议问题