Results 1 to 11 of 11
  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Stupid Array are Stupid.

    A Java program using single dimensional array. Have a list of 10 numbers and fetch a value based on the index.
    A Java program to sort these numbers in ascending order using a bubble sort: 16, 100, 205, 8, 1, 3, 2, 5, 7, 6, 15, 10, 14
    A Java program to find the value 45.3 from this list ={-3,10,5,24,45.3,10.5} using the binary search method.

    I need some help with the bubble methods and the binary methods.

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Need some help with Arrays

    There is a method called Collections.sort() that may be used to sort arrays of enumerable objects - just deliver an ArrayList<int> of your numbers to it:
    Here is an example: Perform Binary Search on Java ArrayList Example | Java Examples - Java Program Sample Source Code

  3. #3
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Need some help with Arrays

    Quote Originally Posted by Sierra View Post
    There is a method called Collections.sort() that may be used to sort arrays of enumerable objects - just deliver an ArrayList<int> of your numbers to it:
    Here is an example: Perform Binary Search on Java ArrayList Example | Java Examples - Java Program Sample Source Code
    Sorry, that won't help, I need the methods for Arrays, not ArrayLists.

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Need some help with Arrays

    Did you try and think a bit about it?
    Did you try to replace Collections.sort with Arrays.sort or use ArrayList.toArray()?

  5. #5
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Stupid Array are Stupid.

    Java Code:
    package OOP;
    
    public class Array2 {
    	int i, j, t;
    	public static void main(String [] args){
    	int a[] = {16, 100, 205, 8, 1, 3, 2, 5, 7, 6, 15, 10, 14};
    	System.out.println("Before sort:");
    	
    		for(int i = 0; i < a.length; i++){
    		System.out.println(a[i]);
    		}
    	System.out.println("Accending order:");
    	for(j = 1; j< a.length; j++){
    		for(i = 0; i<a.length - j; i++){
    			if (n[i] >n[i+1]){
    				int temp = n[i];
    				n[i] = n[i+1];
    				n[i +1] = temp;
    			}
    		}
    	}
    	}
    }
    Trying to do a stupid bubble sort but this shit wont work.

    -EDIT: FIXED
    Last edited by Army; 04-13-2012 at 04:52 PM.

  6. #6
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Stupid Array are Stupid.

    Pseudocode for bubblesort:
    Java Code:
    bubbleSort(Array A)
      for (n=A.size; n>1; n=n-1)
        for (i=0; i<n-1; i=i+1)
          if (A[i] > A[i+1])
            A.swap(i, i+1)
    Guess you went the wrong way down - hope that helps... ;)

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Stupid Array are Stupid.

    Please print the results of your sort. Use the Arrays toString() method to format the array for printing.
    S.o.println("array=" + Arrays.toString(<THENAMEOfARRAY>));
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Binary Search Method

    A Java program to find the value 45.3 from this list ={-3,10,5,24,45.3,10.5} using the binary search method.

    Everything I do come up in errors, can someone just send me something that I can take to make this easier.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: Binary Search Method

    That list is not in any particular order; a prerequisite for the binary search algorithm.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Binary Search Method

    This is the third thread you have opened today on that topic... maybe you should rethink this behaviour and solve it in your original thread.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Binary Search Method

    Quote Originally Posted by Sierra View Post
    This is the third thread you have opened today on that topic... maybe you should rethink this behaviour and solve it in your original thread.
    I agree. I'm merging the three threads.

    Army, please don't do this again. It's extremely rude to abandon threads without even replying to the responses and ask the same question again in another thread.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Stupid mistake, maybe?
    By knox in forum New To Java
    Replies: 1
    Last Post: 11-10-2011, 09:39 AM
  2. I need an idea with something really stupid
    By blf_titi in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-27-2010, 04:24 AM
  3. Stupid error
    By dewitrydan in forum Java Applets
    Replies: 3
    Last Post: 08-09-2010, 02:29 PM
  4. incredibally stupid....
    By tek0011 in forum New To Java
    Replies: 7
    Last Post: 11-26-2009, 10:35 AM
  5. a really stupid question
    By SwinGirl in forum NetBeans
    Replies: 10
    Last Post: 06-25-2008, 10:06 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
  •