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.
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.
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.
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())); } }