20162316刘诚昊 2017-2018-2 《Java程序设计》课下排序测试

实验要求

1 给定下列数据:90 8 7 56 123 235 9 1 653
用JDB或IDEA单步跟踪下列算法的执行过程:选择排序,插入排序,希尔排序,冒泡排序,快速排序,归并排序
2 提交每一趟的截图,要全屏,包含自己的学号信息
3 课下把代码推送到代码托管平台

过程:

1.载入书上的代码“Sorting”:

public class Sorting {//----------------------------------------------------------------------------// Sorts the specified array of integers using the selection// sort algorithm//----------------------------------------------------------------------------public static void selectionSort(Comparable[] data) {int min;for (int index = 0; index < data.length - 1; index++) {min = index;for (int scan = index + 1; scan < data.length; scan++)if (data[scan].compareTo(data[min]) < 0)min = scan;swap(data, min, index);}}//----------------------------------------------------------------------------//  Swaps two elements in the specified array//----------------------------------------------------------------------------private static void swap(Comparable[] data, int index1, int index2) {Comparable temp = data[index1];data[index1] = data[index2];data[index2] = temp;}//----------------------------------------------------------------------------// Sorts the specified array of object using an insertion// sorts algorithm//-----------------------------------------------------------------------------public static void insertionSort(Comparable[] data) {for (int index = 1; index < data.length; index++) {Comparable key = data[index];int position = index;// Shift larger values to the rightwhile (position > 0 && data[position - 1].compareTo(key) > 0) {data[position] = data[position - 1];position--;}data[position] = key;}}//-----------------------------------------------------------------------------// Sorts the specified array of objects using a bubble sort// algorithm.//------------------------------------------------------------------------------public static void bubbleSort(Comparable[] data) {int position, scan;for (position = data.length - 1; position >= 0; position--) {for (scan = 0; scan <= position - 1; scan++)if (data[scan].compareTo(data[scan + 1]) > 0)swap(data, scan, scan + 1);}}//--------------------------------------------------------------------------------// Sorts the specified array of objects using the quick sort algorithm.//--------------------------------------------------------------------------------public static void quickSort(Comparable[] data, int min, int max){int pivot;if (min < max){pivot = partition (data, min, max); //make partitionsquickSort(data, min, pivot-1); //sort left partitionquickSort(data, pivot+1, max);}}//----------------------------------------------------------------------------------// Creates the partitions needed fof quick sort.//-----------------------------------------------------------------------------------private static int partition(Comparable[] data, int min, int max){//Use first element as the partiton value.Comparable partitionValue = data[min];int left = min;int right = max;while (left < right){// Search for an element that is > the partition elementwhile (data[left].compareTo(partitionValue) <= 0 && left < right)left++;// Search for an element that is < the partition elementwhile (data[right].compareTo(partitionValue) >0)right--;if (left < right)swap(data, left, right);}// Move the partition element to its fianl positionswap(data, min, right);return right;}//------------------------------------------------------------------------------------// Sorts the speceified array of objects using the merge sort algorithm//-------------------------------------------------------------------------------------public static void mergeSort(Comparable[] data, int min, int max){if (min < max){int mid = (min + max) / 2;mergeSort(data, min, mid);mergeSort(data, mid+1, max);merge (data, min, mid, max);}}//------------------------------------------------------------------------------------// Sorts the specified array of objects using the merge sort algorithm.//-------------------------------------------------------------------------------------public static void merge(Comparable[] data, int first, int mid, int last){Comparable[] temp = new Comparable[data.length];int first1 = first, last1 = mid; // endpoints of first subarrayint first2 = mid+1, last2 = last; // endpoints of second subarrayint index = first1; // next index open in temp array// Copy smaller item from each subarray intp temp until one// of the subarray is exhaustedwhile (first1 <= last1 && first2 <= last2){if (data[first1].compareTo(data[first2]) < 0){temp[index] = data[first1];first1++;}else{temp[index] = data[first2];first2++;}index++;}// Copy remaining elements from first subarray, if anywhile (first1 <= last1){temp[index] = data[first1];first1++;index++;}// Copy remaining elements from second subarray, if anywhile (first2 <= last2){temp[index] = data[first2];first2++;index++;}// Copy merged data into original arrayfor (index = first; index <= last; index++){data[index] = temp[index];}}//---------------------------------------------------------------------------------public void hillSort(Comparable[] data){int n = data.length;for (int space = n/2; space > 0; space = space /2){for (int i = 0; i < space; i++){hill(data, i, space);}}}public  void hill(Comparable[] data, int index, int space){for (int i = index + space; i < data.length; i += space){Comparable obj =  data[i];for (int j = i; j > index; j -= space){if (obj.compareTo(data[j]) < 0)data[j + space] = data[j];data[j] = obj;}}}}

2.写实现代码,并验证是否处理正确:

public class Sorting_several {public static void main(String[] args) {Sorting q = new Sorting();Comparable[] num1 = {90, 8, 7, 56, 123, 235, 9, 1, 653, 2016};q.selectionSort(num1);System.out.println("选择排序结果:");for(Comparable element : num1){System.out.print(element + " ");}System.out.println();Comparable[] num2 = {90, 8, 7, 56, 123, 235, 9, 1, 653, 2016};q.insertionSort(num2);System.out.println("插入排序结果:");for(Comparable element : num2) {System.out.print(element + " ");}System.out.println();Comparable[] num3 = {90, 8, 7, 56, 123, 235, 9, 1, 653, 2016};q.bubbleSort(num3);System.out.println("冒泡排序结果:");for(Comparable element : num3){System.out.print(element + " ");}System.out.println();Comparable[] num4 = {90, 8, 7, 56, 123, 235, 9, 1, 653, 2016};System.out.println("快速排序结果:");q.quickSort(num4,0,num4.length-1);for(Comparable element : num4){System.out.print(element + " ");}System.out.println();Comparable[] num5 = {90, 8, 7, 56, 123, 235, 9, 1, 653, 2016};System.out.println("归并排序结果:");q.mergeSort(num5,0,num5.length-1);for(Comparable element : num5){System.out.print(element + " ");}System.out.println();}
}

3.Debug 单步跟踪:
选择排序:

插入排序:

冒泡排序:

快速排序:

归并排序:

转载于:https://www.cnblogs.com/ignor/p/7686329.html

20162316刘诚昊 课下排序测试相关推荐

