java实现内部排序算法

时间:2021-10-07 10:58:14
  • 冒泡排序

 

public class BubbleSort{

	public static int[] asc(int[] a){
		int item;
		for (int i = 0; i < a.length-1; i++) {
			item = a[i];
			for (int j = i+1; j < a.length; j++) {
				if (a[j]<a[i]) {
					a[i] = a[j];
					a[j] = item;
					item = a[i];
				}
			}
		}
		return a;
	}
	

	public static int[] desc(int[] a){
		int item;
		for (int i = 0; i < a.length-1; i++) {
			item = a[i];
			for (int j = i+1; j < a.length; j++) {
				if (a[j]>a[i]) {
					a[i] = a[j];
					a[j] = item;
					item = a[i];
				}
			}
		}
		return a;
	}
	
	public static void main(String[] args) {
		int a[] = {23,14,25,32,8,27,15,13,58,8,2,36};
		a = asc(a);
		pump(a);
	}

	private static void pump(int[] a) {
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i]+"\t");
		}
		System.out.println("\n");
	}
}

 

 

  • 堆排序

 

public class HeapSort{

	public static int[] asc(int[] a){
		a = buildHeap(a);
		int item;
		for (int i = a.length-1; i >=1; i--) {
			item = a[0];
			a[0] = a[i];
			a[i] = item;
			heapAdjust(a,0,i-1);
		}
		return a;
	}
	
	private static int[] buildHeap(int[] a) {
		return buildHeap(a,true);
	}
	
	private static int[] buildHeap(int[] a,boolean asc) {
		for (int i = a.length/2; i >= 0; i--) {
			a = heapAdjust(a,i,a.length-1,asc);
		}
		return a;
	}

	public static int[] heapAdjust(int[] a,int index,int size){
		return heapAdjust(a, index, size, true);
	}
	
	public static int[] heapAdjust(int[] a,int index,int size,boolean asc){
		int left = index*2 + 1;
		int right = left + 1;
		int max = index;
		if (left<=size) {
			if (asc&&a[left]>a[max]) {
				max = left;
			}
			if (!asc&&a[left]<a[max]) {
				max = left;
			}
		}
		if (right<=size) {
			if (asc&&a[right]>a[max]) {
				max = right;
			}
			if (!asc&&a[right]<a[max]) {
				max = right;
			}
		}
		if (max != index) {
			int temp = a[max];
			a[max] = a[index];
			a[index] = temp;
			heapAdjust(a, max, size, asc);
		}
		return a;
	}
	

	public static int[] desc(int[] a){
		a = buildHeap(a,false);
		int item;
		for (int i = a.length-1; i >=1; i--) {
			item = a[0];
			a[0] = a[i];
			a[i] = item;
			heapAdjust(a,0,i-1,false);
			pump(a);
		}
		return a;
	}
	
	public static void main(String[] args) {
		int a[] = {23,14,25,32,8,27,15,13,58,8,2,36};
		a = desc(a);
		pump(a);
	}

	private static void pump(int[] a) {
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i]+"\t");
		}
		System.out.println("\n");
	}
}

 

  • 插入排序

 

public class InsertSort{

	public static int[] asc(int[] a){
		int item;
		for (int i = 0; i < a.length-1; i++) {
			item = a[i+1];
			for (int k = i; k >= 0; k--) {
				if (a[k]>item) {
					a[k+1] = a[k];
					a[k] = item;
				}else {
					break ;
				}
			}
		}
		return a;
	}
	
	public static int[] desc(int[] a){
		int item;
		for (int i = 0; i < a.length-1; i++) {
			item = a[i+1];
			for (int k = i; k >= 0; k--) {
				if (a[k]<item) {
					a[k+1] = a[k];
					a[k] = item;
				}else {
					break ;
				}
			}
		}
		return a;
	}
	
	public static void main(String[] args) {
		int a[] = {23,14,25,32,18,27,15,13,58,8,2,36};
		a = desc(a);
		for (int i = 0; i < a.length; i++) {
			System.out.println(a[i]);
		}
	}
}

 

  • 归并排序

 

public class MergeSort{

	public static int[] asc(int[] a){
		return asc(a,0,a.length-1);
	}
	
	public static int[] asc(int[] a,int start,int end){
		if (start<end) {
			if (start+1==end) {
				if(a[start]>a[end]){
					int temp = a[start];
					a[start] = a[end];
					a[end] = temp;
				}
				return a;
			}
			int m = (start+end)/2;
			a = asc(a,start,m);
			a = asc(a,m+1,end);
			a = merge(a,start,m,end);
		}
		return a;
	}
	

