public class Lamada {
static int count = 0;
// 将有二个有序数列a[first...mid]和a[mid...last]合并。
static void mergearray(int a[], int first, int mid, int last, int temp[]) {
int i = first, j = mid + 1;
int m = mid, n = last;
int k = 0;
while (i <= m && j <= n) {
if (a[i] > a[j]) {
// 左数组比右数组大
temp[k++] = a[j++];
// 因为如果a[i]此时比右数组的当前元素a[j]大,
// 那么左数组中a[i]后面的元素就都比a[j]大
// 【因为数组此时是有序数组】
count += mid - i + 1;
} else {
temp[k++] = a[i++];
}
}
while (i <= m) {
temp[k++] = a[i++];
}
while (j <= n) {
temp[k++] = a[j++];
}
for (i = 0; i < k; i++)
a[first + i] = temp[i];
}
static void mergesort(int a[], int first, int last, int temp[]) {
if (first < last) {
int mid = (first + last) / 2;
mergesort(a, first, mid, temp); // 左边有序
mergesort(a, mid + 1, last, temp); // 右边有序
mergearray(a, first, mid, last, temp); // 再将二个有序数列合并
}
}
static void mergeSort(int a[]) {
int[] p = new int[a.length];
mergesort(a, 0, a.length - 1, p);
}
public static void main(String ss[]) {
int data[] = {6,4, 5,7,9, 3, 2, 1 };
mergeSort(data);
for (int i = 0; i < data.length; i++) {
System.out.print(data[i] + "\t");
}
System.out.println();
System.out.println(count);
}
}
算法设计:求逆序
猜你喜欢
转载自blog.csdn.net/bless2015/article/details/88751611
今日推荐
周排行