
本文针对对包含20个整数的栈进行排序,仅保留1到4范围内升序排列的值这一问题,提出了一种基于计数排序的优化方案。通过使用数组或HashMap统计各数值的频率,并按降序将数值重新压入栈中,实现了线性时间复杂度的排序。同时,强调了在Java中优先使用Deque接口的实现类代替Stack类的最佳实践。
对栈中的特定范围内的整数进行排序,并在性能上进行优化是一个常见的算法问题。初始方案虽然能够解决问题,但在时间和空间复杂度上存在改进空间。本文将介绍如何利用计数排序算法,以更高效的方式解决这一问题。
计数排序是一种非基于比较的排序算法,它通过统计每个元素出现的次数来确定排序后的位置。由于问题限定了排序范围为1到4,因此非常适合使用计数排序。
算法步骤:
代码示例(使用数组):
import java.util.Stack;
public class StackSorter {
public static Stack<Integer> sortStack(Stack<Integer> stack) {
final int min = 1;
final int max = 4;
int[] freq = new int[max - min + 1]; // 频率统计数组
// 步骤1: 统计频率
while (!stack.isEmpty()) {
int next = stack.pop();
if (next >= min && next <= max) {
freq[next - min]++; // 统计每个元素的频率
}
}
// 步骤2: 重新压栈
for (int i = freq.length - 1; i >= 0; i--) {
while (freq[i] > 0) {
stack.push(i + min);
freq[i]--;
}
}
return stack;
}
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(5);
stack.push(3);
stack.push(2);
stack.push(1);
stack.push(3);
stack.push(5);
stack.push(3);
stack.push(1);
stack.push(4);
stack.push(7);
Stack<Integer> sortedStack = sortStack(stack);
System.out.println("Sorted Stack: " + sortedStack); // 输出:Sorted Stack: [4, 3, 3, 3, 2, 1, 1]
}
}代码示例(使用HashMap):
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
public class StackSorter {
public static Stack<Integer> sortStack(Stack<Integer> stack) {
final int min = 1;
final int max = 4;
Map<Integer, Integer> freq = new HashMap<>(); // 频率统计Map
// 步骤1: 统计频率
while (!stack.isEmpty()) {
int next = stack.pop();
if (next >= min && next <= max) {
freq.merge(next, 1, Integer::sum); // 统计每个元素的频率
}
}
// 步骤 2: 重新压栈
for (int i = max; i >= min; i--) {
if(freq.containsKey(i)){
int count = freq.get(i);
while (count > 0) {
stack.push(i);
count--;
}
}
}
return stack;
}
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(5);
stack.push(3);
stack.push(2);
stack.push(1);
stack.push(3);
stack.push(5);
stack.push(3);
stack.push(1);
stack.push(4);
stack.push(7);
Stack<Integer> sortedStack = sortStack(stack);
System.out.println("Sorted Stack: " + sortedStack); // 输出:Sorted Stack: [4, 3, 3, 3, 2, 1, 1]
}
}在Java中,java.util.Stack是一个遗留类,官方建议使用Deque接口及其实现类(如ArrayDeque或LinkedList)来代替Stack。
示例:
import java.util.Deque;
import java.util.ArrayDeque;
public class StackSorter {
public static Deque<Integer> sortStack(Deque<Integer> stack) {
final int min = 1;
final int max = 4;
int[] freq = new int[max - min + 1]; // 频率统计数组
// 步骤1: 统计频率
while (!stack.isEmpty()) {
int next = stack.pop();
if (next >= min && next <= max) {
freq[next - min]++; // 统计每个元素的频率
}
}
// 步骤2: 重新压栈
for (int i = freq.length - 1; i >= 0; i--) {
while (freq[i] > 0) {
stack.push(i + min);
freq[i]--;
}
}
return stack;
}
public static void main(String[] args) {
Deque<Integer> stack = new ArrayDeque<>();
stack.push(5);
stack.push(3);
stack.push(2);
stack.push(1);
stack.push(3);
stack.push(5);
stack.push(3);
stack.push(1);
stack.push(4);
stack.push(7);
Deque<Integer> sortedStack = sortStack(stack);
System.out.println("Sorted Stack: " + sortedStack);
}
}使用计数排序算法可以高效地对栈中特定范围内的整数进行排序,其时间复杂度为线性O(n)。通过选择合适的数据结构(数组或HashMap)和遵循Java的最佳实践(使用Deque接口),可以进一步优化代码的性能和可维护性。在实际应用中,可以根据具体情况选择最合适的实现方式。
以上就是使用计数排序优化栈内特定范围整数的排序的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号