Eighty-nine. Counting Sorting, Cardinal Sorting (Search and Sort (4))-JAVA

Search and Sort (1)
Search and Sort (2)
Search and Sort (3)

Count sort

  • One sentence: Use auxiliary arrays to count the numbers that appear in the array, convert elements to subscripts, and subscripts to elements
  • Steps:
    1. Find the largest element value in the original array and record it as max.
    2. Create a new array helper whose length is max plus 1, and its elements are all 0 by default.
    3. Traverse the elements in the original array, use the elements in the original array as the index of the helper array, and use the number of occurrences of the elements in the original array as the element value of the helper array.
    4. Create the result array result and start index index.
    5. Traverse the helper array to find the element whose element value is greater than 0, and fill its corresponding index as the element value into the result array. After each processing, the element value in count is reduced by 1 until the element value is not greater than 0, sequentially process the remaining elements in count.
    6. Return the result array result.
import java.util.Scanner;

public class LianXi {
    
    
   public static void countSort(int []A){
    
    
	 
	   //找出数组A的最大值
	   int max = Integer.MIN_VALUE;
	   for(int num : A){
    
    
		   max = Math.max(max, num);
	   }
	   //初始化计数数组helper
	   int []helper = new int[max+1];
	   //对计数数组各元素赋值
	   for(int num : A){
    
    
		   helper[num]++;
	   }
	   //创建结果数组
	   int []result = new int[A.length];
	   //创建结果数组的起始索引
	   int index = 0;
	   //遍历计数数组,将计数数组的索引填充到结果数组中
	   for(int i = 0; i<helper.length; i++){
    
    
		   while(helper[i]>0){
    
    
			   result[index++] = i;
			   helper[i]--;
		   }
	   }
	   for(int i = 0; i<result.length; i++){
    
    
		   System.out.print(result[i] + " ");
	   }
   }
   public static void main(String[] args){
    
    
	   Scanner in = new Scanner(System.in);
	   int N = in.nextInt();
	   int []A = new int[N];
	   for(int i = 0; i<N; i++){
    
    
		   A[i] = in.nextInt();
	   }
	   System.out.println("初始数组为:");
	   for(int i = 0; i<N; i++){
    
    
		   System.out.print(A[i] + " ");
	   }
	   System.out.println("\n");
	   System.out.println("计数排序后数组为:");
	   countSort(A);
   }
}

Insert picture description here
Base sort

The radix sorting process does not need to compare keywords, but the process of "distribution" and "collection" to achieve sorting, and its time complexity can reach linear order O(n). For decimal numbers, each digit is in {0,9}, and d digits have d columns. The radix sorting is first sorted by the least significant digits. Then sort the order one by one until the highest order is finished.
Convention: There are no zeros and no negative numbers in the array to be sorted (if there are negative numbers, you can do a conversion to all positive numbers)
Insert picture description here

import java.util.ArrayList;
import java.util.Scanner;

public class LianXi {
    
    
    
	//10个桶,每个桶装的数个数不定,适合用数组加ArrayList
	private static ArrayList[] bucket = new ArrayList[10];
	
	//初始化桶
	static {
    
    
		for(int i = 0; i < bucket.length; i++){
    
    
			bucket[i] = new ArrayList();
		}
	}
	
	//将数组arr,按d这个位来分配和收集	
	private static void sort(int[] arr, int d){
    
    
		//全部入桶
		for(int i = 0; i < arr.length; i++){
    
    
			putInBucket(arr[i],getDigitOn(arr[i], d));
		}
		
		//每个桶中的元素依次压入原数组
		int k = 0;
		for(int j = 0; j < bucket.length; j++){
    
       //每个桶
			for(Object m : bucket[j]){
    
    
				arr[k++] = (Integer) m;
			}
		}
		
		//记得清空
		clearAll();
	}
   
	public static void putInBucket(int data, int digitOn){
    
    
		switch(digitOn){
    
    
			case 0:
				bucket[0].add(data);
				break;
			case 1:
				bucket[1].add(data);
				break;
			case 2:
				bucket[2].add(data);
				break;
			case 3:
				bucket[3].add(data);
				break;
			case 4:
				bucket[4].add(data);
				break;
			case 5:
				bucket[5].add(data);
				break;
			case 6:
				bucket[6].add(data);
				break;
			case 7:
				bucket[7].add(data);
			case 8:
				bucket[8].add(data);
				break;
			case 9:
				bucket[9].add(data);
				break;
		}
	}
	
	//获取第d为上的数
	private static int getDigitOn(int i, int d) {
    
    
		if(d <= 1)
			return i%10;
		int num = i / ((d-1) * 10)  % 10;
			return num;
		}
		
	private static void clearAll() {
    
    
		for(ArrayList b : bucket){
    
    
			b.clear();
		}		
	}
	
	public static void sort(int[] arr){
    
    
		//入桶依据的位初始化为1
		int d = 1; 
		//获取最大值
		int max = Integer.MIN_VALUE;
		for(int num : arr){
    
    
			max = Math.max(max, num);
		}
		//最大数据的位数
		int dNum = 1;
		while(max / 10 != 0){
    
    
			dNum++;
			max /= 10;
		}
		
		while(d<=dNum){
    
    
			//依据第二个参数入桶和出桶
			sort(arr, d++);
		}
	}
	
	public static void main(String[] args){
    
    
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int []arr = new int[N];
		for(int i = 0; i<N; i++){
    
    
			arr[i] = in.nextInt();
		}
	    System.out.println("初始数组为:");
	    for(int i = 0; i<N; i++){
    
    
	    	System.out.print(arr[i] + " ");
	    }
	    System.out.println("\n");
	    sort(arr);
	    System.out.println("基数排序后数组为:");
	    for(int i = 0; i<N; i++){
    
    
	    	System.out.print(arr[i] + " ");
	    }
	}
}

Insert picture description here

Guess you like

Origin blog.csdn.net/JiangYu200015/article/details/113825266