title: Odd-Even Sort using cuda Programming
body: <p>I'm trying to implement odd-even sort program in cuda-c language. But, whenever I give a 0 as one of the elements in the input array, the resulted array is not properly sorted.In other cases, however, it is working for other input.I don't understand what is the problem with the code.Here is my code:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;cuda.h&gt;
  #define N 5
  
  __global__ void sort(int *c,int *count)
  {
      int l;
      if(*count%2==0)
            l=*count/2;
      else
           l=(*count/2)+1;
      for(int i=0;i&lt;l;i++)
      {
              if(threadIdx.x%2==0)  //even phase
              {
                  if(c[threadIdx.x]&gt;c[threadIdx.x+1])
                  {
                      int temp=c[threadIdx.x];
                      c[threadIdx.x]=c[threadIdx.x+1];
                      c[threadIdx.x+1]=temp;
                  }
  
              __syncthreads();
              }
              else     //odd phase
              {
                  if(c[threadIdx.x]&gt;c[threadIdx.x+1])
                  {
                      int temp=c[threadIdx.x];
                      c[threadIdx.x]=c[threadIdx.x+1];
                      c[threadIdx.x+1]=temp;
                  }
  
              __syncthreads();
              }
      }//for
  
  }
  
  
  
  int main()
  {int a[N],b[N],n;
      printf("enter size of array");
      scanf("%d",&amp;n);
      print("enter the elements of array");
    for(int i=0;i&lt;n;i++)
    {
      scanf("%d",&amp;a[i]);
    }
    printf("ORIGINAL ARRAY : \n");
    for(int i=0;i&lt;n;i++)
            {
  
            printf("%d ",a[i]);
            }
    int *c,*count;
    cudaMalloc((void**)&amp;c,sizeof(int)*N);
    cudaMalloc((void**)&amp;count,sizeof(int));
    cudaMemcpy(c,&amp;a,sizeof(int)*N,cudaMemcpyHostToDevice);
    cudaMemcpy(count,&amp;n,sizeof(int),cudaMemcpyHostToDevice);
    sort&lt;&lt;&lt; 1,n &gt;&gt;&gt;(c,count);
    cudaMemcpy(&amp;b,c,sizeof(int)*N,cudaMemcpyDeviceToHost);
    printf("\nSORTED ARRAY : \n");
    for(int i=1;i&lt;=n;i++)
        {
           printf("%d ",b[i]);
        }
  
  }
  </code></pre>