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

后端 未结 8 1925
暗喜
暗喜 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:29

    Assuming A Array Containing Digits Between 0 To 9:
    Similar To Bucket Sort In A Way

    int B[10];//buckets
    diff=0;//how many different digits appeared
    
    for(i=0;i=0)//now to place back to array makes an interleaving
    {
     for(digit=0;digit<10;digit++)
     {
      if(B[digit]<>0)
      {
       A[B[digit]+diff]=digit;
       B[digit]--;
      }
     }
     diff--;
    }
    

提交回复
热议问题