已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
import java.lang.Math;public class Sort private double sortArray;public double getSortArray() return sortArray;public void setSortArray(double sortArray) this.sortArray = sortArray;/* * 输出排序样本数组 */public void PrintSortArray(double sortArray) for(int i=0; isortArray.length; i+) System.out.print(sortArrayi + );System.out.println();/* * 产生随机样本 */public double generateRandomSample(int n) sortArray = new doublen;for(int i=0; in; i+) sortArrayi = Math.random();return sortArray;/* * 插入排序 */public void insertSort(double sortArray) double key;int j; for(int i=1; i= 0)&(sortArrayj key) sortArrayj+1 = sortArrayj; j = j-1; sortArrayj+1 = key; /* * 冒泡排序 */public void bubbleSort(double sortArray) for(int i=0; isortArray.length; i+) for(int j=0; j sortArrayj+1) double temp = sortArrayj;sortArrayj = sortArrayj+1;sortArrayj+1 = temp;/* *快速排序 */public void quickSort(double sortArray, int left, int right) if(leftright)int p = partion(sortArray, left, right);quickSort(sortArray, left, p-1);quickSort(sortArray, p+1, right);public int partion(double sortArray, int left, int right) double x = sortArrayright;int i = left-1;for(int j=left; jright; j+) if(sortArrayj=x)i = i+1;double temp = sortArrayi;sortArrayi = sortArrayj;sortArrayj = temp;double temp = sortArrayi+1;sortArrayi+1 = sortArrayright;sortArrayright = temp;return i+1;/* * 合并排序 */public void mergeSort(double sortArray, int p, int r) if(pr) int q = (p+r)/2;mergeSort(sortArray, p, q);mergeSort(sortArray, q+1, r);merge(sortArray, p, q, r);public void merge (double sortArray, int p, int q, int r) int n1 = q-p+1;int n2 = r-q;double left = new doublen1+1;double right = new doublen2+1;for(int i = 0; i n1; i +)lefti = sortArrayp+i; for(int i = 0; i n2; i +)righti = sortArrayq+i+1;leftn1=Double.MAX_VALUE;rightn2=Double.MAX_VALUE;int pl = 0;int pr = 0;for(int i=p; i=r; i+) if(leftpl=rightpr)sortArrayi = leftpl;pl+;elsesortArrayi = rightpr;pr+;/* * 希尔排序 */ public void ShellSort(double a) int inc=a.length; do inc=inc/3+1; for(int start=0;start1); public void InsertSort(double a,int start,int inc) double current; int place=0; for(int i=start+inc;ia.length;i=i+inc) if(ai=start;fu=fu-inc) afu+inc=afu; if(fu=start|(afu-inccurrent) place=fu; break; aplace=current; public static void main(String args) int n = 100;double time = 0;double t1 = 0;double t2 = 0;double t3 = 0;double t4= 0;double t5 = 0;double insertArr ;double bubbleArr ;double quickArr ;double mergeArr ;double shellArr ;Sort s = new Sort();double sample = s.generateRandomSample(10);insertArr = sample;bubbleArr = sample;quickArr = sample;mergeArr = sample;shellArr = sample;System.out.println(N=10时的排序结果);System.out.println(未排序前数据为);s.PrintSortArray(sample);/排序s.insertSort(insertArr);s.bubbleSort(bubbleArr);s.quickSort(quickArr, 0, quickArr.length-1);s.ShellSort(shellArr);s.mergeSort(mergeArr, 0, mergeArr.length-1);System.out.println(插入排序结果为);s.PrintSortArray(insertArr);System.out.println(冒泡排序结果为);s.PrintSortArray(bubbleArr);System.out.println(快速排序结果为);s.PrintSortArray(quickArr);System.out.println(希尔排序结果为);s.PrintSortArray(shellArr);System.out.println(归并排序结果为);s.PrintSortArray(mergeArr);for(int j=0; j3; j+)n = n*10;for(int i=0; i5 ;i+)sample = s.generateRandomSample(n);insertArr = sample;bubbleArr = sample;quickArr = sample;mergeArr = sample;shellArr = sample;/排序,计时time = System.nanoTime();s.insertSort(insertArr);t1 += System.nanoTime() - time;time = System.nanoTime();s.bubbleSort(bubbleArr);t2 += System.nanoTime() - time;time = System.nanoTime();s.quickSort(quickArr, 0, quickArr.length-1);t3 += System.nanoTime() - time;time = System.nanoTime();s.ShellSort(shellArr);t4 += System.nanoTime() - time;time = System.nanoTime();s.mergeSort(mergeArr, 0, mergeArr.length-1);t5 += System.nanoTime() - time;t1 = t1/5;t2 = t2/5;t3 = t3/5;t4 = t4/5;t5 = t5/5;System.out.println(N= + n + 时个排序
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- Unit 5 Here and Now (Period 1)Section A (1a-Pronunciation)教案同步练2025-2026学年人教版英语七年级下册
- 【北京】《消息二则》名师任务单(第2课时)
- 2025-2030年医疗保健培训企业制定与实施新质生产力战略分析研究报告
- 企业数据分类分级评估协议2025年规范
- 2006年浙江省宁波市某校保送生招生考试数学试卷【含答案】
- 科学预防传染构建免疫屏障小学六年级主题班会课件
- 外墙工程承包合同8篇
- 预防传染病要记牢健康快乐无烦恼小学主题班会课件
- 2026村中支教面试题及答案
- 2026调度员面试题及答案
- 2026四川雅安市雨城区河北街道便民服务中心招聘见习人员2人考试备考试题及答案详解
- 2026年七升八数学压轴应用与几何证明专项突破三套组合卷A+B+C(附赠开学摸底卷含多种解题思路与易错清单)
- 建筑施工单位安全生产三级教育制度培训课件
- 2026年广东省大宝山矿业有限公司职工医院医护人员招聘笔试备考题库及答案解析
- 2026年办公室文员笔试试题(含答案)
- 社区老年人健康监测数据采集规范指引
- 北师大版九年级物理全一册电子课本教材
- 北师大版小学数学四年级下册单元测试题含答案(全册)
- 基坑工程作业活动风险分级管控清单
- 铝合金门窗工程报价范本
- 夏季饮食健康和预防蚊虫叮咬
评论
0/150
提交评论