- package com.homework.arraySort;
- /**
- * XXX项目
- * <p>
- * com.homework.arraySort
- * <p>
- * File: ArraySorted.java 创建时间:2012-12-16下午09:53:37
- * </p>
- * <p>
- * Title: 标题(要求能简洁地表达出类的功能和职责)
- * </p>
- * <p>
- * Description: 描述(简要描述类的职责、实现方式、使用注意事项等)
- * </p>
- * <p>
- * Copyright: Copyright (c) 2012 XX股份有限公司
- * </p>
- * <p>
- * Company: XX
- * </p>
- * <p>
- * 模块: java编码规范-数组排序
- * </p>
- *
- * @author XX
- * @version 1.0
- * @history 修订历史(历次修订内容、修订人、修订时间等)
- */
- public class ArraySorted {
- /**
- *@name 数组排序演示
- *@Description 本示例包含有插入排序、选择排序以及冒泡排序算法
- *@Time 创建时间:2012-12-16下午08:55:54
- * @author XX
- *@param array原始整型数组
- * @history 修订历史(历次修订内容、修订人、修订时间等)
- */
- public static void main(String[] args) {
- /**
- * 初始化一个原始的整型数组
- */
- int array[] = { 12, 34, 3, 56, 9, 23, 45, 19, 13, 10 };
- // 创建一个用于排序的类对象
- ArraySorted arraySorted = new ArraySorted();
- /**
- * 排序方法演示开始:
- * 调用对象的排序方法,并将排序之后的结果显示打印出来
- */
- // 1. 采用插入排序法
- System.out.print("插入排序的结果:");
- arraySorted.printArray(arraySorted.insertedSort(array));
- System.out.println();
- // 2. 采用选择排序法
- System.out.print("选择排序的结果:");
- arraySorted.printArray(arraySorted.selectedSort(array));
- System.out.println();
- // 3. 采用冒泡排序法
- System.out.print("冒泡排序的结果:");
- arraySorted.printArray(arraySorted.bubbledSort(array));
- /**
- * 排序方向演示结束
- */
- }
- /**
- *@name 插入排序算法
- *@Description 这是一个插入排序的方法,将待排序数组传入,排序后并返回
- *@Time 创建时间:2012-12-16下午09:42:35
- * @author XX
- *@param array待排序数组
- *@return 按算法排好序,返回
- * @history 修订历史(历次修订内容、修订人、修订时间等)
- */
- public int[] insertedSort(int array[]) {
- // 定义一个中间变量,临时存放变量值
- int temp = 0;
- for (int i = 1; i < array.length; i++) { // 要排序的那个数
- for (int j = 0; j < i; j++) { // 依次和前边排好数对比
- if (array[i] < array[j]) { // 当这个数a[i]小于前边的a[j]时
- temp = array[i];
- for (; i > j; i--) { // 开始插入数据,后边的数据依次往后边移,直到第i个数
- array[i] = array[i - 1];
- }
- // 将临时存放的变量放回下标为j的位置
- array[j] = temp;
- }
- }
- }
- // 将排序好的数组返回
- return array;
- }
- /**
- *@name 选择排序算法
- *@Description 这是一个选择排序的方法,将待排序数组传入,排序后并返回
- *@Time 创建时间:2012-12-16下午09:46:51
- * @author XX
- *@param array待排序数组
- *@return 按算法排好序,返回
- * @history 修订历史(历次修订内容、修订人、修订时间等)
- */
- public int[] selectedSort(int array[]) {
- int temp = 0;
- for (int i = array.length - 1; i > 0; i--) {// 从最后一个开始循环替换成余下数的最大的数
- int maxIndex = 0; // 每次初始化最大数的坐标为0
- for (int j = 0; j <= i; j++) { // 循环找出最大数的下标,并赋值给max
- if (array[maxIndex] < array[j])
- maxIndex = j;
- }
- temp = array[i]; // 再把最大值和最后一个数交换
- array[i] = array[maxIndex];
- array[maxIndex] = temp;
- }
- return array;
- }
- /**
- *@name 冒泡排序算法
- *@Description 这是一个冒泡排序的方法,将待排序数组传入,排序后并返回
- *@Time 创建时间:2012-12-16下午09:49:53
- * @author XX
- *@param array待排序数组
- *@return 按算法排好序,返回
- * @history 修订历史(历次修订内容、修订人、修订时间等)
- */
- public int[] bubbledSort(int array[]) {
- for (int i = array.length - 1; i > 0; i--) { // 从最后一个开始循环
- for (int j = 0; j > i; j++) {// 从第一个数据开始冒泡排序,
- if (array[j] > array[j + 1]) { // 如果左边的数大于相邻右边的,则交换位置
- int temp = array[j + 1];
- array[j + 1] = array[j];
- array[j] = temp;
- }
- }
- }
- return array;
- }
- /**
- *@name 打印数组
- *@Description 这是一个用于打印数组元素的方法
- *@Time 创建时间:2012-12-16下午09:50:48
- * @author XX
- *@param array
- * 待打印数组
- * @history 修订历史(历次修订内容、修订人、修订时间等)
- */
- public void printArray(int array[]) {
- for (int i = 0; i < array.length; i++) { // 打印排序后的数组
- System.out.print(array[i]);
- if (i != array.length - 1)
- System.out.print(", ");
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/210320149103.html
来源: http://www.codesnippet.cn/detail/210320149103.html