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 5 of 5

Thread: Sorting from lowest to highest issue

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Sorting from lowest to highest issue

    I'm very new to Java and ran into a problem. My results are not in order and I'm not sure what I'm doing wrong.

    My results come out like this instead of being in order from lowest to highest:
    "77 99 44 55 22 88 11 0 66 33"


    Here's what I have:

    class ArrayIns
       {
       private long[] a;                 // ref to array a
       private int nElems;               // number of data items
    //--------------------------------------------------------------
       public ArrayIns(int max)          // constructor
          {
          a = new long[max];                 // create the array
          nElems = 0;                        // no items yet
          }
    //--------------------------------------------------------------
       public void insert(long value)    // put element into array
          {
          a[nElems] = value;             // insert it
          nElems++;                      // increment size
          }
    //--------------------------------------------------------------
       public void display()             // displays array contents
          {
          for(int j=0; j<nElems; j++)       // for each element,
             System.out.print(a[j] + " ");  // display it
          System.out.println("");
          }
    //--------------------------------------------------------------
       public void insertionSort()
          {
          int in, out;
     
          for(out=1; out<nElems; out++)     // out is dividing line
             {
             long temp = a[out];            // remove marked item
             in = out;                      // start shifts at out
             while(in>0 && a[in-1] >= temp) // until one is smaller,
                {
                a[in] = a[in-1];            // shift item to right
                --in;                       // go left one position
                }
             }  // end for
          }  // end insertionSort()
    //--------------------------------------------------------------
       }  // end class ArrayIns

    // InsertSortTest.java
    // Test the insert sort algorithm
    //--------------------------------------------------------------
     
    class InsertSortTest
    {
    	public static void main(String[] args) {
    		int maxSize = 100; 	// array size
    		ArrayIns  arr;		// reference to array
       		arr = new ArrayIns(maxSize);  // create the array
     
     
       		arr.insert(77);
       		arr.insert(99);
       		arr.insert(44);
       		arr.insert(55);
       		arr.insert(22);
       		arr.insert(88);
       		arr.insert(11);
       		arr.insert(00);
       		arr.insert(66);
       		arr.insert(33);
     
       		arr.display();   // display items
       		arr.insertionSort();  // execute insertion sort
       		arr.display();   // display items again
    	}
    }  // end class InsertSortTest


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Sorting from lowest to highest issue

    If you want an algorithm that will sort the array of long type, try this one:
    1. get first element, compare it to next element,
    if the next element is lower, interchange the two of them.
    something like this var[0] > var[1] ? or var[1] < var[0] ?
    then, compare again that first element to the next one,
    var[0] > var[2] ? if first element is higher, then interchange the their value.
    do it until you compare the first element up to the last element.

    next get the second element and compare it to the next element
    just like what you did with the first element. (just do what you did with first element,
    but this time, you don't need to compare the second element to the first element,
    since we already got the lowest element of array in first step.
    just repeat that until the last element

    that's the algorithm

    Another way is to use Arrays class in java.
    try to read this link.
    Java - Arrays (Class)Processing
    make sure to read that tutorial.
    specially the last part which is the methods and descriptions each methods in Arrays class

  3. The Following User Says Thank You to dicdic For This Useful Post:

    cb0688 (February 23rd, 2014)

  4. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Sorting from lowest to highest issue

    At first glance it appears the problem is with the implementation of the insertion sort algorithm. Is insertion sorting what you need to implement? (I.e., you can't use any other algorithm?)

  5. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Sorting from lowest to highest issue

    That's correct, jashburn.

  6. #5
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Sorting from lowest to highest issue

    A quick google search using the search term "java insertion sort" will give you lots of hits, e.g., Beginning Java - Unit 6 Arrays - Insertion Sort . The referred web page contains the Java code implementation of this algorithm. Hth!

Similar Threads

  1. DisplayMode with highest possible resolution issue
    By visst in forum AWT / Java Swing
    Replies: 2
    Last Post: January 22nd, 2014, 09:05 AM
  2. Replies: 7
    Last Post: November 18th, 2012, 05:17 AM
  3. Get the lowest value from Object[][]
    By Tyluur in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 19th, 2012, 10:55 AM
  4. [SOLVED] flightpaths, finding lowest cost -- lowest amount of crossovers
    By CjStaal in forum Algorithms & Recursion
    Replies: 4
    Last Post: May 8th, 2012, 12:47 AM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM