当前位置:文档之家› JAVA堆排序和几种排序方法实现代码

JAVA堆排序和几种排序方法实现代码


* @param end
*
结束位置
*/
private static void quickDeal(int[] array, int begin, int end,int method) {
if (end > begin) {
int pos = begin + (int) (Math.random() * (end - begin + 1)); // 计算分隔位置
public static int[] quickOrder(int[] array, int method) {
quickDeal(array,0,array.length - 1,method); return array;
}
/**
*
* @param array
* @param begin
*
开始位置
for (int i = 0; i < orderarray.length; i++) { System.out.println(orderarray[i]);
} }
/**
* 取随机数据,初始化一个数组
*

* @param min
*
随机数的最小值
* @param max
*
最大值
* @param size
位置的值时移动 array[j + 1] = tmp; //插入排序值
} } else if (method == 2) {
if (array[i - 1] < array[i]) { int tmp = array[i]; int j = i - 1; do { array[j + 1] = array[j]; j--; } while (j >= 0 && tmp > array[j]); array[j + 1] = tmp;
for (int i = 1; i < array.length; i++) { if (method == 1) { if (array[i - 1] > array[i]) { int tmp = array[i]; // int j = i - 1; do { array[j + 1] = array[j]; j--; } while (j >= 0 && tmp < array[j]); //当 j>=0 并且 当前值大于数据中 j
}
}
/** * 插入排序方法 * 排序原理:抽出一个数,做为排序基序列,然后依次抽出其它数与,与此序列中的
数进行比较,放入合适的位置
* @param array * @param method * @return */ public static int[] insertOrder(int[] array, int method) {
}
/** * 交换值,交换数组的两个值 * @param array * @param i * @param j */
private static void swap(int[] array,int i, int j) {
int tmp = array[i]; array[i] = array[j]; array[j] = tmp; }
} } return array; }
/** * 双向冒泡排序 * 原理:类似于冒泡排序,只不过是双向的 * @param array * @param method * @return */
public static int[] doubleBubbleOrder(int[] array,int method) {
各种排序方法的实现
import ng.Math; import java.util.Random;
/** * 排序 * * @author javajack */
public class OrderTest {
public static void main(String args[]) { OrderTest.ExecOrder(2);
while(j <= n-1) {
if(j < n-1 && a[j] < a[j+1])
j++;
if(temp >= a[j]) break;
a[(j-1)/2] = a[j];
j = 2 * j + 1; }
a[(j-1)/2] = temp; }
public static void Display(int[] a, String str) {
/**
*
* @param method
*
1 为升序,2 为降序
*/
public static void ExecOrder(int method) {
int[] array = null;
array = initArray(10, 210, 10);
//int[] orderarray = bubbleOrder(array,method); int[] orderarray = doubleBubbleOrder(array,method); //int[] orderarray = insertOrder(array, method); //int [] orderarray = quickOrder(array,method); //int[] orderarray = selectOrder(array, method);
int n = a.length; int temp = 0;
Display(a, "Before sort : ");
for(int i=n/2; i>0; i--) Adjust(a, i-1, n);
for(int i=n-2; i>=0; i--) {
temp = a[i+1]; a[i+1] = a[0]; a[0] = temp;
也就是向前移动
swap(array,pos,i);
pos++; //移动后 pos 增 1
}
}
}
swap(array,pos,end); //end 位置的值前移
quickDeal(array,begin,pos -1,method);
quickDeal(array,pos+1,end,method);
} return init; }
/** * 交换排序方法 * 原理:依次交换值 * @param array * @return */
public static int[] convertOrder(int[] array, int method) { for (int i = 0; i < array.length; i++) { for (int j = i + 1; j < array.length; j++) { if (method==2) { if (array[i] < array[j]) swap(array,i,j); }else if (method == 1) { if (array[i] > array[j]) swap(array,i,j);
System.out.println(str);
for(int i=0; i<a.length; i++) System.out.print(a[i] + " ");
System.out.println(); } }
输出结果为:
Before sort : 26 5 77 1 61 11 59 15 48 19 After sort : 1 5 11 15 19 26 48 59 61 77
int left = 0; int right = array.length -1 ; while (left < right) {
for(int i=left;i<=right;i++)
{ if (method==1) { if (array[left] > array[i]) swap(array,left,i); }else { if (array[left] < array[i]) swap(array,left,i); }
for(int i=0;i<array.length;i++) {
for (int j=array.length -1 ;j>i;j--) {
if (method==2) {
if (array[i] < array[j]) swap(array,i,j);
}else if (method==1) if (array[i] > array[j]) swap(array,i,j);
} } left++; right--; } return array; }
/** * 快速排序方法,运用到递归 * 排序原理:随机找到一个值,然后以此值大小进行分为两个数组,大的放左边,小
的放右边, * 然后再对左右两侧的数据依次排序根据 * @param array * @param method * @return */
堆排序(1)
public class HeapSort {
public static void main(String[] args) {
int[] a = {26, 5, 77, 1, 61, 11, 59, 15, 48, 19};
Sort(a); }
相关主题