[Java]各种经典排序

package sort;

import java.util.ArrayList;
import java.util.List;

public class SortMethods {
	public static void main(String args[]) {
		int a[] = { 5, 8, 1, 4, 3, 1, 2, 3, 7, 6 };
		int size = a.length;

		// BubbleSort(a, size);
		// InsertSort(a, size);
		SelectSort(a, size);
		// QuickSort(a, size);
		// HeapSort(a, size);
		// ShellSort(a, size);
		// MergeSort(a, size);
		// CountSort(a, size);
		print(a, size);
	}

	public static void print(int[] a, int size) {
		for (int i = 0; i < size; i++) {
			System.out.print(a[i] + " ");
		}
	}

	// 冒泡排序
	public static void BubbleSort(int[] a, int size) {
		for (int i = 0; i < size; i++) {
			for (int j = size - 1; j > i; j--) {
				if (a[j] < a[j - 1]) {
					Swap(a, j - 1, j);
				}
			}
		}
	}

	// 选择排序
	public static void SelectSort(int[] a, int size) {
		int min; // min指向最小数的下标
		for (int i = 0; i < size; i++) {
			min = i;
			for (int j = i + 1; j < size; j++) {
				if (a[min] > a[j]) {
					min = j;
				}
			}
			if (min != i)
				Swap(a, i, min);
		}
	}

	// 插入排序
	public static void InsertSort(int[] a, int size) {
		int temp, j;
		for (int i = 1; i < size; i++) {
			temp = a[i];
			j = i - 1;
			while (j >= 0 && temp < a[j]) {
				a[j + 1] = a[j];
				j--;
			}
			a[j + 1] = temp;
		}
	}

	// 快速排序
	public static void QuickSort(int[] a, int size) {
		RecQuickSort(a, 0, size - 1);
	}

	public static void RecQuickSort(int[] a, int low, int high) {
		int p;
		if (low < high) {
			p = Partition(a, low, high);
			RecQuickSort(a, low, p - 1);
			RecQuickSort(a, p + 1, high);
		}
	}

	public static int Partition(int[] a, int low, int high) {
		int p = a[low];
		while (low < high) {
			while (low < high && a[high] >= p) {
				high--;
			}
			a[low] = a[high];
			while (low < high && a[low] <= p) {
				low++;
			}
			a[high] = a[low];
		}
		a[low] = p;
		return low;
	}

	// 堆排序
	public static void HeapSort(int[] a, int size) {
		BuildHeap(a, size);
		for (int i = size - 1; i >= 0; i--) {
			Swap(a, 0, i);
			HeapAdjust(a, 0, i);
		}
	}

	public static void BuildHeap(int[] a, int size) {
		for (int i = size / 2 - 1; i >= 0; i--) {
			HeapAdjust(a, i, size);
		}
	}

	public static void HeapAdjust(int[] a, int i, int size) {
		int temp = a[i];
		int j = 2 * i + 1;
		while (j < size) {
			if (j + 1 < size && a[j + 1] > a[j])
				j++;

			if (a[j] < temp)
				break;

			a[i] = a[j];
			i = j;
			j = 2 * i + 1;
		}
		a[i] = temp;
	}

	// 希尔排序
	public static void ShellSort(int[] a, int size) {
		int temp, k;
		for (int i = size / 2; i > 0; i /= 2) {
			for (int j = i; j < size; j = j + i) {
				temp = a[j];
				k = j;
				while (k - i >= 0 && temp < a[k - i]) {
					a[k] = a[k - i];
					k = k - i;
				}
				a[k] = temp;
			}
		}
	}

	// 归并排序
	public static void MergeSort(int[] a, int size) {
		RecMerge(a, 0, size - 1);
	}

	public static void RecMerge(int[] a, int low, int high) {
		if (low < high) {
			int middle = (low + high) / 2;
			RecMerge(a, low, middle);
			RecMerge(a, middle + 1, high);
			Merge(a, low, middle, high);
		}
	}

	public static void Merge(int[] a, int low, int middle, int high) {
		int temp1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		int temp2[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		int n1 = middle - low + 1;
		int n2 = high - middle;

		for (int i = 0; i < n1; i++) {
			temp1[i] = a[low + i];
		}
		for (int i = 0; i < n2; i++) {
			temp2[i] = a[middle + i + 1];
		}
		temp1[n1] = 100;
		temp2[n2] = 100;
		int j = 0, k = 0;
		for (int i = low; i <= high; i++) {
			if (temp1[j] < temp2[k]) {
				a[i] = temp1[j];
				j++;
			} else {
				a[i] = temp2[k];
				k++;
			}
		}
	}

	// 计数排序
	public static void CountSort(int a[], int size) {
		int MAX = 0;
		for (int i = 0; i < size; i++) {
			if (a[i] > MAX)
				MAX = a[i];
		}
		MAX++;
		List<Integer> temp = new ArrayList<Integer>();
		for (int i = 0; i < MAX; i++) {
			temp.add(i, 0);
		}

		for (int i = 0; i < size; i++) {
			temp.set(a[i], (Integer) (temp.get(a[i])) + 1);
		}

		int j, k = 0;
		for (int i = 0; i < MAX; i++) {
			j = temp.get(i);
			while (j != 0) {
				a[k] = i;
				k++;
				j--;
			}
		}
	}

	public static void Swap(int a[], int i, int j) {
		int temp;
		temp = a[j];
		a[j] = a[i];
		a[i] = temp;
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java十大经典排序算法包括:插入排序、选择排序、冒泡排序、希尔排序、归并排序、快速排序、堆排序、计数排序、桶排序和基数排序。 1. 插入排序:将数组分为已排序和未排序两部分,每次将未排序部分的元素插入到已排序部分的适当位置。 2. 选择排序:每次从未排序部分选择最小的元素,与未排序部分的第一个元素交换位置。 3. 冒泡排序:从左到右依次比较相邻的两个元素,将较大的元素往右移动。 4. 希尔排序:将整个待排序的序列分割成若干个子序列,再对子序列进行插入排序。 5. 归并排序:将数组分为两个子数组,分别进行排序,然后将两个有序子数组合并成一个有序数组。 6. 快速排序:选择一个基准元素,将比基准元素小的放在左边,比基准元素大的放在右边,然后对左右两部分递归进行快速排序。 7. 堆排序:先将数组构建成一个最大堆,然后依次将堆顶元素与最后一个元素交换,并调整堆结构,重复此过程直到整个数组有序。 8. 计数排序:统计数组中每个元素的出现次数,然后按照元素的大小顺序输出。 9. 桶排序:将待排序元素分配到不同的桶中,对每个桶内的元素进行排序,最后按照桶的顺序输出所有元素。 10. 基数排序:将待排序元素按照位数进行排序,先根据个位数排序,再根据十位数排序,依次类推,直到所有位数都排序完成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值