实验9 排序算法实验
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验报告九排序算法实验
班级:姓名:学号:专业:
一、实验目的:
1、掌握内部排序方法的策略和排序过程。
2、掌握插入排序、选择排序、快速排序、归并排序算法。
二、实验内容:
1、存储结构类定义与实现:
自定义如下:
package Ex8;
import .apache.xpath.internal.SourceTree;
/**
* Created by 74062 on 2017/6/6.
*/
public class SortUtil {
public static void main(String[] args) {
int[] array1 = {5,3,8,9,1,7};
int[] array2 = {7,3,8,1,4,7};
int[] array3 = {10,4,6,3,8,7};
int[] array4 = {81,49,19,38,97,76,13,19};
insertSort(array1);
show(array1);
System.out.println();
quickSort(array2);
show(array2);
System.out.println();
mergeSort(array3);
heapSort(array4);
}
public static void insertSort(int[] keys){
for(int i=1;i for(int j = i-1;j>=0;j--){ if(keys[j]>keys[j+1]){ int temp = keys[j]; keys[j] = keys[j+1]; keys[j+1] = temp; }else break; } } } public static void heapSort(int[] keys){ heapSort(keys,true); } //堆排序 public static void heapSort(int[] keys,boolean minheap){ for(int i=keys.length/2-1;i>=0;i--) sift(keys,i,keys.length-1,minheap); for(int i=keys.length-1;i>0;i--){ swap(keys,0,i); sift(keys,0,i-1,minheap); } } private static void sift(int[] keys,int parent,int end,boolean minheap){ System.out.print("sift "+parent+".."+end+" "); int i = parent; int child=2*parent+1; int value=keys[parent]; while(child<=end){ if(child child++; if(minheap?value>keys[child]:value keys[parent]=keys[child]; parent=child; child=2*parent+1; } else break; } keys[parent] = value; show(keys); } public static void quickSort(int[] keys){ quickSort(keys,0,keys.length-1); } private static void quickSort(int[] keys, int begin, int end){ if(begin>=0&&begin int j = end; int vot = keys[i]; while (i!=j){ while (i j--; if(i keys[i++] = keys[j]; while (i i++; if(i keys[j--] = keys[i]; } keys[i] = vot; System.out.println(begin+".."+end+", vot="+vot+" "); quickSort(keys,begin,j-1); quickSort(keys,i+1,end); } } private static void merge(int[] X, int[] Y, int begin1, int begin2, int n){ int i = begin1,j = begin2, k = begin1; while (i if(X[i] Y[k++] = X[i++]; else Y[k++] = X[j++]; } while (i Y[k++] = X[i++]; while (j Y[k++] = X[j++]; }