Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Well Ordered Sort function

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Well Ordered Sort function

    need to create a code that sorts for "well ordered numbers", ie (1<2<3 , 4<7<9.)

    Initially had trouble through the use of a string char evaluation being sufficient, changed it to OOP to complete evaulation.


  2. #2
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Well Ordered Sort function

    import java.util.Arrays;
    import java.util.Scanner;
    public class wellOrderSorter {
        private int startCount;
        private int maxCount;
     
     
        wellOrderSorter(int number)
        {     
            startCount= (int)Math.pow(10,number-1);
            maxCount = (int)Math.pow(10,number)-1;
        }
        public int getStartCount(){
            return startCount;
        }
        public void setStartCount(int startCount){
            this.startCount=startCount;
        }
        public int getMaxCount(){
            return maxCount;
        }
        public void setMaxCount(int maxCount){
                this.maxCount = maxCount;
        }
       public boolean wellOrderSort(int number){
        int[] sortArray = new int [Integer.toString(number).length()];
     
     
         for(int i =0; i<sortArray.length; i++)
             sortArray[i]=Integer.toString(number).charAt(i);
     
         for(int i = 0; i<sortArray.length-1; i++ )
          if (sortArray[i]>=sortArray[i+1])
              return false;
          return true;
       }
       public int [] wholeSort()
       {
           int count = 0;
           int arrayIndex =0;
           if (startCount==1){
               return null;
           }
           for (int i = startCount; i<=maxCount; i++)
              if (wellOrderSort(i))
                  count++;
           int wellArray[] = new int[count];
           for (int i =startCount; i<=maxCount;i++)
               if(wellOrderSort(i)){
                   wellArray[arrayIndex]=i;
                   arrayIndex++;
               }
           return wellArray;
       }
     
     
     
     
     
        public static void main(String[] args){
     
            wellOrderSorter newSorter = new wellOrderSorter(7);
           System.out.println("All possible well ordered combinantions for a "+7+" digit number are\n"+Arrays.toString(newSorter.wholeSort()));
     
        }
    }

Similar Threads

  1. Replies: 1
    Last Post: October 25th, 2012, 03:03 PM
  2. Ordered Linked List
    By LemmyWinks in forum Java Theory & Questions
    Replies: 1
    Last Post: July 21st, 2012, 04:49 PM
  3. How to call a C sort function to sort a Java Array.
    By Dwere13 in forum Java Native Interface
    Replies: 22
    Last Post: July 12th, 2012, 05:44 PM
  4. Ordered Binary Decision Tree
    By nilay in forum Member Introductions
    Replies: 1
    Last Post: October 7th, 2011, 07:12 AM
  5. I Cant seem to get my sort routine to function
    By rrickman9 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 22nd, 2011, 08:48 PM