3

I know how to write an algorithm that sorts a binary array in O(n) time and O(1) auxiliary space complexity, but it doesn't seem to be stable.

void binarySort(int arr[], int arr_size)
{
  int i;
  int count_zeros=0;
  for (i=0 ; i<arr_size ; i++)
  {
    if(arr[i]==0)
      count_zeros++;
  }
    int j;
    for(j=0 ; j<count_zeros ; j++)
      arr[j]=0;
    int k;
    for(k=j ; k<arr_size ; k++)
      arr[k]=1;
}

Consider I want to perform binary radix sort using the stable sort algorithm as a subroutine. Here is an example in which we sort we sort according to the least significant bit.

Input: 11, 101, 1000, 1010, 111, 110, 10

Output: 1000, 1010, 110, 10, 11, 101, 111

Notice in the example above, the order of the binary numbers with the same LSBs is preserved.

Is there any way to edit the above algorithm or make a new algorithm such that the sort algorithm is stable while preserving it's O(n) time complexity and O(1) auxiliary space complexity?

From Wikipedia: Stable sorting algorithms maintain the relative order of records with equal keys (i.e. values). That is, a sorting algorithm is stable if whenever there are two records R and S with the same key and with R appearing before S in the original list, R will appear before S in the sorted list. For those who say that stability doesn't matter, read this post.

24
  • 4
    Since data is received as array of ints containing 0s and 1s, your method of counting and outputting is fine. Still not sure how stability of sort matters here as there are only 2 states, but it could be that I am just dense... Commented Aug 28, 2020 at 22:57
  • 5
    With pure value types like this, how could it possibly be unstable? I don't think the word even applies to this... Commented Aug 28, 2020 at 22:58
  • 1
    @MooingDuck What if the binary array corresponds to some list of ordered items? In that case stability matters. Commented Aug 28, 2020 at 23:00
  • 2
    It sounds like what you're looking for is a stable quicksort partition in O(1) space. Have a paper on the topic. Commented Aug 28, 2020 at 23:04
  • 1
    @Mathphile Yes, the radix sort application is great. I think ciamej claimed that it's possible but failed to demonstrate it and that bothered me. Commented Aug 28, 2020 at 23:39

1 Answer 1

-2

I think I may have come up with a stable sort algorithm that has O(n) time complexity and O(1) auxiliary space complexity. I would appreciate it if someone could verify that it satisfies all the properties. I would also like to see any other possible approaches to this problem. You can try running it here.

void binarySort(int arr[], int arr_length)
{
  int i, count=1;
  for(i=0 ; i<arr_length ; i++)
  {
    if(arr[i]==1)
    {
      arr[i]=count;
      count++;
    }
  }
  int num0s=arr_length-count+1;
  int j=0;
  int k=0;
  while(j<num0s)
  {
    if(arr[k]==0)
    {
      int temp=arr[k];
      arr[k]=arr[j];
      arr[j]=temp;
      j++;
    }
    k++;
  }
  int a;
  for(a=0 ; a<arr_length-num0s ; )
  {
    if(arr[num0s+a]!=a+1)
    {
      int temp=arr[num0s+a];
      arr[num0s+a]=arr[num0s+temp-1];
      arr[num0s+temp-1]=temp;
    }
    else
    {
      arr[num0s+a]=1;
      a++;
    }
  }
}

For those who are downvoting, I would request them to please explain what is wrong with this algorithm. From what I see, it fulfills all the requirements set.

Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.