编程语言
首页 > 编程语言> > 排序算法的Java实现Demo

排序算法的Java实现Demo

作者:互联网

冒泡排序

/**
 * 冒泡排序
 * 
 * @param arr
 * @author zadaya
 */
public static void bubbleSort(int[] arr) {
	int sw = 0; // 存放交换值
	for (int i = 0; i < arr.length; i++) {
		for (int j = arr.length - 1; j > i; j--) {
			if (arr[j] < arr[j - 1]) {
				sw = arr[j];
				arr[j] = arr[j - 1];
				arr[j - 1] = sw;
			}
		}
	}
}

选择排序

/**
 * 选择排序
 * 
 * @param arr
 * @author zadaya
 */
public static void selectionSort(int[] arr) {
	int minIndex;
	int sw;
	for (int i = 0; i < arr.length - 1; i++) {
		minIndex = i;
		for (int j = i + 1; j < arr.length; j++) {
			if (arr[j] < arr[minIndex]) {
				sw = arr[minIndex];
				arr[minIndex] = arr[j];
				arr[j] = sw;
			}
		}
	}
}

插入排序

/**
 * 插入排序
 * 
 * @param arr
 * @author zadaya
 */
public static void insertionSort(int[] arr) {
	int current;
	int preIndex;

	for (int i = 1; i < arr.length; i++) {
		current = arr[i];
		preIndex = i - 1;
		while (preIndex >= 0 && arr[preIndex] > current) {
			arr[preIndex + 1] = arr[preIndex];
			preIndex--;
		}
		arr[preIndex + 1] = current;
	}
}

快速排序

/**
 * 快速排序
 * 
 * @param arr
 * @param leftIndex
 * @param rightIndex
 * @author zadaya
 */
public static void quickSort(int[] arr, int leftIndex, int rightIndex) {
	if (leftIndex >= rightIndex) // 如果左浮标与右浮标相遇则认为完成该分区排序
		return;

	int left = leftIndex, right = rightIndex;
	int key = arr[right];
	while (left < right) {
		while (left < right && arr[left] <= key) {
			left++;
		}
		arr[right] = arr[left];

		while (right > left && arr[right] >= key) {
			right--;
		}
		arr[left] = arr[right];
	}
	arr[right] = key;

	quickSort(arr, leftIndex, left - 1);
	quickSort(arr, right + 1, rightIndex);

}

 Demo

import java.util.Date;

public class sortDemo {
	public static void main(String[] args) {
		int[] testArr = { 33, 25, 11, 56, 1, -336, 88, 231, -89, 0, 29, -65, 922 };

		boolean flag = true;
		for (int i : testArr) {
			if (flag == true) {
				flag = false;
				System.out.print("原始数列:" + i);
			} else
				System.out.print(", " + i);
		}
		System.out.println();

		flag = true;
		int[] testArr1 = { 33, 25, 11, 56, 1, -336, 88, 231, -89, 0, 29, -65, 922 };
		long date11 = new Date().getTime();
		quickSort(testArr1, 0, testArr1.length - 1);
		long date12 = new Date().getTime();
		for (int j : testArr1) {
			if (flag == true) {
				flag = false;
				System.out.print("快速排序结果:" + j);
			} else
				System.out.print(", " + j);
		}
		System.out.println();
		System.out.println("时间差:" + (date12 - date11));

		flag = true;
		int[] testArr2 = { 33, 25, 11, 56, 1, -336, 88, 231, -89, 0, 29, -65, 922 };
		long date21 = new Date().getTime();
		bubbleSort(testArr2);
		long date22 = new Date().getTime();
		for (int j : testArr2) {
			if (flag == true) {
				flag = false;
				System.out.print("冒泡排序结果:" + j);
			} else
				System.out.print(", " + j);
		}
		System.out.println();
		System.out.println("时间差:" + (date22 - date21));

		flag = true;
		int[] testArr3 = { 33, 25, 11, 56, 1, -336, 88, 231, -89, 0, 29, -65, 922 };
		long date31 = new Date().getTime();
		selectionSort(testArr3);
		long date32 = new Date().getTime();
		for (int j : testArr3) {
			if (flag == true) {
				flag = false;
				System.out.print("选择排序结果:" + j);
			} else
				System.out.print(", " + j);
		}
		System.out.println();
		System.out.println("时间差:" + (date32 - date31));

		flag = true;
		int[] testArr4 = { 33, 25, 11, 56, 1, -336, 88, 231, -89, 0, 29, -65, 922 };
		long date41 = new Date().getTime();
		insertionSort(testArr4);
		long date42 = new Date().getTime();
		for (int j : testArr4) {
			if (flag == true) {
				flag = false;
				System.out.print("插入排序结果:" + j);
			} else
				System.out.print(", " + j);
		}
		System.out.println();
		System.out.println("时间差:" + (date42 - date41));
	}

	/**
	 * 冒泡排序
	 * 
	 * @param arr
	 * @author zadaya
	 */
	public static void bubbleSort(int[] arr) {
		int sw = 0; // 存放交换值
		for (int i = 0; i < arr.length; i++) {
			for (int j = arr.length - 1; j > i; j--) {
				if (arr[j] < arr[j - 1]) {
					sw = arr[j];
					arr[j] = arr[j - 1];
					arr[j - 1] = sw;
				}
			}
		}
	}

	/**
	 * 选择排序
	 * 
	 * @param arr
	 * @author zadaya
	 */
	public static void selectionSort(int[] arr) {
		int minIndex;
		int sw;
		for (int i = 0; i < arr.length - 1; i++) {
			minIndex = i;
			for (int j = i + 1; j < arr.length; j++) {
				if (arr[j] < arr[minIndex]) {
					sw = arr[minIndex];
					arr[minIndex] = arr[j];
					arr[j] = sw;
				}
			}
		}
	}

	/**
	 * 插入排序
	 * 
	 * @param arr
	 * @author zadaya
	 */
	public static void insertionSort(int[] arr) {
		int current;
		int preIndex;

		for (int i = 1; i < arr.length; i++) {
			current = arr[i];
			preIndex = i - 1;
			while (preIndex >= 0 && arr[preIndex] > current) {
				arr[preIndex + 1] = arr[preIndex];
				preIndex--;
			}
			arr[preIndex + 1] = current;
		}
	}

	/**
	 * 快速排序
	 * 
	 * @param arr
	 * @param leftIndex
	 * @param rightIndex
	 * @author zadaya
	 */
	public static void quickSort(int[] arr, int leftIndex, int rightIndex) {
		if (leftIndex >= rightIndex) // 如果左浮标与右浮标相遇则认为完成该分区排序
			return;

		int left = leftIndex, right = rightIndex;
		int key = arr[right];
		while (left < right) {
			while (left < right && arr[left] <= key) {
				left++;
			}
			arr[right] = arr[left];

			while (right > left && arr[right] >= key) {
				right--;
			}
			arr[left] = arr[right];
		}
		arr[right] = key;

		quickSort(arr, leftIndex, left - 1);
		quickSort(arr, right + 1, rightIndex);

	}
}

 

标签:arr,right,Java,int,Demo,System,排序,preIndex,out
来源: https://blog.csdn.net/zadaya/article/details/110431224