[数据结构与算法]十大排序算法的Java实现

十大排序算法的Java实现

回头补一补细节和思想

import java.util.*;

public class SortAlgorithm {
	public static void main(String[] args) {
		int[] a = new int[]{9,3,14,-1,-8,1,0,21,-4,-9,12,9};
		RadixSort.sort(a);
		System.out.println(Arrays.toString(a));
	}
}

class BubbleSort {
	static void sort(int[] nums) {
		int left = -1;
		int right = nums.length;
		int tag;
		int tmp;
		while (left < right) {
			tag = -1;
			for (int i = left+2; i < right; i++) {
				if (nums[i] < nums[i-1]) {
					tmp = nums[i];
					nums[i] = nums[i-1];
					nums[i-1] = tmp;
					tag = i;
				}
			}
			if (tag == -1) {
				return;
			} else {
				right = tag;
			}
			tag = nums.length;
			for (int i = right-2; i > left; i--) {
				if (nums[i] > nums[i+1]) {
					tmp = nums[i];
					nums[i] = nums[i+1];
					nums[i+1] = tmp;
					tag = i;
				}
			}
			if (tag == nums.length) {
				return;
			} else {
				left = tag;
			}
		}
	}
}

class QuickSort {
	static void sort(int[] nums) {
		sort(nums, 0, nums.length-1);
	}

	static void sort(int[] nums, int startIndex, int endIndex) {
		if (startIndex >= endIndex) {
			return;
		}
		int pivot = Partition2(nums, startIndex, endIndex);
		sort(nums, startIndex, pivot-1);
		sort(nums, pivot+1, endIndex);
	}

	private static  int Partition(int[] nums, int left, int right) {
		int startIndex = left;
		int pivot = nums[left];
		int tmp;
		left++;
		while (left < right) {
			while (left < right && nums[right] > pivot) {
				right--;
			}
			while (left < right && nums[left] <= pivot) {
				left++;
			}
			tmp = nums[left];
			nums[left] = nums[right];
			nums[right] = tmp;
		}
		tmp = nums[startIndex];
		nums[startIndex] = nums[left];
		nums[left] = tmp;
		return left;
	}

	private static  int Partition2(int[] nums, int left, int right) {
		int startIndex = left;
		int pivot = nums[left];
		int index = right;
		int tmp;
		while (left <= index) {
			if (nums[left] > pivot) {
				tmp = nums[left];
				nums[left] = nums[index];
				nums[index] = tmp;
				index--;
				continue;
			} else {
				left++;
			}
		}
		nums[startIndex] = nums[index];
		nums[index] = pivot;
		return index;
	}

}

class StraigntSelectSort {
	static void sort(int[] nums) {
		int minIndex;
		int tmp;
		for (int i=0; i<nums.length-1; i++) {
			minIndex = i;
			for (int j=i; j<nums.length; j++) {
				if (nums[j] < nums[minIndex]) {
					minIndex = j;
				}
			}
			tmp = nums[i];
			nums[i] = nums[minIndex];
			nums[minIndex] = tmp;
		}
	}
}

class HeapSort {
	public static void sort(int[] nums) {
		buildHeap(nums);
		for (int i=0; i<nums.length-1; i++) {
			swap(nums, 0, nums.length-1-i);
			adjustHeap(nums, 0, nums.length-2-i);
		}
	}

	private static void buildHeap(int[] nums) {
		for (int i=(nums.length-2)/2; i>=0; i--) {
			adjustHeap(nums, i, nums.length-1);
		}
	}

	private static void adjustHeap(int[] nums, int i, int size) {
		if (2*i+1 > size) {
			return;
		}
		int maxIndex = i;
		if (2*i+1 <= size && nums[maxIndex] < nums[2*i+1]) {
			maxIndex = 2*i+1;
		}
		if (2*(i+1) <= size && nums[maxIndex] < nums[2*(i+1)]) {
			maxIndex = 2*(i+1);
		}
		if (maxIndex != i) {
			swap(nums, i, maxIndex);
			adjustHeap(nums, maxIndex, size);
		}
	}

	private static void swap(int[] nums, int i, int j) {
		int tmp = nums[i];
		nums[i] = nums[j];
		nums[j] = tmp;
	}
}

class StraightInsertionSort {
	static void sort(int[] nums) {
		int tmp;
		for (int i=0; i<nums.length; i++) {
			tmp = nums[i];
			for (int j=i-1; j>=0; j--) {
				if (nums[j] > tmp) {
					nums[j+1] = nums[j];
				} else {
					nums[j+1] = tmp;
					break;
				}
				if (j==0) {
					nums[j] = tmp;
				}
			}
		}
	}
}

class ShellSort {
	public static void sort(int[] nums){
		int k = nums.length/2;
		while (k >= 1) {
			for (int i=k; i<nums.length; i++) {
				int tmp=nums[i];
				int j=i-k;
				while (j>=0 && nums[j] > tmp) {
					nums[j+k] = nums[j];
					j-=k;
				}
				nums[j+k] = tmp;
			}
			k/=2;
		}
	}
}

class MergeSort {
		public static void sort(int[] nums) {
			int[] res = mergeSortRecursion(nums, 0, nums.length-1);
			for (int i=0; i<nums.length; i++) {
				nums[i] = res[i];
			}
		}

