java数据的排序方法

来源:互联网 发布:淘宝一件代发教学 编辑:程序博客网 时间:2024/06/10 02:06

代码:

public class SortTest {
public static void printValue(int[] number) {// 打印数组元素的内容
if (number == null) {// 如果该数组是空的则返回,不执行下面的语句。
return;
}
for (int i = 0; i < number.length; i++) {// 用for循环将数据元素取出
System.out.print(number[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] number = new int[] { 53, 10, 2, 4, 54, 14, -15, -2, 0 };// 创建一个int型数组并初始化
System.out.print("使用\"选择排序法\"排序前的数组:");
printValue(number);// 调用printValue方法将number数组的元素打印出
Sort test = new SelectionSort();// 创建SelectionSort对象
// 调用实现父类接口中的两个方法:ascSort和descSort
System.out.print("\t升序排序的结果:");
printValue(test.ascSort(number));
System.out.print("\t降序排序的结果:");
printValue(test.descSort(number));
System.out.print("\n使用\"冒泡排序法\"排序前的数组:");
printValue(number);// 调用printValue方法将number数组的元素打印出
test = new BubbleSort();// 创建BubbleSort对象
// 调用实现父类接口中的两个方法:ascSort和descSort
System.out.print("\t升序排序的结果:");
printValue(test.ascSort(number));
System.out.print("\t降序排序的结果:");
printValue(test.descSort(number));
System.out.print("\n使用\"快速排序法\"排序前的数组:");
printValue(number);// 调用printValue方法将number数组的元素打印出
test = new QuickSort();// 创建QuickSort对象
// 调用实现父类接口中的两个方法:ascSort和descSort
System.out.print("\t升序排序的结果:");
printValue(test.ascSort(number));
System.out.print("\t降序排序的结果:");
printValue(test.descSort(number));
}
}
interface Sort {// 排序接口
// 在接口中定义了两个抽象方法
public int[] ascSort(int[] number);// 对整型数组按升序排序,并返回排好序的数组
public int[] descSort(int[] number);// 对整型数组按降序排序,并返回排好序的数组
}
// 选择排序法
class SelectionSort implements Sort {// 创建一个类实现接口
public int[] ascSort(int[] number) {// 升序排序法
if (number == null) {// 如果该数组是空的则返回,不执行下面的语句。
return null;
}
int[] copyArray = (int[]) number.clone();// 调用数组的clone方法,直接克隆一个数组。
int size = copyArray.length;
// 从头遍历数组元素
for (int i = 0; i < size; i++) {
// 遍历下标为i之后的元素
for (int j = i; j < size; j++) {
// 如果数组前面的值比后面的值大,则交换位置
if (copyArray[i] > copyArray[j]) {
exchange(copyArray, i, j);
}
}
}
return copyArray;
}
public int[] descSort(int[] number) {// 降序排序法
if (number == null) {// 如果该数组是空的则返回,不执行下面的语句。
return null;
}
int[] copyArray = (int[]) number.clone();// 调用数组的clone方法,直接克隆一个数组。
int size = copyArray.length;
// 从头遍历数组元素
for (int i = 0; i < size; i++) {
// 遍历下标为i之后的元素
for (int j = i; j < size; j++) {
// 如果数组前面的值比后面的值小,则交换位置
if (copyArray[i] < copyArray[j]) {
exchange(copyArray, i, j);
}
}
}
return copyArray;
}
// 交换数组中下标的值,其中:from源下标, to目标下标
public void exchange(int[] toArray, int from, int to) {
int temp = toArray[from];
toArray[from] = toArray[to];
toArray[to] = temp;
}
}
// 冒泡排序法
class BubbleSort implements Sort {// 创建一个类去实现Sort接口
SelectionSort ss = new SelectionSort();// 创建SelectionSort类对象
public int[] ascSort(int[] number) {// 升序排序法
if (number == null) {// 如果该数组是空的则返回,不执行下面的语句。
return null;
}
int[] copyArray = (int[]) number.clone();// 调用数组的clone方法,直接克隆一个数组。
// 用当前的数组元素和在它之后的所有元素进行比较
for (int i = 0; i < copyArray.length; i++) {
for (int j = i + 1; j < copyArray.length; j++) {
// 将下标为i的数与下标为j的数进行比较,如果后面的元素小于前面的则进行元素交换
if (copyArray[i] > copyArray[j]) {
ss.exchange(copyArray, i, j);
}
}
}
return copyArray;
}
public int[] descSort(int[] number) {// 降序排序法
if (number == null) {// 如果该数组是空的则返回,不执行下面的语句。
return null;
}
int[] copyArray = (int[]) number.clone();// 调用数组的clone方法,直接克隆一个数组。
// 用当前的数组元素和在它之后的所有元素进行比较
for (int i = 0; i < copyArray.length; i++) {
for (int j = i + 1; j < copyArray.length; j++) {
// 将下标为i的数与下标为j的数进行比较,如果后面的元素大于前面的则进行元素交换
if (copyArray[i] < copyArray[j]) {
ss.exchange(copyArray, i, j);
}
}
}
return copyArray;
}
}
// 快速排序法
class QuickSort implements Sort {// 创建一个类去实现Sort接口
SelectionSort ss = new SelectionSort();// 创建SelectionSort类对象
public int[] ascSort(int[] number) {// 升序排序法
if (number == null) {// 如果该数组是空的则返回,不执行下面的语句。
return null;
}
int[] copyArray = (int[]) number.clone();// 调用数组的clone方法,直接克隆一个数组。
return this.asc_quickSort(copyArray, 0, copyArray.length - 1);// 调用asc_quickSort方法
}
public int[] descSort(int[] number) {// 降序排序法
if (number == null) {// 如果该数组是空的则返回,不执行下面的语句。
return null;
}
int[] copyArray = (int[]) number.clone();// 调用数组的clone方法,直接克隆一个数组。
return this.desc_quickSort(copyArray, 0, copyArray.length - 1);// 调用desc_quickSort方法
}
// 用递归的方式实现快速升序排序。copyArray:待排序的数组,begin:开始下标,end:终止下标
private int[] asc_quickSort(int[] copyArray, int begin, int end) {
if (begin < end) {
int n = asc_partition(copyArray, begin, end);
asc_quickSort(copyArray, begin, n - 1);
asc_quickSort(copyArray, n + 1, end);
}
return copyArray;
}
// 用递归的方式实现快速降序排序。copyArray:待排序的数组,begin:开始下标,end:终止下标
private int[] desc_quickSort(int[] copyArray, int begin, int end) {
if (begin < end) {
int n = desc_partition(copyArray, begin, end);
desc_quickSort(copyArray, begin, n - 1);
desc_quickSort(copyArray, n + 1, end);
}
return copyArray;
}
// 升序排序。以数组中第一个元素为标准,把大于该数的元素往后排,把小于该数的元素往前排
private int asc_partition(int[] copyArray, int first, int last) {
int temp = copyArray[first];
int n = first;
for (int i = first + 1; i <= last; i++) {
if (copyArray[i] < temp) {
n++;
ss.exchange(copyArray, n, i);
}
}
ss.exchange(copyArray, first, n);
return n;
}
// 降序排序。以数组中第一个元素为标准,把大于该数的元素往前排,把小于该数的元素往后排
private int desc_partition(int[] copyArray, int first, int last) {
int temp = copyArray[first];
int n = first;
for (int i = first + 1; i <= last; i++) {
if (copyArray[i] > temp) {
n++;
ss.exchange(copyArray, n, i);
}
}
ss.exchange(copyArray, first, n);
return n;
}
}

原创粉丝点击