Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. package Interesting;
  2.  
  3. import java.util.*;
  4.  
  5. public class Merge {
  6. public static void merge(int[] a, int i, int c, int j){
  7. int b[] = new int[a.length];
  8. int l = i;
  9. int r = j;
  10. int k = c+1;
  11. int t = i;
  12.  
  13. while (i <= c && k <= j){
  14. if (a[i] < a[k]){
  15. b[t] = a[i];
  16. i++;
  17. }
  18. else{
  19. b[t] = a[k];
  20. k++;
  21. }
  22. t++;
  23. }
  24.  
  25. while (i <= c){
  26. b[t] = a[i];
  27. i++;
  28. t++;
  29. }
  30. while (k <= j){
  31. b[t] = a[k];
  32. k++;
  33. t++;
  34. }
  35.  
  36. for (int e = l; e <= r; e++){
  37. a[e] = b[e];
  38. }
  39. }
  40.  
  41. public static void mergeSort(int[] a, int i, int j){
  42. if (i < j){
  43. int c = (i + j) / 2;
  44.  
  45. mergeSort(a, i, c);
  46. mergeSort(a, c+1, j);
  47. merge(a, i, c, j);
  48. }
  49. }
  50.  
  51. public static void main(String[] args){
  52. Scanner sc = new Scanner(System.in);
  53. int n = sc.nextInt();
  54. int a[] = new int[n];
  55.  
  56. for (int l = 0; l < n; l++)
  57. a[l] = sc.nextInt();
  58.  
  59. mergeSort(a, 0, n-1);
  60.  
  61. for (int l = 0; l < n; l++)
  62. System.out.print(a[l] + " ");
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement