标签:
排序算法的分类:
主要介绍内部排序:
package com.hsx;
/**
* 直接插入排序
*/
public class DirectInsert {
public static void main(String[] args) {
int[] array = {38,65,97,76,13,27,49};
System.out.println("排序之前:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
insertSort(array);
System.out.println();
System.out.println("排序之后:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
/**
* 直接插入排序算法
* @param a
*/
public static int[] insertSort(int[] a) {
if (a != null) {
for (int i = 1; i < a.length; i++) {
// 带插入的元素
int temp = a[i];
int j;
// 从已排序的部分的最后一个元素开始比较
for (j = i - 1; j >= 0; j--) {
// 将大于带插入temp元素向后移
if (a[j] > temp) {
a[j + 1] = a[j];
}
else {
break;
}
}
a[j + 1] = temp;
}
}
}
}
package com.hsx;
/**
* 二分插入排序
*/
public class BinaryInsert {
public static void main(String[] args) {
int[] array = {38,65,97,76,13,27,49};
System.out.println("排序之前:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
insertSort(array);
System.out.println();
System.out.println("排序之后:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
/**
* 二分插入排序算法
* @param a
*/
public static void insertSort(int[] a) {
if (a != null) {
for (int i = 1; i < a.length; i++) {
// 待插入的元素
int temp = a[i];
int right = i - 1;
int left = 0;
int mid = 0;
while (left <= right) {
mid = (left + right) / 2;
if (temp < a[mid]) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
for (int j = i - 1; j >= left; j--) {
a[j + 1] = a[j];
}
if (left != i) {
a[left] = temp;
}
}
}
}
}
package com.hsx;
/**
* 希尔排序
*/
public class ShellInsert {
public static void main(String[] args) {
int[] array = {38,65,97,76,13,27,49};
System.out.println("排序之前:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
insertSort(array);
System.out.println();
System.out.println("排序之后:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
/**
* 希尔排序算法
* @param a
*/
public static void insertSort(int[] a) {
int d = a.length;
while (true) {
d = d / 2;
for (int i = 0; i < d; i++) {
for (int j = i + d; j < a.length; j = j + d) {
int temp = a[j];
int k;
for (k = j - d; k >= 0 && a[k] > temp; k = k - d) {
a[k + d] = a[k];
}
a[k + d] = temp;
}
}
if (d == 1) {
break;
}
}
}
}
package com.hsx;
/**
* 简单选择排序
*/
public class SimpleSelect {
public static void main(String[] args) {
int[] array = {38,65,97,76,13,27,49};
System.out.println("排序之前:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
selectSort(array);
System.out.println();
System.out.println("排序之后:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
/**
* 简单选择排序算法
* @param a
*/
public static void selectSort(int[] a) {
if (a != null) {
for (int i = 0; i < a.length; i++) {
int min = a[i];
int n = i; //最小索引数
for (int j = i + 1; j < a.length; j++) {
if(min > a[j]) {
min = a[j];
n = j;
}
}
a[n] = a[i];
a[i] = min;
}
}
}
}
package com.hsx;
import java.util.Arrays;
/**
* 堆排序
*/
public class HeapSelect {
public static void main(String[] args) {
int[] array = {38,65,97,76,13,27,49};
System.out.println("排序之前:");
System.out.println(Arrays.toString(array));
selectSort(array);
System.out.println("排序之后:");
System.out.println(Arrays.toString(array));
}
/**
* 堆排序
* @param a
*/
public static void selectSort(int[] a) {
if (a != null) {
// 循环建堆
int length = a.length;
for (int i = 0; i < length - 1; i++) {
//建堆
buildMaxHeap(a, length - 1 - i);
//交换
swap(a, 0, length - 1 - i);
}
}
}
/**
* 建堆:建大顶堆
* @param a
* @param lastIndex
*/
public static void buildMaxHeap(int[] a, int lastIndex) {
// 从lastIndex出节点(最后一个节点)的父节点开始
for (int i = (lastIndex - 1) / 2; i >= 0; i--) {
// k保存正在判断的的节点
int k = i;
// 如果当前k节点的字节点存在
while (k * 2 + 1 <= lastIndex) {
// k节点的左节点的索引
int biggerIndex = 2 * k + 1;
// 如果biggerIndex小与lastIndex,即biggerIndex代表的右节点存在
if (biggerIndex < lastIndex) {
// 如果右及诶单的值较大
if (a[biggerIndex] < a[biggerIndex + 1]) {
// biggerIndex总是记录较大子节点的索引
biggerIndex++;
}
}
// 如果k节点的值小于其较大的子节点的值
if (a[k] < a[biggerIndex]) {
// 交换
swap(a, k, biggerIndex);
// 将biggerIndex赋予k,开始while循环的下一次循环,重新保证k节点的值大于其左右节点的值
k = biggerIndex;
}
else {
break;
}
}
}
}
/**
* 交换
* @param a
* @param j
*/
public static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
package com.hsx;
import java.util.Arrays;
/**
* 冒泡排序
*/
public class BubbleSort {
public static void main(String[] args) {
int[] array = {38,65,97,76,13,27,49};
System.out.println("排序之前:");
System.out.println(Arrays.toString(array));
swapSort(array);
System.out.println("排序之后:");
System.out.println(Arrays.toString(array));
}
/**
* 冒泡排序算法
* @param a
*/
public static void swapSort(int[] a) {
if (a != null) {
// 比较多少次
for (int i = 0; i < a.length; i++) {
// 每两个元素之间进行比较
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
}
}
package com.hsx;
import java.util.Arrays;
/**
* 快速排序
*/
public class QuickSort {
public static void main(String[] args) {
int[] array = {38,65,97,76,13,27,49};
System.out.println("排序之前:");
System.out.println(Arrays.toString(array));
swapSort(array);
System.out.println("排序之后:");
System.out.println(Arrays.toString(array));
}
/**
* 快速排序算法
* @param a
*/
public static void swapSort(int[] a) {
if (a != null) {
quickSort(a, 0, a.length - 1);
}
}
/**
* 递归调用
* @param a
* @param low
* @param high
*/
public static void quickSort(int[] a, int low, int high) {
if (low < high) {
int mid = getMid(a, low, high);
quickSort(a, low, mid - 1);
quickSort(a, mid + 1, high);
}
}
public static int getMid(int[] a, int low, int high) {
int key = a[low];
while (low < high) {
// 找到比关键字小的元素位置
while (low < high && a[high] >= key) {
high--;
}
a[low] = a[high];
while (low < high && a[low] <= key) {
low++;
}
a[high] = a[low];
}
a[low] = key;
return low;
}
}
package com.hsx;
import java.util.Arrays;
/**
* 归并排序
*/
public class MergerSort {
public static void main(String[] args) {
int[] array = {38,65,97,76,13,27,49};
System.out.println("排序之前:");
System.out.println(Arrays.toString(array));
mergerSort(array, 0, array.length - 1);
System.out.println("排序之后:");
System.out.println(Arrays.toString(array));
}
/**
* 归并排序算法
* @param a
*/
public static void mergerSort(int[] a, int left, int right) {
if (a != null) {
if (left < right) {
int mid = (left + right) / 2;
mergerSort(a, left, mid);
mergerSort(a, mid + 1, right);
merger(a, left, mid, right);
}
}
}
public static void merger(int[] a, int left, int mid, int right) {
int[] tempA = new int[a.length];
int middle = mid + 1;
int temp = left;
int third = left;
while (left <= mid && middle <= right) {
// 从两个数字中选取较小的元素放入到中间数组
if (a[left] <= a[middle]) {
tempA[third++] = a[left++];
}
else {
tempA[third++] = a[middle++];
}
}
// 将剩余的部分放入到中间数组
while (left <= mid) {
tempA[third++] = a[left++];
}
while (middle <= right) {
tempA[third++] = a[middle++];
}
// 将中间数组复制到原数组
while (temp <= right) {
a[temp] = tempA[temp++];
}
}
}
package com.hsx;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 基数排序(桶排序)
*/
public class BucketSort {
public static void main(String[] args) {
int[] array = {38,65,97,76,13,27,49};
System.out.println("排序之前:");
System.out.println(Arrays.toString(array));
bucketSort(array);
System.out.println("排序之后:");
System.out.println(Arrays.toString(array));
}
/**
* 基数排序算法
* @param a
*/
public static void bucketSort(int[] a) {
//找到最大数,确定要排序几趟
int max = 0;
for (int i = 0; i < a.length; i++) {
if(max<a[i]){
max = a[i];
}
}
//判断位数
int times = 0;
while(max>0){
max = max/10;
times++;
}
//建立十个队列
List<ArrayList> queue = new ArrayList<ArrayList>();
for (int i = 0; i < 10; i++) {
ArrayList queue1 = new ArrayList();
queue.add(queue1);
}
//进行times次分配和收集
for (int i = 0; i < times; i++) {
//分配
for (int j = 0; j < a.length; j++) {
int x = a[j]%(int)Math.pow(10, i+1)/(int)Math.pow(10, i);
ArrayList queue2 = queue.get(x);
queue2.add(a[j]);
queue.set(x,queue2);
}
//收集
int count = 0;
for (int j = 0; j < 10; j++) {
while(queue.get(j).size()>0){
ArrayList<Integer> queue3 = queue.get(j);
a[count] = queue3.get(0);
queue3.remove(0);
count++;
}
}
}
}
}
标签:
原文地址:http://blog.csdn.net/hsx1612727380/article/details/51170514