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: How to merge an int array then put it in ascending order without any Java APIs

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to merge an int array then put it in ascending order without any Java APIs

    This is what I have:

    package challengespackage;
     
    public class ArrayMerge {
     
    	public static void main(String[] args) {
    		System.out.print(merge(new int[] { 9, 3, 0 }, new int[] { 4, 7, 8 }));
    	}
     
    	private static int[] merge(int[] iArrOne, int[] iArrTwo) {
    		int[] iArrMerged = new int[iArrOne.length + iArrTwo.length];
     
    		for (int i = 0; i < iArrOne.length; i++) {
    			iArrMerged[i] = iArrOne[i];
    		}
     
    		for (int j = 0; j < iArrTwo.length; j++) {
    			iArrMerged[iArrOne.length + j] = iArrTwo[j];
    		}
     
    		return iArrMerged;
    	}
    }

    So far all it does is combine to two arrays. I am unsure of how to sort the array in ascending order without using any Java APIs like Arrays.sort();. Any help is appreciated!
    Last edited by JBow94; November 6th, 2010 at 11:15 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to merge an int array then put it in ascending order without any Java APIs

    Are the two arrays guaranteed to be sorted before you merge them?

    If not, you can either sort each array separately and then merge them, or merge the two arrays and then sort them. There's not much difference if neither array is sorted to begin with (probably simply combine the two arrays together and then sort would be easier).

    If one or both of the arrays are already sorted, then you want to make sure both arrays are sorted.

    Once you've got the two arrays sorted, there's a merge algorithm provided here (scroll part way down the page). You could even use the complete merge sort algorithm to sort your two arrays, and then just call the merge function to merge your two arrays together.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to merge an int array then put it in ascending order without any Java APIs

    Take your pick of a sorting algorithm: Sorting Algorithms. You could as well sort them as you place them in, using a modified form of the above algorithms.

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to merge an int array then put it in ascending order without any Java APIs

    Thanks for the replies! I will check into those.

    To provide more insight on the thread, the numbers aren't in any order. What the function merge is supposed to do is:

    combine both arrays into one array - I got this
    sort the combined array into ascending order - This is what I am not sure how to do

    I am not allowed to use any APIs like Arrays.sort though.

  5. #5
    Member
    Join Date
    May 2010
    Posts
    39
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Lightbulb Re: How to merge an int array then put it in ascending order without any Java APIs

    Quote Originally Posted by JBow94 View Post
    Arrays.sort though.
    Hmm, I was just about to ask that question

    Well you could just generate the effect yourself you need to check for the lowest, and keep looping till your new array is the size of the original:
    	//Time
    	public static int[] sort(int[] unsorted) {
    		int[] sorted = new int[unsorted.length];
    		boolean[] usedIndex = new boolean[unsorted.length];
    		int size = 0;
    		while(size != unsorted.length) {
    			int lowest = Integer.MAX_VALUE;
    			int finalCur = -1;
    			for(int x = 0; x != unsorted.length; x++) {
    				if(!usedIndex[x]) {
    					if(lowest > unsorted[x]) {
    						lowest = unsorted[x];
    						finalCur = x;
    					}
     
    				}
    			}
    			usedIndex[finalCur] = true;
    			sorted[size++] = lowest;
    		}	
    		return sorted;
    	}

Similar Threads

  1. how to merge my OO with GUI
    By zyspt in forum AWT / Java Swing
    Replies: 1
    Last Post: May 18th, 2010, 03:28 PM
  2. Sort in Cyrilic order
    By cselic in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 5th, 2010, 03:08 PM
  3. Merge 2 Sorted Text Files into a Third
    By Epyllion in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 10th, 2010, 08:24 PM
  4. Java Source/APIs to create a Fourm
    By softwarebuzz in forum Java Theory & Questions
    Replies: 2
    Last Post: January 9th, 2010, 01:46 AM
  5. Stack Order?
    By TimW in forum AWT / Java Swing
    Replies: 2
    Last Post: September 19th, 2009, 07:33 AM