package com.swain.cell;
public class UsingMergeSort {
public static void main(String[] args)
{
// TODO Auto-generated
method stub
UsingMergeSort
obj = new UsingMergeSort();
int a[] = { 34, 56, 23,
55, 78, 3, 88 };
UsingMergeSort.divide(a,
0, 6);
for (int i = 0; i < a.length; i++)
System.out.println("---" + a[i]);
}
public static void divide(int array[], int low, int high) {
System.out.println("divide
method");
if (low < high) {
int mid = (low + high) /
2;
divide(array,
mid, low);
divide(array,
mid + 1, high);
UsingMergeSort.mergeArray(array,
low, mid, high);
}
}
public static void mergeArray(int array[], int low, int mid, int high) {
System.out.println("MErger
method");
int tempArray[] = new int[array.length];
int i = low;
int j = low;
int k = mid + 1;
for (int l = 0; l < array.length; l++) {
System.out.println(array[l]);
}
while (j <= mid
&& k <= high) {
if (array[j] <=
array[k]) {
tempArray[i]
= array[j];
i++;
j++;
}
else {
tempArray[i]
= array[k];
k++;
i++;
}
}
for (; j < mid; j++)
{
tempArray[i]
= array[j];
i++;
}
for (; k < high; k++)
{
tempArray[i]
= array[k];
i++;
k++;
}
for (int l = 0; l < array.length; l++) {
System.out.println("Sorted" + tempArray[l]);
}
for (i = 0; i <
tempArray.length; i++)
array[i]
= tempArray[i];
}
}
No comments:
Post a Comment