  1. 20162316刘诚昊 Java Queue的测试

    交慢了一步.. 转载于:https://www.cnblogs.com/ignor/p/7686621.html

  2. 20162316刘诚昊 17年10月9日测验“排序课堂测试”

    20162316刘诚昊 2017-2018-2 <Java程序设计>10月9日"排序课堂测试" 实验要求 1 用JDB或IDEA单步跟踪对3 8 12 34 54 84 ...

  3. 20162316刘诚昊 队列课下作业

    20162316刘诚昊 2017-2018-2 <Java程序设计>队列课下作业 实验要求 1 补充课上没有完成的作业 2 参考15.3节,用自己完成的队列(链队,循环数组队列)实现模拟票 ...

  4. 2017-2018-1 20162316刘诚昊 实验三 查找与排序

    20162316刘诚昊 2017-2018-2 <Java程序设计>第三次实验 查找与排序 实验链接: 实验三 查找与排序-1 实验二 查找与排序-2 实验二 查找与排序-3 实验二 查找 ...

  5. 20162316刘诚昊 10月9日“查找课堂测试”

    20162316刘诚昊 2017-2018-2 <Java程序设计>10月9日"查找课堂测试" 测试要求: 1 用JDB或IDEA单步跟踪在下列数据中(3 8 12 3 ...

  6. 20162316刘诚昊 课程总结

    20162316刘诚昊 2016-2017-2<程序设计与数据结构>课程总结 目录: 每周作业链接汇总 写得最好的博客 阅读量最高的博客 实验报告链接汇总 团队项目报告链接汇总 代码托管链 ...

  7. 2017-2018-1 20162316刘诚昊 实验二 树

    20162316刘诚昊 2017-2018-2 <Java程序设计>第二次实验 树 实验链接: 实验二 树-1-实现二叉树 实验二 树-2-中序先序序列构造二叉树 实验二 树-3-决策树 ...

  8. 20162316刘诚昊 《程序设计与数据结构》 第三周学习总结

    20162316刘诚昊 2017-2018-2 <Java程序设计>第三周学习总结 教材学习内容总结 1. 查找是在一组数据中找到指定的目标元素或判定组内不存在目标的过程,常用方法为线性查 ...

  9. 20162316刘诚昊 用链表实现队列

    20162316刘诚昊 2017-2018-2 <Java程序设计>用链表实现队列 实验要求 1 参考程序15.5给出方法deque,first,isEmpty,size,toString ...

最新文章

  1. Qt5开发及实例学习之标准颜色对话框类QColorDialog:选择某种颜色
  2. BZOJ 1443: [JSOI2009]游戏Game
  3. git push origin master和git push有什么区别?
  4. django模板中使用JQ代码实现瀑布流显示效果
  5. 【DIY】可能是最实用最便宜的 arduino 温湿度计方案,200615整合家用声控温湿度计完整方案...
  6. NYOJ 417 死神来了
  7. Mysql基础知识:索引
  8. ABAP gateway里OData url里的select操作是如何在后端实现的
  9. String类、StringBuffer类、StringBuilder类的区别
  10. 《RHEL6.3 FTP服务器虚拟用户的配置(含图)》——如此简单
  11. 电路原理解析_接近开关检测旋转设备的工作原理
  12. Perl获取前后任意月份月末
  13. python中基础知识_Python中的一些基础知识
  14. MCtalk教育大咖说:不忘教育初心,柚子练琴聚焦音乐陪练
  15. tm影像辐射定标_ENVI中TM的辐射定标和大气校正
  16. ADS1115使用之电路与程序
  17. 利用QQ游戏破解QQ密码
  18. 为什么这么多人会选择单页设计?
  19. maya之坐标轴与模型显示状态
  20. Peewee fn 用法

热门文章

  1. 第1章 Raspberry Pi树莓派系统镜像制作方法
  2. MacBook如何切换操作win系统?2023最新版Parallels虚拟机
  3. 一起聊聊 dB、dB、dBm、dBi 吧!
  4. 高德地图红绿灯读秒是怎么实现的?(二)
  5. TAQ服务器npc多久自动交物资,魔兽世界怀旧服:奥罗服务器物资捐献完成,已成国服第一个开门...
  6. 安防监控百科:如何将家用监控摄像机连接到手机
  7. Windows10 2004五月更新正式版官方ISO纯净版镜像下载
  8. VariantsTransport_SAP刘梦_新浪博客
  9. 华为云EAI应用(1)定制语言合成
  10. android 位于底部的tab,GitHub - DevinFu/BottomTabBar: Android应用中位于底部的tab栏