各種排序演算法java實現

2021-04-12 19:57:26 字數 4329 閱讀 6302

插入排序:

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

氣泡排序:

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

選擇排序:

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

}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

快速排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.sortutil;

/*** @author treeroot

* @since 2006-2-2

* @version 1.0

*/public class quicksort implements sortutil.sort

private void quicksort(int data,int i,int j)

/*** @param data

* @param i

* @param j

* @return

*/private int partition(int data, int l, int r,int pivot)

while(l }

改進後的快速排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.sortutil;

/*** @author treeroot

* @since 2006-2-2

* @version 1.0

*/public class improvedquicksort implements sortutil.sort

while(lthreshold)

if((j-l)>threshold)

}//new insertsort().sort(data);

insertsort(data);

}/**

* @param data

*/private void insertsort(int data)

歸併排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.sortutil;

/*** @author treeroot

* @since 2006-2-2

* @version 1.0

*/public class mergesort implements sortutil.sort

private void mergesort(int data,int temp,int l,int r)

int i1=l;

int i2=mid+1;

for(int cur=l;cur<=r;cur++)

改進後的歸併排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.sortutil;

/*** @author treeroot

* @since 2006-2-2

* @version 1.0

*/public class improvedmergesort implements sortutil.sort

private void mergesort(int data, int temp, int l, int r)

for (j = 1; j <= r - mid; j++)

int a = temp[l];

int b = temp[r];

for (i = l, j = r, k = l; k <= r; k++) else }}

/*** @param data

* @param l

* @param i

*/private void insertsort(int data, int start, int len)

堆排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.sortutil;

/*** @author treeroot

* @since 2006-2-2

* @version 1.0

*/public class heapsort implements sortutil.sort

public void remove()

//fixdown

private void fixdown(int k)

}private void fixup(int k) }

}

}

sortutil:

package org.rut.util.algorithm;

import org.rut.util.algorithm.support.bubblesort;

import org.rut.util.algorithm.support.heapsort;

import org.rut.util.algorithm.support.improvedmergesort;

import org.rut.util.algorithm.support.improvedquicksort;

import org.rut.util.algorithm.support.insertsort;

import org.rut.util.algorithm.support.mergesort;

import org.rut.util.algorithm.support.quicksort;

import org.rut.util.algorithm.support.selectionsort;

import org.rut.util.algorithm.support.shellsort;

/*** @author treeroot

* @since 2006-2-2

* @version 1.0

*/public class sortutil

private static string name=;

private static sort impl=new sort;

public static string tostring(int algorithm)

public static void sort(int data, int algorithm)

public static inte***ce sort

public static void swap(int data, int i, int j) }

各種排序演算法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 ...

各種排序演算法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 ...

各種排序演算法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 ...