Results 1 to 1 of 1
  1. #1
    viperlasson is offline Member
    Join Date
    Jan 2010
    Rep Power

    Default Sorting an Array via Stacks

    Hello, I am having an issue with sorting an array (initialArray) of 100 integers. The method to be used is having two stacks, and by using pop/push/top, and an algorithm, to create sortedArray, a sorted version of initialArray. I'm pretty sure my constructor and methods are correct, posted below.

    Java Code:
      public Lab1Stack ()
        theArray = (int[]) new int [100];
        topOfStack = -1;
      public boolean isEmpty()
    	  if (topOfStack == -1)
    		  return true;
    		  return false;
      public void push (int value)
        theArray[++topOfStack] = value;
      public int pop ()
    	return theArray[topOfStack--];
      public int top ()
    	  return theArray[topOfStack];
    But I am reading my proffesor's hint of the algorithm, and kind of getting confused. This is what I have so far.

    Java Code:
        Lab1Stack s1 = new Lab1Stack();
        Lab1Stack s2 = new Lab1Stack();
        int value;
     for (int i=0; i<initialArray.length; i++)
        	while ((s1.isEmpty()== false) && (initialArray[i] <
            	value = s1.pop();
        	while ((s2.isEmpty() == false) && (initialArray[i] >=
        		value = s2.pop();
    It is kind of working, both arrays are being filled of and sorted, but sometimes 5 to 10 numbers are missing... Usually index 99 in the initialArray doesnt get popped into either object... Any help would be appreciated.

    Oh yeah, at this point im just looking at the two arrays, I'm not popping values into my sortedArray yet since this isn't working.
    Last edited by viperlasson; 02-01-2010 at 06:58 AM.

Similar Threads

  1. HELP!!! Problems with Array: matching, sorting, etc
    By jrelvi23 in forum New To Java
    Replies: 15
    Last Post: 11-18-2009, 11:14 PM
  2. Storing high score and sorting the array
    By Implode in forum New To Java
    Replies: 8
    Last Post: 09-28-2009, 12:43 AM
  3. Converting array to list and sorting it
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:36 PM
  4. Sorting an array of Strings
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:39 PM
  5. Replies: 0
    Last Post: 04-14-2008, 08:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts