[关闭]
@rg070836rg 2015-11-29T18:10:11.000000Z 字数 3660 阅读 1783

算法概论实验一

算法概论实验


  1. 实验一
  2. 实验目的与要求:理解分治法的基本思想和设计方法。
  3. 实验题目:
  4. 1.实现基于分治法的归并排序算法.
  5. 2. 实现快速排序的算法,并尝试采用不同的方法实现线性的划分过程.
  6. 3. 有一个数的序列A[1]、A[2] A[3] 、…… A[n],若i<j,并且A[i]>A[j],则称A[i]与A[j]构成了一个逆序对,设计算法求数列A中逆序对的个数.
  7. 4.(选做题) 引入逆序计数问题作为考察两个序列有多大差别的一个好的度量指标。但是人们可能感觉这个量度太敏感了。如果i<j,并且A[i]>2A[j],我们把这对i,j叫做重要的逆序。设计一个O(nlogn) 的算法计数在两个序列中的重要逆序个数。

1 实现基于分治法的归并排序算法.

  1. package test_chen_921;
  2. public class MergeSort {
  3. public static void main(String[] args) {
  4. int[] r = { 5,2,1,0};
  5. merge_sort(r, 0, r.length - 1);
  6. print(r);
  7. }
  8. private static void print(int[] r) {
  9. // TODO 打印数组
  10. for (int i : r) {
  11. System.out.print(" " + i);
  12. }
  13. System.out.println();
  14. }
  15. private static void merge_sort(int[] r, int low, int high) {
  16. // TODO 划分 合并
  17. if (low < high) {
  18. int mid = (low + high) / 2;
  19. merge_sort(r, low, mid);
  20. merge_sort(r, mid + 1, high);
  21. merge(r, low, mid, high);
  22. }
  23. }
  24. private static void merge(int[] r, int low, int mid, int high) {
  25. // TODO 归并两个有序数组
  26. int k = 0;
  27. int i = low;
  28. int j = mid + 1;
  29. int[] tmp = new int[high - low + 1];
  30. while (i <= mid && j <= high) {
  31. if (r[i] <= r[j])
  32. tmp[k++] = r[i++];
  33. else
  34. tmp[k++] = r[j++];
  35. }
  36. while (i <= mid)
  37. tmp[k++] = r[i++];
  38. while (j <= high)
  39. tmp[k++] = r[j++];
  40. for (int m : tmp)
  41. r[low++] = m;
  42. }
  43. }

2. 实现快速排序的算法,

  1. package test_chen_921;
  2. public class quick_test {
  3. public static void main(String[] args) {
  4. int[] r = { 5, 8, 6, 4, 2, 3, 1, 7, 9, 0 };
  5. quick_sort(r, 0, r.length - 1);
  6. print(r);
  7. }
  8. private static void print(int[] r) {
  9. // TODO 打印数组
  10. for (int i : r) {
  11. System.out.print(" " + i);
  12. }
  13. System.out.println();
  14. }
  15. private static void quick_sort(int[] r, int low, int high) {
  16. // TODO 找中值,并左右递归
  17. if (low < high) {
  18. int mid = partion(r, low, high);
  19. // print(r);
  20. quick_sort(r, low, mid);
  21. quick_sort(r, mid + 1, high);
  22. }
  23. }
  24. private static int partion(int[] r, int low, int high) {
  25. // TODO 把数组段第一个数划分,并返回中间值的下标
  26. int pivot = r[low];// 记录pivot
  27. while (low < high) {
  28. while (low < high && r[high] > pivot)
  29. high--;
  30. if (low < high)
  31. r[low++] = r[high];
  32. while (low < high && r[low] < pivot)
  33. low++;
  34. if (low < high)
  35. r[high--] = r[low];
  36. }
  37. r[low] = pivot;
  38. return low;
  39. }
  40. }

