各类排序算法java的实现.doc
文本预览下载声明
各类排序算法java的实现
插入排序:package org.rut.util.algorithm.support;import org.rut.util.algorithm.SortUtil;/*** @author treeroot* @since 2006-2-2* @version 1.0*/public class InsertSort implements SortUtil.Sort{? /* (non-Javadoc)? * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])? */? public void sort(int[] data) {? ? int temp;? ? for(int i=1;idata.length;i++){? ? ? ? for(int j=i;(j0)(data[j]data[j-1]);j--){? ? ? ? ? SortUtil.swap(data,j,j-1);? ? ? ? }? ? } ? ? ? }}
冒泡排序:package org.rut.util.algorithm.support;import org.rut.util.algorithm.SortUtil;/*** @author treeroot* @since 2006-2-2* @version 1.0*/public class BubbleSort implements SortUtil.Sort{? /* (non-Javadoc)? * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])? */? public void sort(int[] data) {? ? int temp;? ? for(int i=0;idata.length;i++){? ? ? ? for(int j=data.length-1;ji;j--){? ? ? ? ? if(data[j]data[j-1]){? ? ? ? ? ? SortUtil.swap(data,j,j-1);? ? ? ? ? }? ? ? ? }? ? }? }}选择排序:package org.rut.util.algorithm.support;import org.rut.util.algorithm.SortUtil;/*** @author treeroot* @since 2006-2-2* @version 1.0*/public class SelectionSort implements SortUtil.Sort {? /*? * (non-Javadoc)? * ? * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])? */? public void sort(int[] data) {? ? int temp;? ? for (int i = 0; i data.length; i++) {? ? ? ? int lowIndex = i;? ? ? ? for (int j = data.length - 1; j i; j--) {? ? ? ? ? if (data[j] data[lowIndex]) {? ? ? ? ? ? lowIndex = j;? ? ? ? ? }? ? ? ? }? ? ? ? SortUtil.swap(data,i,lowIndex);? ? }? }}Shell排序:package org.rut.util.algorithm.support;import org.rut.util.algorithm.SortUtil;/*** @author treeroot* @since 2006-2-2* @version 1.0*/public class ShellSort implements SortUtil.Sort{? /* (non-Javadoc)? * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])? */? public void sort(int[] data) {? ? for(int i=data.length/2;i2;i/=2){? ? ? ? for(int j=0;ji;j++){? ? ? ? ? in
显示全部