		public static void sort2(int[] nums) {
			int k=1;
			int right;
			int mid;
			int size;
			int leftEndIndex;
			int rightEndIndex;
			while (k < nums.length) {
				for (int left=0; left<nums.length; left+=2*k) {
					mid=0;
					right=left+k;
					leftEndIndex = right;
					size=left+2*k-1>=nums.length?nums.length-left:2*k;
					rightEndIndex = left+size;
					int[] tmp = new int[size];
					while (left < leftEndIndex && right < rightEndIndex) {
						if (nums[left] <= nums[right]) {
							tmp[mid]=nums[left];
							mid++;
							left++;
						} else {
							tmp[mid]=nums[right];
							mid++;
							right++;
						}
					}
					while (left < leftEndIndex) {
						tmp[mid]=nums[left];
						left++;
						mid++;
					}
					while (right < rightEndIndex) {
						tmp[mid]=nums[right];
						right++;
						mid++;
					}
					for (int i=leftEndIndex-k; i<rightEndIndex; i++) {
						nums[i] = tmp[i-left+k];
					}
					left = leftEndIndex-k;
				}
				k=2*k;
			}
		}

		public static int[] mergeSortRecursion(int[] nums, int left,
		                                       int right) {
			if (right == left) {
				int[] res = {nums[left]};


				return res;
			}
			int[] leftNums = mergeSortRecursion(nums, left, (right+left)/2);
			int[] rightNums = mergeSortRecursion(nums, (right+left)/2+1, right);
			int[] res = merge(leftNums, rightNums);
			return res;
		}

		public static int[] merge(int[] left, int[] right) {
			int leftIndex = 0;
			int rightIndex = 0;
			int mergeIndex = 0;
			int[] res = new int[left.length+right.length];
			while (leftIndex < left.length && rightIndex < right.length) {
				if (left[leftIndex] <= right[rightIndex]) {
					res[mergeIndex] = left[leftIndex];
					leftIndex++;
					mergeIndex++;
				} else {
					res[mergeIndex] = right[rightIndex];
					rightIndex++;
					mergeIndex++;
				}
			}
			if (leftIndex == left.length) {
				while (rightIndex < right.length) {
					res[mergeIndex] = right[rightIndex];
					mergeIndex++;
					rightIndex++;
				}
			} else {
				while (leftIndex < left.length) {
					res[mergeIndex] = left[leftIndex];
					mergeIndex++;
					leftIndex++;
				}
			}
			return res;
		}
}

class CountingSort {
	public static void sort(int[] nums) {
		int max=nums[0];
		int min=nums[0];
		int[] res = new int[nums.length];
		for (int i:nums) {
			if (i>max) {
				max=i;
			}
			if (i<min) {
				min=i;
			}
		}
		int k=max-min+1;
		int[] tag = new int[k];
		for (int i:nums) {
			tag[i-min]++;
		}
		for (int i=1; i<tag.length; i++) {
			tag[i]+=tag[i-1];
		}
		for (int i=nums.length-1; i>=0; i--) {
			res[tag[nums[i]-min]-1] = nums[i];
			tag[nums[i]-min]--;
		}
		for (int i=0; i<nums.length; i++) {
			nums[i] = res[i];
		}
	}
}

class BucketSort {
	public static void sort(int[] nums) {
		int min = nums[0];
		int max = nums[0];
		int tmp;
		for (int i:nums) {
			if (i > max) {
				max = i;
			}
			if (i < min) {
				min = i;
			}
		}
		int bucketNum = (max-min)/10;
		List<LinkedList<Integer>> bucketList = new ArrayList<>();
		for (int i=0; i<bucketNum; i++) {
			bucketList.add(new LinkedList<Integer>());
		}
		for (int i=0; i<nums.length; i++) {
			tmp = (nums[i]-min)/(max-min);
			bucketList.get(tmp).add(nums[i]);
		}
		for (int i=0; i<bucketNum; i++) {
			Collections.sort(bucketList.get(i));
		}
		int resIndex=0;
		for (int i=0; i<bucketNum; i++) {
			while (bucketList.get(i).size()>0) {
				nums[resIndex] = bucketList.get(i).remove();
				resIndex++;
			}
		}
	}
}

class RadixSort {
	public static void sort(int[] nums) {
		List<LinkedList<Integer>> bucketList = new ArrayList<>();
		for (int i=0; i<10; i++) {
			bucketList.add(new LinkedList<>());
		}
		int max=nums[0];
		int min=nums[0];
		for (int i:nums) {
			if (i > max) {
				max = i;
			}
			if (i < min) {
				min = i;
			}
		}
		int maxTmp=1;
		while ((max-min)/(10*maxTmp)!=0) {
			maxTmp++;
		}
		int tmp=1;
		int resIndex;
		while (tmp<=maxTmp) {
			for (int i:nums) {
//				System.out.println();
				bucketList.get((i-min)%(int)Math.pow(10, tmp)/(int)Math.pow(10, tmp-1)).add(i-min);
			}
			resIndex=0;
			for (int i=0; i<10; i++) {
				while (bucketList.get(i).size()!=0) {
					nums[resIndex]=bucketList.get(i).poll()+min;
					resIndex++;
				}
			}
			tmp++;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值