public class SelectionSort {

	private static void selectSortTest() {
int[] sortArray = { 5, 2, 4, 1, 3 };
System.out.print("选择排序前: ");
Utils.printArray(sortArray);
selectSort(sortArray);
System.out.print("选择排序后: ");
Utils.printArray(sortArray);
} public static void selectSort(int[] sort) {
int temp;
for (int i = 0; i < sort.length - 1; i++) {
for (int j = i + 1; j < sort.length; j++) {
if (sort[i] > sort[j]) {
temp = sort[i];
sort[i] = sort[j];
sort[j] = temp;
}
}
}
} public static void heapSortTest(){
int[] arr = { 5, 2, 4, 1, 3 };
Utils.printArray("堆排序前:",arr);
heapSort(arr);
Utils.printArray("堆排序后:",arr);
} public static void heapSort(int[] arr) {
int arrLen = arr.length;
int temp = 0; //建立堆
for (int i = (arrLen-1) / 2; i >= 0; i--)
adjustHeap(arr, i, arrLen); //调整堆
for (int i = arrLen - 2; i >= 0; i--) {
temp = arr[i + 1];
arr[i + 1] = arr[0];
arr[0] = temp;
adjustHeap(arr, 0, i + 1); // Utils.printArray("第"+(9-i)+"次调整:",arr);
}
} public static void adjustHeap(int[] arr, int ri, int n) {
int temp = arr[ri];
int ci = 2 * ri + 1;
while (ci <= n - 1) {
if (ci < n - 1 && arr[ci] < arr[ci + 1])
ci++; if (temp >= arr[ci])
break;
arr[(ci - 1) / 2] = arr[ci];
ci = 2 * ci + 1;
}
arr[(ci - 1) / 2] = temp;
} public static void main(String [] args){
selectSortTest();
heapSortTest();
}
}

  

最新文章

  1. .NET string字符串的截取、移除、替换、插入
  2. 浅谈数据库优化方案--表和SQL
  3. 配置maven
  4. HTTP协议学习---(五)压缩
  5. LeetCode题解——Longest Substring Without Repeating Characters
  6. python导入matplotlib模块出错
  7. 浮动层固定兼容IE6 position:fixed的最佳解决方案
  8. 打算自己做app,你们做过吗?
  9. Python自学笔记-logging模块详解
  10. 700名黑客无法攻破Trillium安全防护软件 欲以网络安全险获利
  11. java的hashmap与hashtable说明,简单易理解
  12. 深入理解css3中的线性渐变
  13. 共创力咨询推出《静态代码分析(PCLint)高级实务培训》课程!
  14. C#smtp邮件消息提醒的一些bug总结
  15. 单元测试mock当前时间
  16. 实体关系图应用——google ads
  17. Android 利用二次贝塞尔曲线模仿购物车加入物品抛物线动画
  18. CF983B XOR-pyramid
  19. &lt;Spark Streaming&gt;&lt;Flume&gt;&lt;Integration&gt;
  20. Spring beanFactory ApplicationContext

热门文章

  1. Oracle 存储过程(2)
  2. call()与apply()区别
  3. 很棒的jQuery代码片段分享
  4. Web字体工具整理,网页图标字体以及使用方法整理
  5. (转)ThinkPHP系统常量
  6. android handler机制简单介绍
  7. Apache下Worker模式MPM参数分析
  8. iOS 使用xmpp做聊天客户端
  9. TalkingData Cocos2dx集成指南【最新】
  10. 错误:Duplicate interface definition for class