排序类总结

1. 排序类(冒泡排序,选择排序,插入排序,快速排序,归并排序,堆排序)

2. 冒泡排序,选择排序,插入排序

public class Mysort{
       // 随机生成数组
        public static int[] generateRandomArray(int maxSize, int maxValue) {
            int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
            }
            return arr;
        }
	
        public static void swap(int[] arr,int i,int j) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
	
        // for test
        public static void printArray(int[] arr) {
            if (arr == null) {
                return;
            }
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
            System.out.println();
        }

        // for test
        public static int[] copyArray(int[] arr) {
            if (arr == null) {
                return null;
            }
            int[] res = new int[arr.length];
            for (int i = 0; i < arr.length; i++) {
                res[i] = arr[i];
            }
            return res;
        }
        // for test
        public static void comparator(int[] arr) {
                Arrays.sort(arr);
        }	
        // for test
        public static boolean isEqual(int[] arr1, int[] arr2) {
            if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
                return false;
            }
            if (arr1 == null && arr2 == null) {
                return true;
            }
            if (arr1.length != arr2.length) {
                return false;
            }
            for (int i = 0; i < arr1.length; i++) {
                if (arr1[i] != arr2[i]) {
                    return false;
                }
            }
            return true;
		}
 	
      //冒泡排序
        public static int[] bubblesort(int[] arr) {
            for(int i = arr.length-1;i>0;i--) {
                for(int j=0;j<i;j++) {
                    if(arr[j+1]<arr[j]) {
                        swap(arr, j, j+1);
                    }
                }
            }
            return arr;
        }
    
        // 选择排序
        public static int[] selectsort(int[] arr) {
            for(int i = 0;i<arr.length-1;i++) {
                int min = i;
                for(int j= i+1;j<arr.length;j++) {
                    if(arr[j]<arr[min]) {
                        min = j;
                    }
                }			
                if(min!=i) {
                    swap(arr, min, i);
                }			
            }
            return arr;
        }
		//插入排序
        public static int[] insertsort(int[] arr) {
            for(int i = 1;i<arr.length;i++) {
                for(int j=i-1;j>=0&&arr[j]>arr[j+1];j--) {
                    swap(arr, j, j+1);	
                }
            }
            return arr;
        } 
    
       // 主方法测试
    	public static void main(String[] args) {
			int testTime = 50;
			int maxSize = 100;
			int maxValue = 100;
			boolean succeed = true;
			for (int i = 0; i < testTime; i++) {
				int[] arr1 = generateRandomArray(maxSize, maxValue);
				int[] arr2 = copyArray(arr1);
				//bubblesort(arr1);
				mergeSort(arr1);
				comparator(arr2);
				printArray(arr1);
				printArray(arr2);
				if (!isEqual(arr1, arr2)) {
					succeed = false;
					printArray(arr1);
					printArray(arr2);
					break;
				}
			}
			System.out.println(succeed ? "Nice!" : "Fucking fucked!");

		}
}

3. 归并排序

import java.util.Arrays;

public class MergeSort {
	// for test
		public static int[] generateRandomArray(int maxSize, int maxValue) {
			int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
			for (int i = 0; i < arr.length; i++) {
				arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
			}
			return arr;
		}
		
		public static void swap(int[] arr,int i,int j) {
			int tmp = arr[i];
			arr[i] = arr[j];
			arr[j] = tmp;
		}
		
		// for test
		public static void printArray(int[] arr) {
			if (arr == null) {
				return;
			}
			for (int i = 0; i < arr.length; i++) {
				System.out.print(arr[i] + " ");
			}
			System.out.println();
		}
		
		// for test
		public static void comparator(int[] arr) {
				Arrays.sort(arr);
		}
		
