Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. /*
  2. Task for ООО Бизнес Технологии
  3. */
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8. /**
  9. *
  10. * @param A - array to sort
  11. * @param p - left bound , include
  12. * @param r - right bound, include
  13. */
  14. static void sort(int[] A, int p, int r){
  15. int q;
  16. if(p< r){
  17. q = (p + r) / 2;
  18. sort(A, p, q);
  19. sort(A, q + 1, r);
  20. merge(A, p, q, r);
  21. }
  22. }
  23.  
  24. /**
  25. *
  26. * @param A - array to sort
  27. * @param l - left bound of first subarray
  28. * @param m - right bound of first subarray, include
  29. * @param r - right bound of subarray, include
  30. */
  31. static void merge(int[] A, int l, int m, int r){
  32. ArrayList <Integer> subArray = new ArrayList<>();
  33. int first = l;
  34. int second = m + 1;
  35. while((first <= m) || (second <= r)){
  36. if( (first <= m) && (second <= r)){
  37. if(A[first] < A[second]){
  38. subArray.add(A[first]);
  39. first++;
  40. }
  41. else{
  42. subArray.add(A[second]);
  43. second++;
  44. }
  45. }
  46. else{
  47. if(first <= m) {
  48. subArray.add(A[first]);
  49. first++;
  50. }
  51. else{
  52. subArray.add(A[second]);
  53. } second++;
  54. }
  55. }
  56. for(int i = l; i <= r; i++)
  57. A[i] = subArray.get(i - l);
  58. }
  59.  
  60. public static void main(String[] args) {
  61. int n;
  62. Scanner scan = new Scanner(System.in);
  63. n = scan.nextInt();
  64. int[] x = new int[n];
  65. for(int i = 0; i < n; i++)
  66. x[i] = scan.nextInt();
  67. sort(x, 0, n - 1);
  68. for(int i = 0; i < n; i++)
  69. System.out.print(x[i] + " ");
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement