fork download
  1. #include <stdio.h>
  2.  
  3. void swap(int *i, int *j)
  4. {
  5. int temp = *i;
  6. *i = *j;
  7. *j = temp;
  8. }
  9.  
  10. int seperateEvenAndOdd(int arr[], int size)
  11. {
  12. // Initialize left and right indexes
  13. int left = 0;
  14. int right = size - 1;
  15.  
  16. while(left < right)
  17. {
  18. // Increment left index till the number is even
  19. // num%2 == 0, condition to check even number
  20. while(arr[left]%2 == 0 && left < right)
  21. {
  22. left++;
  23. }
  24.  
  25. // Decrement right index till the number is odd
  26. while(arr[right]%2 == 1 && left < right)
  27. {
  28. right--;
  29. }
  30.  
  31. // Time to swap
  32. if(left < right)
  33. {
  34. swap(&arr[left], &arr[right]);
  35. left++;
  36. right--;
  37. }
  38. }
  39. }
  40.  
  41. int main(void) {
  42. // your code goes here
  43. int arr[] = {4, 8, 15, 16, 23, 42};
  44.  
  45. seperateEvenAndOdd(arr, 6);
  46.  
  47. for (int i = 0; i<6;i++)
  48. printf("%d ", arr[i]);
  49.  
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
4 8 42 16 23 15