	private static int[] merge(int[] a, int start, int m, int end) {
		for (int j = m+1; j <= end; j++) {
			int index = j,i=j-1,item = a[j];
			while (i >= start&&item<a[i]) {
				index = i--;
			}
			if (index != j) {
				for (int k = j; k >index; k--) {
					a[k] = a[k-1];
				}
				a[index] = item;
			}
		}
		return a;
	}
	
	private static int[] merge(int[] a, int start, int m, int end,boolean asc) {
		for (int j = m+1; j <= end; j++) {
			int index = j,i=j-1,item = a[j];
			if (asc) {
				while (i >= start&&item<a[i]) {
					index = i--;
				}
			}else {
				while (i >= start&&item>a[i]) {
					index = i--;
				}
			}
			if (index != j) {
				for (int k = j; k >index; k--) {
					a[k] = a[k-1];
				}
				a[index] = item;
			}
		}
		return a;
	}
	

	public static int[] desc(int[] a){
		return desc(a,0,a.length-1);
	}
	
	private static int[] desc(int[] a,int start,int end) {
		if (start<end) {
			if (start+1==end) {
				if(a[start]<a[end]){
					int temp = a[start];
					a[start] = a[end];
					a[end] = temp;
				}
				return a;
			}
			int m = (start+end)/2;
			a = desc(a,start,m);
			a = desc(a,m+1,end);
			a = merge(a,start,m,end,false);
		}
		return a;
	}

	public static void main(String[] args) {
		int a[] = {23,14,25,32,8,27,15,13,58,8,2,36};
		a = desc(a);
		pump(a);
	}

	private static void pump(int[] a) {
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i]+"\t");
		}
		System.out.println("\n");
	}
}

 

 

  • 快速排序

 

public class QuickSort {

	public static int[] asc(int[] a, int low, int high) {
		if (low >= high || (a[low] <= a[high] && low + 1 == high)) {
			return a;
		} else if (a[low] > a[high] && low + 1 == high) {
			int temp = a[high];
			a[high] = a[low];
			a[low] = temp;
			return a;
		}
		int tag = a[low], start = low, end = high;
		boolean left_right = false;
		while (low < high) {
			if (!left_right) {
				if (a[high] < tag) {
					a[low] = a[high];
					a[high] = tag;
					low++;
					left_right = true;
				} else {
					high--;
				}
			} else {
				if (a[low] > tag) {
					a[high] = a[low];
					a[low] = tag;
					left_right = false;
					high--;
				} else {
					low++;
				}
			}
		}
		if (start < high) {
			a = asc(a, start, high == low ? high - 1 : high);
		}
		if (low < end) {
			a = asc(a, low == high ? low + 1 : low, end);
		}
		return a;
	}

	public static int[] desc(int[] a, int low, int high) {
		if (low >= high || (a[low] >= a[high] && low + 1 == high)) {
			return a;
		} else if (a[low] < a[high] && low + 1 == high) {
			int temp = a[high];
			a[high] = a[low];
			a[low] = temp;
			return a;
		}
		int tag = a[low], start = low, end = high;
		boolean left_right = false;
		while (low < high) {
			if (!left_right) {
				if (a[high] > tag) {
					a[low] = a[high];
					a[high] = tag;
					low++;
					left_right = true;
				} else {
					high--;
				}
			} else {
				if (a[low] < tag) {
					a[high] = a[low];
					a[low] = tag;
					left_right = false;
					high--;
				} else {
					low++;
				}
			}
		}
		if (start < high) {
			a = desc(a, start, high == low ? high - 1 : high);
		}
		if (low < end) {
			a = desc(a, low == high ? low + 1 : low, end);
		}
		return a;
	}

	public static void main(String[] args) {
		int a[] = { 23, 14, 25, 32, 8, 27, 15, 13, 58, 8, 2, 36 };
		a = desc(a, 0, a.length - 1);
		pump(a);
	}

	private static void pump(int[] a) {
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + "\t");
		}
		System.out.println("\n");
	}
}

 

 

  • 基数排序

 

public class RadixSort {

