Fall in love with [data structures] radix sort, bucket sort, sleep sort

Ten classic sorting algorithm!
Here Insert Picture Description

Foreword

pleasebe sureLook at this: sorting algorithms pre-knowledge + code environment ready .

When the contents of the above are ready, then start radix sort it!

Radix Sort

Radix sort well suited for sorting integers (especially non-negative integer ), here only for non-negative integers radix sort.

Flow of execution: in order to single digits, tens, hundreds digit, the thousand, ten thousand digits ... sort (from low to high)
Here Insert Picture Description

Code

Digit, tens digit, the hundreds digit in the range 0 to 9 are fixed, can count sorting to sort them.

package com.mj.sort;

public class RadixSort extends Sort<Integer> {

	@Override
	protected void sort() {
		// 找出最大值
		int max = array[0];
		for (int i = 1; i < array.length; i++) {
			if (array[i] > max) {
				max = array[i];
			}
		}
		
		// 个位数: array[i] / 1 % 10 = 3
		// 十位数:array[i] / 10 % 10 = 9
		// 百位数:array[i] / 100 % 10 = 5
		// 千位数:array[i] / 1000 % 10 = ...

		for (int divider = 1; divider <= max; divider *= 10) {
			countingSort(divider);
		}
	}
	
	protected void countingSort(int divider) {
		// 开辟内存空间,存储次数
		int[] counts = new int[10];
		// 统计每个整数出现的次数
		for (int i = 0; i < array.length; i++) {
			counts[array[i] / divider % 10]++;
		}
		// 累加次数
		for (int i = 1; i < counts.length; i++) {
			counts[i] += counts[i - 1];
		}
		
		// 从后往前遍历元素,将它放到有序数组中的合适位置
		int[] newArray = new int[array.length];
		for (int i = array.length - 1; i >= 0; i--) {
			newArray[--counts[array[i] / divider % 10]] = array[i];
		}
		
		// 将有序数组赋值到array
		for (int i = 0; i < newArray.length; i++) {
			array[i] = newArray[i];
		}
	}
}

Complexity and stability

  • Preferably, the worst average time complexity: O (d * (n + k)), d is the maximum number of bits, k is the hexadecimal
  • Space complexity: O (n + k), k is the hexadecimal
  • Radix Sort belong to stable sort

Radix sort - another idea

Here Insert Picture Description

Code

Here Insert Picture Description
Here Insert Picture Description

Complexity and stability

  • Space complexity is O (kn + k), the time complexity is O (dn)
  • d is the maximum number of bits, k is the hexadecimal

Bucket sort

Implementation process:

  • ① create a certain number of barrels (such as using arrays, linked lists as a barrel)
  • ② according to a certain rule (different types of data, different rules), the elements of a sequence corresponding to a uniform distribution of the tub
  • ③ were sorted individually for each bucket
  • ④ all non-empty bucket elements are combined into an ordered sequence

Element index in the bucket: the number of elements element values ​​*
Here Insert Picture Description

achieve

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Complexity and stability

  • Space complexity: O (n + m), m is the number of buckets
  • Time complexity: Here Insert Picture Description
    thus is O (n + k), k is the n * logn - n * logm
  • Bucket sort belongs to the stable sort

"Sort of the strongest" - sleep sort

/**
 * 休眠排序
 */
public class SortThread extends Thread{
	public int value;
	public SortThread(int value) {
		this.value = value;
	}
	public void run(){
		try{
			Thread.sleep(value);
			System.out.println(value);
		}catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		int [] array = {10, 100, 50, 30, 60, 61, 64, 47,79, 59};
		for (int i = 0; i < array.length; i++) {
			new SortThread(array[i]).start();
		}
		
	}
}
Published 170 original articles · won praise 47 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_43734095/article/details/105170908