		// for test
		public static boolean isEqual(int[] arr1, int[] arr2) {
			if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
				return false;
			}
			if (arr1 == null && arr2 == null) {
				return true;
			}
			if (arr1.length != arr2.length) {
				return false;
			}
			for (int i = 0; i < arr1.length; i++) {
				if (arr1[i] != arr2[i]) {
					return false;
				}
			}
			return true;
		}
		
		// for test
		public static int[] copyArray(int[] arr) {
			if (arr == null) {
				return null;
			}
			int[] res = new int[arr.length];
			for (int i = 0; i < arr.length; i++) {
				res[i] = arr[i];
			}
			return res;
		}
		
		//归并排序开始 
		public static void mergeSort(int[] arr) {
			if(arr==null||arr.length<2) return;
			mergeSort(arr,0,arr.length-1);
		}
		
		// 拆分
		public static void mergeSort(int[] arr, int L, int R) {
			// TODO Auto-generated method stub
			if(L==R) return;
			int mid = L + ( (R-L) >> 1);
			mergeSort(arr, L, mid);
			mergeSort(arr, mid+1, R);
			//合并
			merge(arr,L,mid,R);
		}
   		// 合并
		public static void merge(int[] arr,int l,int m,int r) {
			int[] help = new int[r-l+1];
			int i = 0;
			int p1 = l;
			int p2 = m+1;
			while(p1 <= m && p2 <= r) {
				help[i++] = arr[p1] < arr[p2] ? arr[p1++] : arr[p2++];
			}
			
			while(p1<=m) {
				help[i++] = arr[p1++];
			}
			while(p2<=r) {
				help[i++] = arr[p2++];
			}
			
			for(i=0;i<help.length;i++) {
				arr[l+i] = help[i];
			}
		}
		
		
		public static void main(String[] args) {
			int testTime = 50;
			int maxSize = 100;
			int maxValue = 100;
			boolean succeed = true;
			for (int i = 0; i < testTime; i++) {
				int[] arr1 = generateRandomArray(maxSize, maxValue);
				int[] arr2 = copyArray(arr1);
				//bubblesort(arr1);
				mergeSort(arr1);
				comparator(arr2);
				printArray(arr1);
				printArray(arr2);
				if (!isEqual(arr1, arr2)) {
					succeed = false;
					printArray(arr1);
					printArray(arr2);
					break;
				}
			}
			System.out.println(succeed ? "Nice!" : "Fucking fucked!");
		}
}

4 快速排序

public class QuickSORT {
	// for test
			public static int[] generateRandomArray(int maxSize, int maxValue) {
				int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
				for (int i = 0; i < arr.length; i++) {
					arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
				}
				return arr;
			}
			
			public static void swap(int[] arr,int i,int j) {
				int tmp = arr[i];
				arr[i] = arr[j];
				arr[j] = tmp;
			}
			
			// for test
			public static void printArray(int[] arr) {
				if (arr == null) {
					return;
				}
				for (int i = 0; i < arr.length; i++) {
					System.out.print(arr[i] + " ");
				}
				System.out.println();
			}
			
			// for test
			public static void comparator(int[] arr) {
					Arrays.sort(arr);
			}
			
			// for test
			public static boolean isEqual(int[] arr1, int[] arr2) {
				if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
					return false;
				}
				if (arr1 == null && arr2 == null) {
					return true;
				}
				if (arr1.length != arr2.length) {
					return false;
				}
				for (int i = 0; i < arr1.length; i++) {
					if (arr1[i] != arr2[i]) {
						return false;
					}
				}
				return true;
			}
			
			// for test
			public static int[] copyArray(int[] arr) {

				if (arr == null) {
					return null;
				}
				int[] res = new int[arr.length];
				for (int i = 0; i < arr.length; i++) {
					res[i] = arr[i];
				}
				return res;
			}
			
			//快速排序
			public static void quicksort(int[] arr) {
				if(arr==null||arr.length<2) return;
				quicksort(arr,0,arr.length-1);
			}
			// 拆分
			public static void quicksort(int[] arr, int L, int R) {
					if(L < R) {
						int[] p = partition(arr, L, R);
						quicksort(arr, L, p[0] - 1);
						quicksort(arr, p[1] + 1, R);
					}
			}
			// 找到中间分割坐标 枢轴点
			public static int[] partition(int[] arr,int l,int r) {
				int less = l-1;
				int more = r;
				while(l<more) {
					if(arr[l]<arr[r]) {
						swap(arr, ++less, l++);
					}else if(arr[l]>arr[r]) {
						swap(arr, --more, l);
					}else {
						l++;
					}
				}
				
				swap(arr, more, r);
				return new int[] {less+1,more};
			}
			
			
			public static void main(String[] args) {
				int testTime = 50;
				int maxSize = 100;
				int maxValue = 100;
				boolean succeed = true;
				for (int i = 0; i < testTime; i++) {
					int[] arr1 = generateRandomArray(maxSize, maxValue);
					int[] arr2 = copyArray(arr1);
					//bubblesort(arr1);
					//mergeSort(arr1);
					quicksort(arr1);
					comparator(arr2);
					printArray(arr1);
					printArray(arr2);
					if (!isEqual(arr1, arr2)) {
						succeed = false;
						printArray(arr1);
						printArray(arr2);
						break;
					}
				}
				System.out.println(succeed ? "Nice!" : "Fucking fucked!");

			}
}

