Java examples for Data Structure:Sort
merge Sort
//package com.java2s; public class Main { public static void mergeSort(Number[] A, int start, int end) { if (end - start < 1) return; int split = ((end - start) / 2) + start; mergeSort(A, start, split);/*from w w w . j ava2 s .com*/ mergeSort(A, split + 1, end); merge(A, start, end, split); } public static void merge(Number[] A, int start, int end, int split) { Number[] L = new Number[(split + 1) - start]; Number[] R = new Number[end - split]; for (int i = 0; i < L.length; i++) { L[i] = A[start + i]; } for (int i = 0; i < R.length; i++) { R[i] = A[split + 1 + i]; } int ri = 0; int li = 0; for (int i = start; i <= end; i++) { if (L[li].floatValue() <= R[ri].floatValue()) { A[i] = L[li]; li++; } else { A[i] = R[ri]; ri++; } if (li == L.length) { while (ri < R.length) { i++; A[i] = R[ri]; ri++; } return; } if (ri == R.length) { while (li < L.length) { i++; A[i] = L[li]; li++; } return; } } } }