Counting Sort&Radix Sort

1.Implement and test the Counting Sort.
2.Implement and test the Radix Sort using Binary code.
3.Implement a stack on the basis of an array with consideration of overflow and underflow.

package homework4;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainTest {

	public static void main(String[] args) 
	{
		//第一题 CountingSort
		int[] a = new int[] {2,3,4,2,6,7,9,3,2,2};
		int[] b = new int[11];
		CountingSort(a,b);
		System.out.println(Arrays.toString(b));
		
		//第二题 RadixSort Binary
		int[] c =new int[] {22,21,43,76,45,78,7,9,3,2};
		RadixSort(c);
		System.out.println(Arrays.toString(c));
		
//		//第三题 Stack
		Stack stack = new Stack(10);
		try
		{
			stack.push(10);
			stack.pop(9);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}


	public static void CountingSort(int[] A , int[] B ) {
		int k = 0;
		for(int i = 0 ; i < A.length ; i++)
		{
			if(A[i] > k)
				k = A[i];
		}
		int[] C = new int[k + 1];
		for(int i = 1 ; i <= k ; i++)
			C[i] = 0;
		
		for(int j = 0 ; j <= A.length - 1 ; j++)
			C[A[j]] = C[A[j]] + 1;
		
		for(int i = 1 ; i <= k ; i++)
			C[i] = C[i] + C[i-1];
		
		for(int j = A.length - 1 ; j >= 0 ; j--)
		{
			B[C[A[j]]] = A[j];
			C[A[j]] = C[A[j]] - 1;
		}
	}
	
	
	private static void RadixSort(int[] data) 
{
	for(int i = 0 ; i < 32 ; i++) 
	{
		ArrayList<Integer> l0 = new ArrayList<>();
		ArrayList<Integer> l1 = new ArrayList<>();
		for(int x = 0 ; x < data.length ; x++) {
			if((data[x] >> i&1) == 0)
				l0.add(data[x]);
			else l1.add(data[x]);
		}
		
		for(int j = 0 ; j < l0.size() ; j++)
			data[j] = l0.get(j);
		for(int j = 0 ; j < l1.size() ; j++)
			data[j + l0.size()] = l1.get(j);
	}
}

}

class Stack
{
	private int top;
	private int[] data;
	
	public Stack(int maxSize){
		data = new int[maxSize];
		top = -1;
	}
	
	void push(int x) throws Exception{
		if(top == data.length - 1)
			throw new Exception("The Stack is Overflow.");
		else data[++top] = x;
	}
	
	int pop(int x) throws Exception{
		if(top == -1)
			throw new Exception("The Stack is Underflow.");
		else {
			top--;
			return data[top + 1];
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答你的问题。 对于C++生成随机数,可以使用rand()函数,该函数可以生成一个0到RAND_MAX之间的随机整数。具体使用方法如下: ```c++ #include <cstdlib> #include <ctime> int main() { srand((unsigned)time(NULL)); // 初始化随机数生成器 int random_num = rand(); // 生成随机数 return 0; } ``` 对于基排序算法,其主要思想是将待排序的元素按照位数进行分解,然后按照个、十、百等位数依次排序,最终得到有序序列。具体的步骤如下: 1. 找到待排序数列中最大数的位数,记为d; 2. 对于每一位数(从个位开始到最高位),进行稳定排序(稳定排序保证相同大小的元素在排序后相对位置不变); 3. 重复步骤2,直到排序完成。 其中,中间的稳定排序采用计数排序算法。计数排序算法的主要思想是统计每个元素在序列中出现的次数,然后根据元素出现次数对元素进行排序。具体的步骤如下: 1. 统计待排序数列中每个元素出现的次数; 2. 计算小于等于每个元素的元素个数,得到每个元素在有序序列中的位置; 3. 将每个元素放置到有序序列中对应的位置; 4. 重复步骤1~3,直到所有元素都放置到有序序列中。 关于时间复杂度,基排序算法的时间复杂度为O(d*(n+k)),其中d为最大数的位数,n为待排序序列的长度,k为计数排序算法中元素的范围。计数排序算法的时间复杂度为O(n+k)。 希望以上回答能够对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值