	private static int[] radix(int[] a, int radix, boolean asc) {
		int[][] index = new int[10][];
		int pow = Double.valueOf(Math.pow(10, radix)).intValue(), bucket = 0;
		for (int i = 0; i < a.length; i++) {
			bucket = a[i] % pow / (pow / 10);
			if (index[bucket] == null) {
				index[bucket] = new int[] { a[i] };
			} else {
				index[bucket] = add(index[bucket], a[i]);

			}
		}
		int k = 0;
		int[] b = new int[a.length];
		if (asc) {
			for (int i = 0; i < 10; i++) {
				if (index[i] != null && index[i].length > 0) {
					System.arraycopy(index[i], 0, b, k, index[i].length);
					k += index[i].length;
				}
			}
		} else {
			for (int i = 9; i >= 0; i--) {
				if (index[i] != null && index[i].length > 0) {
					System.arraycopy(index[i], 0, b, k, index[i].length);
					k += index[i].length;
				}
			}
		}
		return b;
	}

	private static int[] add(int[] a, int v) {
		if (a == null || a.length == 0) {
			return new int[] { v };
		} else {
			int[] b = new int[a.length + 1];
			System.arraycopy(a, 0, b, 0, a.length);
			b[a.length] = v;
			return b;
		}
	}

	public static int[] asc(int[] a) {
		int bucket = 1, max = 0;
		for (int i = 1; i < a.length; i++) {
			if (a[i] > a[max]) {
				max = i;
			}
		}
		bucket = String.valueOf(a[max]).length();
		for (int i = 1; i <= bucket; i++) {
			a = radix(a, i, true);
		}
		return a;
	}

	public static int[] desc(int[] a) {
		int bucket = 1, max = 0;
		for (int i = 1; i < a.length; i++) {
			if (a[i] > a[max]) {
				max = i;
			}
		}
		bucket = String.valueOf(a[max]).length();
		for (int i = 1; i <= bucket; i++) {
			a = radix(a, i, false);
		}
		return a;
	}

	public static void main(String[] args) {
		int a[] = { 23, 14, 25, 32, 8, 27, 15, 13, 58, 8, 2, 36 };
		a = desc(a);
		pump(a);
	}

	private static void pump(int[] a) {
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + "\t");
		}
		System.out.println("\n");
	}
}

 

 

  • 选择排序

 

public class SelectSort{

	public static int[] asc(int[] a){
		int index;
		for (int i = 0; i < a.length-1; i++) {
			index = i;
			for (int j = i+1; j < a.length; j++) {
				if (a[j]<a[index]) {
					index = j;
				}
			}
			if (a[i]>a[index]) {
				int item = a[i];
				a[i] = a[index];
				a[index] = item;
			}
		}
		return a;
	}
	

	public static int[] desc(int[] a){
		int index;
		for (int i = 0; i < a.length-1; i++) {
			index = i;
			for (int j = i+1; j < a.length; j++) {
				if (a[j]>a[index]) {
					index = j;
				}
			}
			if (a[i]<a[index]) {
				int item = a[i];
				a[i] = a[index];
				a[index] = item;
			}
		}
		return a;
	}
	
	public static void main(String[] args) {
		int a[] = {23,14,25,32,8,27,15,13,58,8,2,36};
		a = desc(a);
		pump(a);
	}

	private static void pump(int[] a) {
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i]+"\t");
		}
		System.out.println("\n");
	}
}

 


  • 希尔排序

 

public class ShellSort {

	public static int[] asc(int[] a) {
		int d = (a.length + 1) / 2;
		while (d >= 1) {
			shell(a, d, true);
			d = d / 2;
		}
		return a;
	}

	private static void shell(int[] a, int d, boolean asc) {
		int item;
		if (asc) {
			for (int i = 0; i < d; i++) {
				for (int j = i; j + d < a.length; j += d) {
					item = a[j + d];
					for (int k = j; k >= 0; k -= d) {
						if (a[k] > item) {
							a[k + d] = a[k];
							a[k] = item;
						} else {
							break;
						}
					}
				}
			}
		} else {
			for (int i = 0; i < d; i++) {
				for (int j = i; j + d < a.length; j += d) {
					item = a[j + d];
					for (int k = j; k >= 0; k -= d) {
						if (a[k] < item) {
							a[k + d] = a[k];
							a[k] = item;
						} else {
							break;
						}
					}
				}
			}
		}
	}

	public static int[] desc(int[] a) {
		int d = (a.length + 1) / 2;
		while (d >= 1) {
			shell(a, d, false);
			d = d / 2;
		}
		return a;
	}

	public static void main(String[] args) {
		int a[] = { 23, 14, 25, 32, 18, 27, 15, 13, 58, 8, 2, 36 };
		a = desc(a);
		pump(a);
	}

	private static void pump(int[] a) {
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + "\t");
		}
		System.out.println("\n");
	}
}