使用javascript排序堆栈的元素

时间:2016-12-22 12:51:21

标签: javascript sorting recursion stack

我试图理解使用给出的递归对堆栈元素进行排序 http://www.geeksforgeeks.org/sort-a-stack-using-recursion/ 不允许使用像while,for..etc这样的任何循环结构。我们只能在Stack S上使用以下ADT函数:

is_empty(S):测试堆栈是否为空。

push(S):向堆栈添加新元素。

pop(S):从堆栈中删除顶部元素。

top(S):返回顶部元素的值。请注意这一点                函数不会从堆栈中删除元素。 我试过下面但是收到错误

var stack = [-3, 14, 18, -5, 30];

function sortStack() {
  if (stack.length > 0) {
    temp = stack.pop();
    sortStack();
    sortedInsert(temp, stack);
  }
}

function sortedInsert(element, stack) {
  if (stack.length > 0 || element > stack[stack.length - 1]) {
    stack.push(element);
  } else {
    temp = stack.pop();
    sortedInsert(element, stack);
    stack.push(temp);
  }

}

sortStack();

console.log(stack);

RangeError: Maximum call stack size exceeded
at sortedInsert:12:22
at sortedInsert:21:5
at sortedInsert:21:5
at sortedInsert:21:5
at sortedInsert:21:5
at sortedInsert:21:5

4 个答案:

答案 0 :(得分:2)

使用javascript,本地(作用域)变量需要声明为var,否则它们是静态的。如果没有sortStack()中的var之前的t,那么t将是一个静态的,只是被每个pop弹写覆盖,在sortStack()的所有返回上留下t == -3。在sortedInsert()中使用x也会出现同样的问题。



var stack = [-3, 14, 18, -5, 30];

function sortStack(s) {
  if (s.length > 0) {
    var t = s.pop();
    sortStack(s);
    sortedInsert(s, t);
  }
}

function sortedInsert(s, e) {
  if (s.length == 0 || e > s[s.length - 1]) {
    s.push(e);
  } else {
    var x = s.pop();
    sortedInsert(s, e);
    s.push(x);
  }
}

sortStack(stack);

console.log(stack);




答案 1 :(得分:0)

var stack = [-3, 14, 18, -5, 30];

function compare(a,b) {

    return parseInt(a, 10) - parseInt(b, 10);
    }

stack.sort(compare);

console.log(stack);

答案 2 :(得分:0)

如果您只想对数组进行排序,可以使用sort()方法。检查以下示例:

var stack = [-3, 14, 18, -5, 30];
console.log(stack.sort());

如果您想了解如何手动排序数组,请查看this ans注意:以下代码是从同一个复制的):

var stack = [-3, 14, 18, -5, 30];

function arrSort(arr, subkey) {
  //Default to 0 if no subkey is set
  subkey = (subkey === undefined ? 0 : subkey);

  var a = arr.slice(0),
      b = [], x;

  // For each section in the array, create an array containing whatever we are trying to sort by and our unique ID
  for (x in a) {
    b[x] = [a[x][subkey], x];
  }

  b = b.sort();

  //Wipe out all the data that's currently in arr!
  arr.splice(0, arr.length);

  for (x in b) {
    arr.push(a[b[x][1]]);
  }

  return arr;
}

// console.log(arrSort(stack, 0));
console.log(arrSort(stack));

答案 3 :(得分:0)

/*
Create a temporary stack say tmpStack.
    While input stack is NOT empty do this:
            Pop an element from input stack call it temp
            while temporary stack is NOT empty and top of temporary stack is greater than temp,
                     pop from temporary stack and push it to the input stack
            push temp in temporary stack
The sorted numbers are in tmpStack
*/

class sortStack{
  constructor(){
      this.tempStack = [];
  }

  sortStack(inputStack){
         if(inputStack.length==0){
             return "empty "
         }
         while(inputStack.length > 0){

            let item = inputStack.pop()
            while(this.tempStack.length > 0 && item < this.tempStack[this.tempStack.length -1]){
                inputStack.push(this.tempStack.pop())
            }
            this.tempStack.push(item)
         }

         return this.tempStack;
  }

}


let a = [34, 3, 31, 98, 92, 23];
let s = new sortStack();
console.log(s.sortStack(a))

相关问题