以下是我用java实现各种常见的排序算法
package com.husiwang.Sort;
/**
* Created by SiwangHu on 2015/2/6.
*/
public class Sort {
private int[] Data; //数据缓存
private Visit Visited; //回调函数
public Sort(){
Data=null;
Visited=new Visit();
}
public Sort(int[] data){
Data=new int[data.length];
for (int i = 0; i <data.length ; i++) {
Data[i]=data[i];
}
Visited=new Visit();
}
public void InitData(int[] data){
Data=new int[data.length];
for (int i = 0; i <data.length ; i++) {
Data[i]=data[i];
}
}
public void InsertSort(){
if(Data!=null){
for(int i=0;i<Data.length;i++){
int temp=Data[i];
for(int j=i;j>-1;j--){
if(temp<Data[j]){
Data[j+1]=Data[j];
Data[j]=temp;
}
}
}
}
}
public void SelectSort(){
if(Data!=null){
for(int i=0;i<Data.length;i++){
for(int j=i;j<Data.length;j++){
if(Data[i]>Data[j]){
int temp=Data[j];
Data[j]=Data[i];
Data[i]=temp;
}
}
}
}
}
public void BubbleSort(){
if(Data!=null){
for(int i=0;i<Data.length;i++){
for(int j=1;j<Data.length-i;j++){
if(Data[j-1]>Data[j]){
int temp=Data[j-1];
Data[j-1]=Data[j];
Data[j]=temp;
}
}
}
}
}
public void ShellSort(int[] dk){
for(int i=0;i<dk.length;i++){
shellSort(dk[i]);
}
}
private void shellSort(int dk){
if(Data!=null){
for(int i=0;i<Data.length;i+=dk){
int temp=Data[i];
for(int j=i;j>-1;j-=dk){
if(temp<Data[j]){
Data[j+dk]=Data[j];
Data[j]=temp;
}
}
}
}
}
public void QuickSort(){
quickSort(0, Data.length-1);
}
private void quickSort(int begin,int end){
if(begin<end) {
int mid = Handle(begin, end);
quickSort(0, mid-1);
quickSort(mid + 1, end);
}
}
private int Handle(int begin,int end) {
int Begin = begin, End = end, key = Data[begin], temp;
while (Begin < End) {
while (Begin < End && Data[End] >= key) {
End--;
}
temp = Data[Begin];
Data[Begin] = Data[End];
Data[End] = temp;
while (Begin < End && Data[Begin] <= key) {
Begin++;
}
temp = Data[Begin];
Data[Begin] = Data[End];
Data[End] = temp;
}
return Begin;
}
public void MergeSort(){
mergeSort(0,Data.length-1);
}
private void mergeSort(int begin,int end){
if(begin<end){
int mid=(begin+end)/2;
mergeSort(begin,mid);
mergeSort(mid+1,end);
Merge(begin,mid,end);
}
}
private void Merge(int begin,int mid,int end){
int[] Temp=new int[Data.length];
int index=begin,center=mid+1,temp=begin;
while(begin<=mid&¢er<=end){
if(Data[begin]<Data[center]){
Temp[index]=Data[begin];
begin++;
index++;
}
else{
Temp[index]=Data[center];
center++;
index++;
}
}
while(begin<=mid){
Temp[index]=Data[begin];
begin++;
index++;
}
while (center<=end){
Temp[index]=Data[center];
center++;
index++;
}
while(temp<=end){
Data[temp]=Temp[temp];
temp++;
}
}
public void HeapSort(){
int swap;
for(int i=Data.length-1;i>0;i--){
HeapAdjust(0, i);
swap=Data[i];
Data[i]=Data[0];
Data[0]=swap;
}
}
private void HeapAdjust(int begin,int end){
int Begin=(end-1)/2;
for(int i=Begin;i>begin-1;i--){
int temp=i,swap;
if(Data[temp]<Data[2*i+1]&&2*i+1<=end)
temp=2*i+1;
if(Data[temp]<Data[2*i+2]&&2*i+2<=end)
temp=2*i+2;
swap=Data[i];
Data[i]=Data[temp];
Data[temp]=swap;
}
}
public int[] getData() {
return Data;
}
public void Traverse(){
Traverse(Visited);
}
public void Traverse(Visit visited){
for(int i=0;i<Data.length;i++){
visited.visited(Data[i]);
}
}
@Override
public String toString() {
String temp="";
for (int i = 0; i <Data.length ; i++) {
temp+=Data[i]+" ";
}
return "Sort{" +
"Data=" + temp +
'}';
}
}
package com.husiwang.Sort;
/**
* Created by SiwangHu on 2015/2/6.
*/
public class Visit {
public void visited(int data){
System.out.println(data);
}
}
转载于:https://blog.51cto.com/siwanghu/1685558