skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Thursday, August 15, 2013

Merge-Sort Algorithm implementation in JAVA

Posted by Admin at 10:10 AM – 0 comments
 
Merge-Sort Function

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void MergeSort(int low, int high)
   // a[low : high] is a global array to be sorted.
   // Small(P) is true if there is only one element to
   // sort. In this case the list is already sorted.
   {
       if (low < high) { // If there are more than one element
              // Divide P into subproblems.
              // Find where to split the set.
              int mid = (low + high)/2;
              // Solve the subproblems.
              MergeSort(low, mid);
              MergeSort(mid + 1, high);
              // Combine the solutions.
              Merge(low, mid, high);
       }
   }
 
   void Merge(int low, int mid, int high)
   // a[low:high] is a global array containing two sorted
   // subsets in a[low:mid] and in a[mid+1:high]. The goal
   // is to merge these two sets into a single set residing
   // in a[low:high]. b[] is an auxiliary global array.
   {
       int h = low, i = low, j = mid+1, k;
       while ((h <= mid) && (j <= high)) {
          if (a[h] <= a[j]) { b[i] = a[h]; h++; }
          else { b[i] = a[j]; j++; } i++;
       }
       if (h > mid) for (k=j; k<=high; k++) {
                       b[i] = a[k]; i++;
                    }
       else for (k=h; k<=mid; k++) {
               b[i] = a[k]; i++;
            }
       for (k=low; k<=high; k++) a[k] = b[k];
   }

Merge Sort Java Code :
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.*;
 
public class MergeSort {
 public int a[] = new int[50];
 
 public void merge_sort(int low, int high) {
  int mid;
  if (low < high) {
   mid = (low + high) / 2;
   merge_sort(low, mid);
   merge_sort(mid + 1, high);
   merge(low, mid, high);
  }
 }
 
 public void merge(int low, int mid, int high) {
  int h, i, j, k;
  int b[] = new int[50];
  h = low;
  i = low;
  j = mid + 1;
 
  while ((h <= mid) && (j <= high)) {
   if (a[h] <= a[j]) {
    b[i] = a[h];
    h++;
   } else {
    b[i] = a[j];
    j++;
   }
   i++;
  }
  if (h > mid) {
   for (k = j; k <= high; k++) {
    b[i] = a[k];
    i++;
   }
  } else {
   for (k = h; k <= mid; k++) {
    b[i] = a[k];
    i++;
   }
  }
  for (k = low; k <= high; k++)
   a[k] = b[k];
 }
 
 public MergeSort() {
  int num, i;
 
  System.out.println("             MERGE SORT PROGRAM            ");
 
  System.out.println();
  System.out.println();
  System.out
    .println("Please Enter THE No. OF ELEMENTS you want to sort[THEN PRESS ENTER]:");
  num = new Scanner(System.in).nextInt();
  System.out.println();
  System.out.println("Now, Please Enter the (" + num
    + ") nos. [THEN PRESS ENTER]:");
  for (i = 1; i <= num; i++) {
   a[i] = new Scanner(System.in).nextInt();
  }
  merge_sort(1, num);
  System.out.println();
  System.out.println("So, the sorted list (using MERGE SORT) will be :");
  System.out.println();
  System.out.println();
 
  for (i = 1; i <= num; i++)
   System.out.print(a[i] + "    ");
 
 }
 
 public static void main(String[] args) {
  new MergeSort();
 }
}
Labels: Algorithm , Sorting Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger