Stack(5)对栈排序不用循环空间复杂度O(1)
2017-03-24 20:54
148 查看
对栈stack排序,不能使用循环while,for等,只能使用Stack的以下方法(Java版本)
例
算法:
例子
给定堆栈为
-3 < - 堆栈顶部
14
18
-5
30
让我们用上面的例子说明堆栈的排序:
首先从堆栈中弹出所有元素,并将pop()元素存储在变量’temp’中。在弹出所有元素功能的堆栈框架将看起来像:
temp = -3 - >堆栈帧#1
temp = 14 - >堆栈帧#2
temp = 18 - >堆栈帧#3
temp = -5 - >堆栈帧#4
temp = 30 - >堆栈帧#5
现在堆栈是空的,并且’insert_in_sorted_order()’函数被调用,并且在堆栈的底部插入30个(来自堆栈帧#5)。现在堆栈看起来如下:
30 < - 堆栈顶部
现在选择下一个元素即-5(来自堆栈帧#4)。由于-5 <30,-5插入堆栈的底部。现在堆栈变成:
30 < - 堆栈顶部
- 5
选择下一个18(来自堆栈帧#3)。由于18 <30,18被插入30以下。现在堆成为:
30 < - 堆栈顶部
18
-5
选择下一个14(来自堆栈帧#2)。自14 <30和14 <18以来,它被插入到18以下。现在堆栈变成:
30 < -所述堆叠的顶部
18 14
-5
现在-3(来自堆栈帧#1)被选择为-3 <30和-3 <18和-3 <14,它被插入到14以下。现在栈变成:
30 < -所述堆叠的顶部
18
14
-3
-5
代码
isEmpty():判断堆栈是否为空。 push(S):将新元素添加到堆栈。 pop(S):从堆栈中删除顶层元素。 peek(S):返回顶元素的值。
例
输入:-3 <---顶部 14 18 -5 30 输出:30 <---顶部 18 14 -3 -5
算法:
sortStack(Stack S) 如果堆栈不为空: temp = pop(S); sortStack(S); sortedInsert(S,temp);
sortedInsert(Stack S, element) 如果栈为空 或者element > top element push(S, elem) else temp = pop(S) sortedInsert(S, element) push(S, temp)
例子
给定堆栈为
-3 < - 堆栈顶部
14
18
-5
30
让我们用上面的例子说明堆栈的排序:
首先从堆栈中弹出所有元素,并将pop()元素存储在变量’temp’中。在弹出所有元素功能的堆栈框架将看起来像:
temp = -3 - >堆栈帧#1
temp = 14 - >堆栈帧#2
temp = 18 - >堆栈帧#3
temp = -5 - >堆栈帧#4
temp = 30 - >堆栈帧#5
现在堆栈是空的,并且’insert_in_sorted_order()’函数被调用,并且在堆栈的底部插入30个(来自堆栈帧#5)。现在堆栈看起来如下:
30 < - 堆栈顶部
现在选择下一个元素即-5(来自堆栈帧#4)。由于-5 <30,-5插入堆栈的底部。现在堆栈变成:
30 < - 堆栈顶部
- 5
选择下一个18(来自堆栈帧#3)。由于18 <30,18被插入30以下。现在堆成为:
30 < - 堆栈顶部
18
-5
选择下一个14(来自堆栈帧#2)。自14 <30和14 <18以来,它被插入到18以下。现在堆栈变成:
30 < -所述堆叠的顶部
18 14
-5
现在-3(来自堆栈帧#1)被选择为-3 <30和-3 <18和-3 <14,它被插入到14以下。现在栈变成:
30 < -所述堆叠的顶部
18
14
-3
-5
代码
package Stack; import java.util.Stack; public class SortStack { public static void main(String[] args) { int array[]={-3,14,18,-5,30}; Stack< Integer> stack=new Stack<Integer>(); int i=0; while(i<array.length) { stack.push(array[i++]); } Sort(stack); while(!stack.isEmpty()) { System.out.println(stack.pop()); } } public static void Sort(Stack< Integer> stack) { if(!stack.isEmpty()) { int temp=stack.pop(); Sort(stack); InsertSort(stack,temp); } } public static void InsertSort(Stack<Integer> stack,int Elem) { if(stack.isEmpty()||Elem>stack.peek()) { stack.push(Elem); } else { int temp=stack.pop(); InsertSort(stack, Elem); stack.push(temp); } } }
相关文章推荐
- Leet Code Binary Tree Inorder Traversal(非递归且实现不用栈的空间复杂度为O(1)的实现)
- Leet Code Binary Tree Preorder Traversal(非递归且实现不用栈的空间复杂度为O(1)的实现)
- 不用递归,空间复杂度为1的中序遍历二叉树
- 试设计一个算法,将数组A(0..n-1)中的元素循环右移k位,并要求空间复杂度为O(1),时间复杂度为O(n)。
- 不用额外空间的整数交换以及时间复杂度为O(n)空间复杂度为O(1)的排序算法
- 视频序列的空间复杂度和时间复杂度的不用模型计算
- 【每日一题】查找一个字符串中第一个只出现两次的字符。要求时间复杂度为O(N),空间复杂度为O(1)
- 【学无止境】ajax长循环,反向ajax初体会,不用ws实现即时聊天
- 2012年UC的一道笔试题——空间时间复杂度限制下的数字排序
- 常见排序的时间复杂度和空间复杂度
- 常用的数据结构操作与排序算法的时间和空间复杂度
- 第二周第二天(abstract;匿名内部类/单例设计模式/不用for循环求11000的值/finalstatic/interface 接口/instanceof;interface)
- 字符串递归求长(不用任何其他变量,不用goto,不用循环)
- 时间复杂度和空间复杂度详解
- heap stack 堆栈 全局变量 静态变量 进程空间 内存分配
- (科大讯飞)删除字符串中的连续空格(只保留一个),O(n)时间复杂度,O(1)空间复杂度
- 排序算法之 归并排序 及其时间复杂度和空间复杂度
- Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)
- 一个整型数组里除了一个或者两个或者三个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)
- 时间复杂度和空间复杂度详解