![](/img/trans.png)
[英]How to sort a stack using only Push, Pop, Top, IsEmpty, IsFull?
[英]How to sort a stack using only stack operations?
我在網上找到了這個問題。
給定一個堆棧 S,編寫一個 C 程序來對堆棧進行排序(按升序)。 我們不允許對堆棧的實現方式做出任何假設。 要使用的唯一功能是:
Push
Pop
Top
IsEmpty
IsFull
我認為我們可以構建堆並對其進行排序。 對此的最佳解決方案是什么?
假設這里允許的唯一數據結構是堆棧,那么您可以使用 2 個堆棧。
迭代直到原棧為空,在每次迭代中,從原棧中彈出一個元素,當第二個棧頂元素大於移除的元素時,彈出第二個棧並將其壓入原棧。 現在您可以將最初從原始堆棧中彈出的元素推送到第二個堆棧。
這種方法的時間復雜度是 O(N^2)。
實現這個算法的 C 代碼是(原諒我生疏的 C 技能):
void SortStack(struct Stack * orig_stack)
{
struct Stack helper_stack;
while (!IsEmpty(orig_stack))
{
int element = Pop(orig_stack);
while (!IsEmpty(&helper_stack) && Top(&helper_stack) < element)
{
Push(orig_stack, Pop(&helper_stack));
}
Push(&helper_stack, element);
}
while (!IsEmpty(&helper_stack))
{
Push(orig_stack, Pop(&helper_stack));
}
}
鑒於這些堆棧操作,您可以編寫遞歸插入排序。
void sort(stack s) {
if (!IsEmpty(s)) {
int x = Pop(s);
sort(s);
insert(s, x);
}
}
void insert(stack s, int x) {
if (!IsEmpty(s)) {
int y = Top(s);
if (x < y) {
Pop(s);
insert(s, x);
Push(s, y);
} else {
Push(s, x);
}
} else {
Push(s, x);
}
}
它可以使用相同的堆棧遞歸完成。 O(n^2) 我已經用 C++ 編碼了它,但是轉換為 C 是微不足道的。 我只是喜歡模板,而您確實將您的問題標記為 C++
template<typename T>
void Insert(const T& element, Stack<T>& stack)
{
if(element > stack.Top())
{
T top = stack.Pop();
Insert(element, stack);
stack.Push(top);
}
else
{
stack.Push(element);
}
}
template<typename T>
void StackSort(Stack<T>& stack)
{
if(!stack.IsEmpty())
{
T top = stack.Pop();
StackSort(stack);
Insert(top, stack);
}
}
編輯以收回我的投票! :))
煎餅排序是另一種有趣的方法: http : //en.wikipedia.org/wiki/Pancake_sorting#cite_note-4 。
這應該是實現 3 堆棧排序的最快方法。 時間復雜度為 O(n log(n))。 目標是在項目從排序堆棧中彈出時以升序結束。
多相歸並排序的 Wiki 文章(使用數組):
http://en.wikipedia.org/wiki/Polyphase_merge_sort
3 堆棧多相排序的示例 C++ 代碼,使用指針,一個指針作為每個堆棧的堆棧指針,以及一個指向每次運行結束和每個堆棧結束的指針。 運行大小指針用於跟蹤運行大小何時在堆棧中間增加或減少。 當數據在堆棧之間傳輸時,降序序列標志用於跟蹤序列是降序還是升序。 它在開始時被初始化,然后在每對運行合並后交替。
typedef unsigned int uint32_t;
static size_t fibtbl[48] =
{ 0, 1, 1, 2, 3, 5,
8, 13, 21, 34, 55, 89,
144, 233, 377, 610, 987, 1597,
2584, 4181, 6765, 10946, 17711, 28657,
46368, 75025, 121393, 196418, 317811, 514229,
832040, 1346269, 2178309, 3524578, 5702887, 9227465,
14930352, 24157817, 39088169, 63245986, 102334155, 165580141,
267914296, 433494437, 701408733,1134903170,1836311903,2971215073};
// binary search: return index of largest fib() <= n
size_t flfib(size_t n)
{
size_t lo = 0;
size_t hi = sizeof(fibtbl)/sizeof(fibtbl[0]);
while((hi - lo) > 1){
size_t i = (lo + hi)/2;
if(n < fibtbl[i]){
hi = i;
continue;
}
if(n > fibtbl[i]){
lo = i;
continue;
}
return i;
}
return lo;
}
// poly phase merge sort array using 3 arrays as stacks, a is source
uint32_t * ppmrg3s(uint32_t a[], uint32_t b[], uint32_t c[], size_t n)
{
if(n < 2)
return a;
uint32_t *asp = a; // a stack ptr
uint32_t *aer; // a end run
uint32_t *aes = a + n; // a end
uint32_t *bsp = b + n; // b stack ptr
uint32_t *ber; // b end run
uint32_t *bes = b + n; // b end
uint32_t *csp = c + n; // c stack ptr
uint32_t *ces = c + n; // c end
uint32_t *rsp = 0; // run size change stack ptr
size_t ars = 1; // run sizes
size_t brs = 1;
size_t scv = 0-1; // size change increment / decrement
bool dsf; // == 1 if descending sequence
{ // block for local variable scope
size_t f = flfib(n); // fibtbl[f+1] > n >= fibtbl[f]
dsf = ((f%3) == 0); // init compare flag
if(fibtbl[f] == n){ // if exact fibonacci size, move to b
for(size_t i = 0; i < fibtbl[f-1]; i++)
*(--bsp) = *(asp++);
} else { // else move to b, c
// update compare flag
dsf ^= (n - fibtbl[f]) & 1;
// move excess elements to b
aer = a + n - fibtbl[f];
while (asp < aer)
*(--bsp) = *(asp++);
// move dummy count elements to c
aer = a + fibtbl[f - 1];
while (asp < aer)
*(--csp) = *(asp++);
rsp = csp; // set run size change ptr
}
} // end local variable block
while(1){ // setup to merge pair of runs
if(asp == rsp){ // check for size count change
ars += scv; // (due to dummy run size == 0)
scv = 0-scv;
rsp = csp;
}
if(bsp == rsp){
brs += scv;
scv = 0-scv;
rsp = csp;
}
aer = asp + ars; // init end run ptrs
ber = bsp + brs;
while(1){ // merging pair of runs
if(dsf ^ (*asp <= *bsp)){ // if a <= b
*(--csp) = *(asp++); // move a to c
if(asp != aer) // if not end a run
continue; // continue back to compare
do // else move rest of b run to c
*(--csp) = *(bsp++);
while (bsp < ber);
break; // and break
} else { // else a > b
*(--csp) = *(bsp++); // move b to c
if(bsp != ber) // if not end b run
continue; // continue back to compare
do // else move rest of a run to c
*(--csp) = *(asp++);
while (asp < aer);
break; // and break
}
} // end merging pair of runs
dsf ^= 1; // toggle compare flag
if(bsp == bes){ // if b empty
if(asp == aes) // if a empty, done
break;
std::swap(bsp, csp); // swap b, c
std::swap(bes, ces);
brs += ars; // update run size
} else { // else b not empty
if(asp != aes) // if a not empty
continue; // continue back to setup next merge
std::swap(asp, csp); // swap a, c
std::swap(aes, ces);
ars += brs; // update run size
}
}
return csp; // return ptr to sorted array
}
使用包含交換函數的自定義堆棧類 (xstack) 進行排序的示例 Java 代碼。
static final int[] FIBTBL =
{ 0, 1, 1, 2, 3, 5,
8, 13, 21, 34, 55, 89,
144, 233, 377, 610, 987, 1597,
2584, 4181, 6765, 10946, 17711, 28657,
46368, 75025, 121393, 196418, 317811, 514229,
832040, 1346269, 2178309, 3524578, 5702887, 9227465,
14930352, 24157817, 39088169, 63245986, 102334155, 165580141,
267914296, 433494437, 701408733,1134903170,1836311903};
// return index of largest fib() <= n
static int flfib(int n)
{
int lo = 0;
int hi = 47;
while((hi - lo) > 1){
int i = (lo + hi)/2;
if(n < FIBTBL[i]){
hi = i;
continue;
}
if(n > FIBTBL[i]){
lo = i;
continue;
}
return i;
}
return lo;
}
// poly phase merge sort using 3 stacks
static void ppmrg3s(xstack a, xstack b, xstack c)
{
if(a.size() < 2)
return;
int ars = 1; // init run sizes
int brs = 1;
int asc = 0; // no size change
int bsc = 0;
int csc = 0;
int scv = 0-1; // size change value
boolean dsf; // == 1 if descending sequence
{ // block for local variable scope
int f = flfib(a.size()); // FIBTBL[f+1] > size >= FIBTBL[f]
dsf = ((f%3) == 0); // init compare flag
if(FIBTBL[f] == a.size()){ // if exact fibonacci size,
for (int i = 0; i < FIBTBL[f - 1]; i++) { // move to b
b.push(a.pop());
}
} else { // else move to b, c
// update compare flag
dsf ^= 1 == ((a.size() - FIBTBL[f]) & 1);
// i = excess run count
int i = a.size() - FIBTBL[f];
// j = dummy run count
int j = FIBTBL[f + 1] - a.size();
// move excess elements to b
do{
b.push(a.pop());
}while(0 != --i);
// move dummy count elements to c
do{
c.push(a.pop());
}while(0 != --j);
csc = c.size();
}
} // end block
while(true){ // setup to merge pair of runs
if(asc == a.size()){ // check for size count change
ars += scv; // (due to dummy run size == 0)
scv = 0-scv;
asc = 0;
csc = c.size();
}
if(bsc == b.size()){
brs += scv;
scv = 0-scv;
bsc = 0;
csc = c.size();
}
int arc = ars; // init run counters
int brc = brs;
while(true){ // merging pair of runs
if(dsf ^ (a.peek() <= b.peek())){
c.push(a.pop()); // move a to c
if(--arc != 0) // if not end a
continue; // continue back to compare
do{ // else move rest of b run to c
c.push(b.pop());
}while(0 != --brc);
break; // and break
} else {
c.push(b.pop()); // move b to c
if(0 != --brc) // if not end b
continue; // continue back to compare
do{ // else move rest of a run to c
c.push(a.pop());
}while(0 != --arc);
break; // and break
}
} // end merging pair of runs
dsf ^= true; // toggle compare flag
if(b.empty()){ // if end b
if(a.empty()) // if end a, done
break;
b.swap(c); // swap b, c
brs += ars;
if (0 == asc)
bsc = csc;
} else { // else not end b
if(!a.empty()) // if not end a
continue; // continue back to setup next merge
a.swap(c); // swap a, c
ars += brs;
if (0 == bsc)
asc = csc;
}
}
a.swap(c); // return sorted stack in a
}
java自定義堆棧類:
class xstack{
int []ar; // array
int sz; // size
int sp; // stack pointer
public xstack(int sz){ // constructor with size
this.ar = new int[sz];
this.sz = sz;
this.sp = sz;
}
public void push(int d){
this.ar[--sp] = d;
}
public int pop(){
return this.ar[sp++];
}
public int peek(){
return this.ar[sp];
}
public boolean empty(){
return sp == sz;
}
public int size(){
return sz-sp;
}
public void swap(xstack othr){
int []tempar = othr.ar;
int tempsz = othr.sz;
int tempsp = othr.sp;
othr.ar = this.ar;
othr.sz = this.sz;
othr.sp = this.sp;
this.ar = tempar;
this.sz = tempsz;
this.sp = tempsp;
}
}
來自T33C 答案的修改代碼
(斯萬之前給出校正從語言標簽C ++到Ç ):
stack.top()
只能在!stack.empty()
檢查
void insert(int element, stack<int> &stack) {
if (!stack.empty() && element > stack.top()) {
int top = stack.top();
stack.pop();
insert(element, stack);
stack.push(top);
}
else {
stack.push(element);
}
}
void sortStack(stack<int> & stack) {
if (!stack.empty()) {
int top = stack.top();
stack.pop();
sortStack(stack);
insert(top, stack);
}
}
請注意, Michael J. Barber 的回答(見上文)是不正確的,它在元素為空時忘記將元素推回堆棧。 正確版本如下:
void sort(stack s) {
if (!IsEmpty(s)) {
int x = Pop(s);
sort(s);
insert(s, x);
}
}
void insert(stack s, int x) {
if (!IsEmpty(s)) {
int y = Top(s);
if (x < y) {
Pop(s);
insert(s, x);
Push(s, y);
} else {
Push(s, x);
}
} else {
Push(s, x); // !!! must step, otherwise, the stack will eventually be empty after sorting !!!
}
}
這是基於@OrenD 給出的答案的 Javascript 解決方案
var org = [4,67,2,9,5,11,3];
var helper = [];
while(org.length > 0){
var temp = org.pop();
while((helper.length > 0) && (helper[helper.length-1] < temp)){
org.push(helper.pop());
}
helper.push(temp);
}
while(helper.length > 0){
org.push(helper.pop());
}
/* the basic idea is we go on
* popping one element (o) from the original stack (s) and we
* compare it with the new stack (temp)
* if o (the popped element from original stack)
* is < the peek element from new stack temp
* then we push the new stack element to original stack
* and recursively keep calling till temp is not empty
* and then push the element at the right place.
* else we push the element to the new stack temp
* (o >= new temp stack.
*
* Entire logic is recursive.
*/
public void sortstk( Stack s )
{
Stack<Integer> temp = new Stack<Integer>();
while( !s.isEmpty() )
{
int s1 = (int) s.pop();
while( !temp.isEmpty() && (temp.peek() > s1) )
{
s.push( temp.pop() );
}
temp.push( s1 );
}
// Print the entire sorted stack from temp stack
for( int i = 0; i < temp.size(); i++ )
{
System.out.println( temp.elementAt( i ) );
}
}
使用三個堆棧 S(源堆棧,具有未排序元素的堆棧)、A、B,您可以開始玩類似於歸並排序的游戲。
我認為很明顯,如果您對 A 和 B 上的元素進行了排序(以相同的方向,升序或降序),則可以使用 S 對 A 和 B 進行合並排序,然后將結果移動到例如 B .
您在 A、B 或 S 上有一些元素這一事實並不會阻止您使用 A、B 或 S 進行合並(,只要您擁有 A、B 和 S 的當前大小的標記,那么您不會超調)。 如果您的過程在 S 上帶來了有序元素,那么使用 A 和 B 以您喜歡的任何方向將已排序部分移到 A 或 B 中是沒有大腦的:您可以直接將其與 A 或 B 以相反的順序放置,或者,例如,將所有元素放置到 A,然后再次反轉到 B。
假設您在 A 上有 4 個已排序的元素,在 B 上有 16 個,以及在 S 上有一些未排序的元素。
A.push(S.pop) 現在創建一個 2 元素排序合並。 再次 B.push(S.pop) 並創建另一個 2 元素排序合並。 如果合並沒有分開和/或不在同一方向,您可以按您喜歡的任何方式操作元素,直到在 B(甚至 S)上達到 4 元素排序合並。 現在合並來自 A 的初始 4 元素排序合並和 B 上的那部分,直到達到 8 元素排序合並。
重復所有操作,直到創建另一個 8 元素排序合並。 您將它以正確的順序放在 B(或 S)上並合並以獲得 16 元素排序合並。 現在您將它放在 A(或 S)上,並與我們一直在 B 上進行的 16 元素合並合並。
優化的實現很棘手,因為您需要減少從一個堆棧到另一個堆棧的移動(恢復)排序合並。 但是,如果您修復並決定要使用 A、B 和 S 的內容並強制例如:A 以降序包含較小的合並部分和 B 較大的合並部分,則算法更簡單。
您需要將一些頂部元素從一個堆棧移動到另一個堆棧這一事實為復雜性增加了一個常數因子,但它永遠不會超過 2。除此之外,復雜性是 n*log(n)(您達到了 2n 個元素通過合並 2 個 n 元素排序合並進行排序合並,並且合並需要不超過 2n 步。)
C#中的實現(其他類似的語言很明顯)
void Stacking(Stack<int> sourceStack, Stack<int> mainStack, Stack<int> childStack, int n)
{
if (n == 0) return;
if (n == 1)
{
mainStack.Push(sourceStack.Pop());
return;
}
int mainSize = n/2;
int childSize = n - n/2;
Stacking(sourceStack, mainStack, childStack, mainSize);
Stacking(sourceStack, childStack, mainStack, childSize);
while (true)
{
while (mainSize > 0 && mainStack.Peek() >= childStack.Peek())
{
sourceStack.Push(mainStack.Pop());
mainSize--;
}
if (mainSize == 0) break;
while (childSize > 0 && childStack.Peek() >= mainStack.Peek())
{
sourceStack.Push(childStack.Pop());
childSize--;
}
if (childSize == 0) break;
}
while (mainSize > 0)
{
sourceStack.Push(mainStack.Pop());
mainSize--;
}
while (childSize > 0)
{
sourceStack.Push(childStack.Pop());
childSize--;
}
for (int i = 0; i < n; i++)
mainStack.Push(sourceStack.Pop());
}
void SortStack(Stack<int> sourceStack)
{
int n = sourceStack.Count();
// possible optimization: if (n < 2) return;
Stack<int> mainStack = new Stack<int>();
Stack<int> childStack = new Stack<int>();
Stacking(sourceStack, mainStack, childStack, n);
for (int i = 0; i < n; i++)
sourceStack.Push(mainStack.Pop());
}
如果您最多可以使用 log(n) 個堆棧,則可以簡化上述過程。 這個堆棧數並不意味着您將永遠使用大於 n 階的額外空間。 您只需標記將用於合並 1、2、4... 元素的堆棧。 在這種情況下,您不關心順序,因為合並 2^n 的部分將始終按相同方向排序。 但是,這種解決方案只是規避了您只能使用push和pop操作的事實; 除此之外它在所有方面都相當於歸並排序。 本質上,如果堆棧的數量不受限制,那么您也可以輕松模擬快速排序。
我認為冒泡排序可以通過遞歸在堆棧上工作。
void sortStack(stack<int>& st){
bool flag = true;
int n = st.size();
for(int i = 1; i <= n - 1 && flag; i++){
flag = false;
bubbleSortStack(st, flag, i);
}
}
void bubbleSortStack(stack<int>& st, bool& flag, int endSize){
if(st.size() == endSize)
return;
int val = st.top();
st.pop();
if(val > st.top()){
flag = true;
int tmp = st.top();
st.push(val);
val = tmp;
}
bubbleSortStack(st, flag);
st.push(val);
}
如果您沒有關於堆棧內容的任何額外信息,那么您幾乎只能將所有數據取出,對其進行排序,然后將其放回原處。Heapsort 在這里會很棒,quicksort 或 introsort 也是如此。
如果對使用其他數據結構沒有限制,我會說最小堆。 每當從堆棧中彈出一個元素時,將其放入堆中。 彈出完成后,從堆頂(其余的最少)取出元素並將其推入堆棧。 重復這樣的過程直到堆為空。
創建堆,平均時間為O(nlogn); 從堆中刪除元素並將元素按升序放回,平均時間也是 O(nlogn)。
它看起來如何?
class Program
{
static void Main(string[] args)
{
Stack<int> stack = new Stack<int>();
stack.Push(8);
stack.Push(5);
stack.Push(3);
stack.Push(4);
stack.Push(7);
stack.Push(9);
stack.Push(5);
stack.Push(6);
Stack<int> tempStack = new Stack<int>();
while (stack.Count > 0)
{
int temp = stack.Pop();
while(tempStack.Count>0 && tempStack.Peek() > temp)
{
stack.Push(tempStack.Pop());
}
tempStack.Push(temp);
}
while (tempStack.Count > 0)
{
Console.Write(tempStack.Pop() + " ");
}
Console.ReadLine();
}
}
由於沒有說明可以使用多少個額外的堆棧,所以我假設可以使用任意數量的堆棧來盡可能有效地解決排序問題。
如果您暫時忘記堆棧約束並假設這是一個正常的排序問題。 那你怎么解決呢? (提示:歸並排序)
使用輔助堆棧對堆棧執行正常歸並排序。 時間復雜度 - N*log(n)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.