3 逆序数

  1. package test_chen_921;
  2. public class InverseNum {
  3. public static void main(String[] args) {
  4. int []r = {80, 70, 9,9};
  5. System.out.println(getInverseNum(r, 0, r.length-1));
  6. }
  7. private static int getInverseNum(int[] r, int low, int high) {
  8. // TODO 计算前一半的逆序数,计算后一半的逆序数,计算之间的逆序数,相加并返回
  9. if(low<high)
  10. {
  11. int mid=(low+high)/2;
  12. int front_num=getInverseNum(r,low,mid);
  13. int behind_num=getInverseNum(r,mid+1,high);
  14. int between_num=mergeInverseNum(r,low,mid,high);
  15. return front_num+behind_num+between_num;
  16. }
  17. return 0;
  18. }
  19. private static void print(int[] r) {
  20. // TODO 打印数组
  21. for (int i : r) {
  22. System.out.print(" " + i);
  23. }
  24. System.out.println();
  25. }
  26. private static int mergeInverseNum(int[] r, int low, int mid, int high) {
  27. // TODO 与归并排序一致
  28. int []tmp=new int[high-low+1];
  29. int k1=low;
  30. int k2=mid+1;
  31. int k3=0;
  32. int count=0;
  33. while(k1<=mid && k2<=high)
  34. {
  35. if(r[k1]<=r[k2])
  36. tmp[k3++]=r[k1++];
  37. else
  38. {
  39. tmp[k3++]=r[k2++];
  40. //一旦这个数(r[k1])大于r[k2],那么他后面的数必定大于r[k2];所以count来源于他后面的数目的个数。
  41. count += mid - k1 + 1;
  42. }
  43. }
  44. while(k1<=mid)
  45. tmp[k3++]=r[k1++];
  46. while(k2<=high)
  47. tmp[k3++]=r[k2++];
  48. for(int m : tmp)
  49. r[low++]=m;
  50. return count;
  51. }
  52. }

4 重要逆序数

  1. package test_chen_921;
  2. public class SignificantNum {
  3. public static void main(String[] args) {
  4. int[] r = { 80, 70, 9, 9 };
  5. System.out.println(getSignificantNum(r, 0, r.length - 1));
  6. }
  7. private static int getSignificantNum(int[] r, int low, int high) {
  8. // TODO 计算前一半的逆序数,计算后一半的逆序数,计算之间的逆序数,相加并返回
  9. if (low < high) {
  10. int mid = (low + high) / 2;
  11. int front_num = getSignificantNum(r, low, mid);
  12. int behind_num = getSignificantNum(r, mid + 1, high);
  13. int between_num = mergeSig nificantNum(r, low, mid, high);
  14. return front_num + behind_num + between_num;
  15. }
  16. return 0;
  17. }
  18. private static void print(int[] r) {
  19. // TODO 打印数组
  20. for (int i : r) {
  21. System.out.print(" " + i);
  22. }
  23. System.out.println();
  24. }
  25. private static int mergeSignificantNum(int[] r, int low, int mid, int high) {
  26. // TODO 先记数,后排序
  27. int[] tmp = new int[high - low + 1];
  28. int k1 = low;
  29. int k2 = mid + 1;
  30. int k3 = 0;
  31. int count = 0;
  32. // count
  33. while (k1 <= mid && k2 <= high) {
  34. if (r[k1] <= 2 * r[k2])
  35. k1++;
  36. else {
  37. count += mid - k1 + 1;
  38. k2++;
  39. }
  40. }
  41. // merge
  42. k1 = low;
  43. k2 = mid + 1;
  44. while (k1 <= mid && k2 <= high) {
  45. if (r[k1] <= r[k2])
  46. tmp[k3++] = r[k1++];
  47. else
  48. tmp[k3++] = r[k2++];
  49. }
  50. while (k1 <= mid)
  51. tmp[k3++] = r[k1++];
  52. while (k2 <= high)
  53. tmp[k3++] = r[k2++];
  54. for (int m : tmp)
  55. r[low++] = m;
  56. return count;
  57. }
  58. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注