Skip to content

Instantly share code, notes, and snippets.

@CvutFelStudentAccount
Created October 13, 2011 20:04
Show Gist options
  • Select an option

  • Save CvutFelStudentAccount/1285346 to your computer and use it in GitHub Desktop.

Select an option

Save CvutFelStudentAccount/1285346 to your computer and use it in GitHub Desktop.
Merge Sort (Java)
package homeworks;
/**
*
* @author Petr
*/
class Homework1 implements Mergesort{
public int[] getFirstHalfOf(int[] array) {
if (array.length % 2 != 0) {
int[] firstHalf = new int[(array.length / 2) + 1];
System.arraycopy(array, 0, firstHalf, 0, (array.length / 2) + 1);
return firstHalf;
} else {
int[] firstHalf = new int[array.length / 2];
System.arraycopy(array, 0, firstHalf, 0, array.length / 2);
return firstHalf;
}
}
public int[] getSecondHalfOf(int[] array) {
int[] secondHalf = new int[array.length / 2];
System.arraycopy(array, array.length - array.length / 2, secondHalf, 0, array.length / 2);
return secondHalf;
}
public int[] merge(int[] firstHalf, int[] secondHalf) {
int arr1[] = firstHalf;
int arr2[] = secondHalf;
int[] helpArr = new int[arr1.length];
int array[] = new int[firstHalf.length + secondHalf.length];
int mainArrayIndex = 0, firstArrayIndex = 0, secondArrayIndex = 0;
while (arr1.length > 0 || arr2.length > 0) {
if (arr1.length > 0 && arr2.length > 0) {
if (arr1[0] <= arr2[0]) {
array[mainArrayIndex] = arr1[0];
helpArr = arr1;
arr1 = new int[helpArr.length-1];
System.arraycopy(helpArr, 1, arr1, 0, arr1.length);
mainArrayIndex++;
} else {
array[mainArrayIndex] = arr2[0];
helpArr = arr2;
arr2 = new int[helpArr.length-1];
System.arraycopy(helpArr, 1, arr2, 0, arr2.length);
mainArrayIndex++;
}
} else if (arr1.length > 0) {
array[mainArrayIndex] = arr1[0];
helpArr = arr1;
arr1 = new int[helpArr.length-1];
System.arraycopy(helpArr, 1, arr1, 0, arr1.length);
mainArrayIndex++;
} else if (arr2.length > 0) {
array[mainArrayIndex] = arr2[0];
helpArr = arr2;
arr2 = new int[helpArr.length-1];
System.arraycopy(helpArr, 1, arr2, 0, arr2.length);
mainArrayIndex++;
}
}
return array;
}
public int[] mergesort(int[] array) {
if(array.length <= 1){
return array;
}
int lengthOfArray1 = array.length/2;
int elementsInA2 = lengthOfArray1;
if((array.length % 2) == 1)
lengthOfArray1 += 1;
int arr1[] = getFirstHalfOf(array);
int arr2[] = getSecondHalfOf(array);
System.arraycopy(array, 0, arr1, 0, arr1.length);
System.arraycopy(array, arr1.length, arr2, 0, arr2.length);
arr1 = mergesort(arr1);
arr2 = mergesort(arr2);
return merge(arr1, arr2);
}
}
interface Mergesort {
int[] getFirstHalfOf(int[] array); // vrati nesetridenou kopii prvni poloviny (horni celou cast) pole array
int[] getSecondHalfOf(int[] array); // vrati nesetridenou kopii druhe poloviny (dolni celou cast) pole array
int[] merge(int[] firstHalf, int[] secondHalf); // slije prvky v firstHalf a secondHalf do jednoho pole a vrati ho
int[] mergesort(int[] array); // vrati setridenou kopii pole array
}
class Main {
public static void main(String[] args) {
int A[] = new int[10];
int B[] = {};
int C[] = {};
populateArray(A);
Homework1 homework = new Homework1();
int D[]=homework.merge(B, C);
printArray(D);
System.out.println("Pred razenim:");
printArray(A);
A = homework.mergesort(A);
System.out.println("\nPo razeni: ");
printArray(A);
}
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println("");
}
public static int[] populateArray(int[] array) {
for (int i = 0; i < array.length; i++) {
array[i] = (int) (Math.random() * 100);
}
return array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment