Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. public class Question2
  2. {
  3. public static void sortByFour(int[] arr)
  4. {
  5. int first=0;
  6. while(arr[first]%4==0 && first<arr.length)
  7. first++;
  8.  
  9. int last=arr.length-1;
  10. while(arr[last]%4==3 && last>=0)
  11. last--;
  12.  
  13. int temp=0;
  14. int pivot;
  15. for(pivot=first; pivot<=last; pivot++)
  16. {
  17. if(arr[pivot]%4==1)
  18. {
  19. temp=arr[pivot];
  20. arr[pivot]=arr[first];
  21. arr[first]=temp;
  22. pivot++;
  23. first++;
  24. }
  25. else if(arr[pivot]%4==2)
  26. {
  27. temp = arr[pivot];
  28. arr[pivot] = arr[last];
  29. arr[last] = temp;
  30. last--;
  31. }
  32. else
  33. pivot++
  34. }
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement