【问题标题】:sorting elements of stack using javascript使用javascript对堆栈的元素进行排序
【发布时间】:2016-12-22 12:51:21
【问题描述】:

我试图理解使用给出的递归对堆栈元素进行排序 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

【问题讨论】:

  • geeksforgeeks.org/sort-a-stack-using-recursion 我正在尝试在 javascript 中实现这个算法
  • 您需要使用var 来获取本地临时变量,而不是全局变量。你也有混淆堆栈是空的,堆栈有sortedInsert中的元素。

标签: javascript sorting recursion stack


【解决方案1】:

使用 javascript,本地(作用域)变量需要声明为 var,否则它们是静态的。如果在 sortStack() 中 t 之前没有 var,则 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);

【讨论】:

    【解决方案2】:

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

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

    如果您想了解如何手动对数组进行排序,请查看this ans注意:以下代码是从相同的 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));
    

    【讨论】:

    • 谢谢,但我正在尝试理解与堆栈相关的算法geeksforgeeks.org/sort-a-stack-using-recursion 我正在尝试在 javascript 中实现这个算法,我们不能在 js 中实现这个
    • 我们绝对可以实现这个JS,会检查链接并尝试在我的ans中解释。
    【解决方案3】:
    var stack = [-3, 14, 18, -5, 30];
    
    function compare(a,b) {
    
        return parseInt(a, 10) - parseInt(b, 10);
        }
    
    stack.sort(compare);
    
    console.log(stack);
    

    【讨论】:

    • geeksforgeeks.org/sort-a-stack-using-recursion 我正在尝试用javascript实现这个算法
    • var stack = [-3, 14, 18, -5, 30];函数气泡排序(项目){ var length = items.length; for (var i = (length - 1); i >= 0; i--) { //传递次数 for (var j = (length - i); j > 0; j--) { //比较相邻位置 if (parseInt(items[j])
    • 不允许使用任何循环结构,例如 while、for..etc。我们只能在 Stack S 上使用以下 ADT 函数: is_empty(S) :测试堆栈是否为空。 push(S) :将新元素添加到堆栈中。 pop(S) :从堆栈中删除顶部元素。 top(S) :返回顶部元素的值。请注意,此函数不会从堆栈中删除元素。
    • 这是geeks4geeks问题描述中给出的限制
    【解决方案4】:

    /*
    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))

    【讨论】:

      猜你喜欢
      • 2011-01-23
      • 1970-01-01
      • 2015-01-14
      • 2021-09-03
      • 2021-11-02
      • 2019-05-05
      • 2020-12-04
      • 2011-06-17
      • 2010-10-22
      相关资源
      最近更新 更多