常见排序算法
来源:互联网 发布:task host windows 编辑:程序博客网 时间:2024/05/22 17:02
不多说直接上代码,代码中实现了:快速排序、堆排序、直接插入排序、希尔排序
package com.scalahome;/** * Created by Administrator on 2016/8/14. */public class SortDemo { public static void main(String[] args) { int[] array = new int[]{2,24,4,53,2551,32,531,4,1,43,-24,2,53}; SortDemo sortDemo = new SortDemo(); sortDemo.printArray(array);// sortDemo.quickSort(array);// sortDemo.heapSort(array);// sortDemo.insertSort(array); sortDemo.shellSort(array); sortDemo.printArray(array); } /** * 希尔排序 * * @param array */ public void shellSort(int[] array) { for(int gap = array.length / 2; gap > 0; gap = gap / 2) { for(int offset = 0; offset < gap; offset++) { insertSort(array, gap, offset); } } } public void insertSort(int[] array, int gap, int offset) { for(int i = offset + gap; i < array.length; i += gap) { int j = i - gap; int newElement = array[i]; for(; j >= 0 && array[j] > newElement; j -= gap) { array[j + gap] = array[j]; } array[j + gap] = newElement; } } /** * 直接插入排序 * * @param array */ public void insertSort(int[] array) { for(int i = 1; i < array.length; i++) { int j = i - 1; int newElement = array[i]; for(; j >= 0 && array[j] > newElement; j--) { array[j + 1] = array[j]; } array[j + 1] = newElement; } } /** * 堆排序 * * @param array */ public void heapSort(int[] array) { for(int i = array.length - 1; i > 0; i--) { maxHeap(array, i); swap(array, 0, i); } } public void maxHeap(int[] array, int lastPosition) { for(int i = lastPosition / 2; i >= 0; i--) { int j = i; while (2 * j + 1 <= lastPosition) { int firstChildIndex = 2 * j + 1; int secondChildIndex = firstChildIndex + 1; int bigger = firstChildIndex; if(secondChildIndex < lastPosition && array[secondChildIndex] > array[firstChildIndex]) { bigger = secondChildIndex; } if(array[bigger] > array[j]) { swap(array, j, bigger); j = bigger; } else { break; } } } } /** * 快速排序 * * @param array */ public void quickSort(int[] array) { quickSort(array, 0, array.length - 1); } public void quickSort(int[] array, int start, int to) { if(start >= to) return; int position = partitionSort(array, start, to); quickSort(array, start, position); quickSort(array, position + 1, to); } public int partitionSort(int[] array, int from, int to) { int i = from; int j = to; int value = array[i]; int flag = 0; while(j > i) { if(flag == 0) { if(array[j] < value) { swap(array, i, j); flag = (flag + 1) % 2; } else { j--; } } else { if(array[i] > value) { swap(array, i, j); flag = (flag + 1) % 2; } else { i++; } } } return i; } public void swap(int[] array, int pos1, int pos2) { array[pos1] = array[pos1] ^ array[pos2]; array[pos2] = array[pos1] ^ array[pos2]; array[pos1] = array[pos1] ^ array[pos2]; } public void printArray(int[] array) { StringBuilder buffer = new StringBuilder(); buffer.append('['); for(int item : array) { buffer.append(item); buffer.append(','); } buffer.setLength(buffer.length() - 1); buffer.append(']'); System.out.println(buffer.toString()); }}
1 0
- 常见的排序算法
- 常见排序算法
- 常见排序算法代码
- 常见排序算法
- 常见排序算法总结
- 常见排序算法
- 常见排序算法学习
- 常见排序算法总结
- 常见的排序算法
- 常见排序算法分析
- 常见排序算法总结
- 常见排序算法
- 常见排序算法
- 常见排序算法介绍
- 常见排序算法小结
- 常见排序算法总结
- 常见经典排序算法
- 常见排序算法
- bzoj1857 SCOI2010传送带
- ssh简述
- android选择时间攻略
- 【VS开发】使用WinPcap编程(2)——打开网络设备并且开始捕获数据包
- Xutils之ViewUtils解析
- 常见排序算法
- 软件工程学习总结
- scala基础------>Trait Iterable
- 【白话设计模式三】迭代器模式(Iterator)
- [刷题]算法竞赛入门经典(第2版) 4-10/UVa815 - Flooded!
- 索引合并和组合索引的比较
- Python学习记录4(语句)
- android开发之添加标签与删除标签
- Oracle数据库双机热备方案