5 堆排序

public class HeapSORT {
	public static int[] generateRandomArray(int maxSize, int maxValue) {
		int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
		}
		return arr;
	}
	
	public static void swap(int[] arr,int i,int j) {
		int tmp = arr[i];
		arr[i] = arr[j];
		arr[j] = tmp;
	}
	
	// for test
	public static void printArray(int[] arr) {
		if (arr == null) {
			return;
		}
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
		System.out.println();
	}
	
	// for test
	public static void comparator(int[] arr) {
			Arrays.sort(arr);
	}
	
	// for test
	public static boolean isEqual(int[] arr1, int[] arr2) {
		if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
			return false;
		}
		if (arr1 == null && arr2 == null) {
			return true;
		}
		if (arr1.length != arr2.length) {
			return false;
		}
		for (int i = 0; i < arr1.length; i++) {
			if (arr1[i] != arr2[i]) {
				return false;
			}
		}
		return true;
	}
	
	// for test
	public static int[] copyArray(int[] arr) {

		if (arr == null) {
			return null;
		}
		int[] res = new int[arr.length];
		for (int i = 0; i < arr.length; i++) {
			res[i] = arr[i];
		}
		return res;
	}
	
	
	
	public static void heapsort(int[] arr) {
		if(arr==null||arr.length<2) return;
		
		for(int i=0;i<arr.length;i++) {
			heapInsert(arr,i);
		}
		
		int size = arr.length;
		swap(arr, 0, --size);
		while(size>0) {
			heapify(arr, 0, size);
			swap(arr, 0, --size);
		}
		
		
	}
	
	public static void heapInsert(int[] arr, int index) {
			while(arr[index] > arr[(index-1)/2]) {
				swap(arr, index, (index-1)/2);
				index = (index-1)/2;
			}
	}

	public static void  heapify(int[] arr,int index,int size) {
		int left = index *2 +1;
		while(left<size) {
			int large = left+1 <size && arr[left+1] > arr[left] ? left+1:left;
			int L = arr[large] > arr[index] ? large:index;
			if(L==index) break;
			swap(arr, index, L);
			index = L;
			left = index *2 +1;
		}
	}
	
	
	public static void main(String[] args) {
		int testTime = 50;
		int maxSize = 100;
		int maxValue = 100;
		boolean succeed = true;
		for (int i = 0; i < testTime; i++) {
			int[] arr1 = generateRandomArray(maxSize, maxValue);
			int[] arr2 = copyArray(arr1);
			//bubblesort(arr1);
			//mergeSort(arr1);
			heapsort(arr1);
			comparator(arr2);
			printArray(arr1);
			printArray(arr2);
			if (!isEqual(arr1, arr2)) {
				succeed = false;
				printArray(arr1);
				printArray(arr2);
				break;
			}
		}
		System.out.println(succeed ? "Nice!" : "Fucking fucked!");

	}
}
posted @ 2021-10-15 10:45  小猫妮  阅读(32)  评论(0编辑  收藏  举报