12 Replies - 19686 Views - Last Post: 21 November 2008 - 01:03 PM Rate Topic: -----

#1 jnrgakono  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-January 05

Sorting Array In Java

Post icon  Posted 15 January 2005 - 03:04 AM

Am having trouble with the code of 2 java programs;
1, a program that reads frm user 12 integer numbers,store them in an array and then extracts fro this array the largest and the smallest number within it then display these numbers to the console.

2.modify this program so that it uses a vector class from the java.util.*class
Is This A Good Question/Topic? 0
  • +

Replies To: Sorting Array In Java

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1877
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Sorting Array In Java

Posted 15 January 2005 - 08:34 AM

I'm assuming you mean java not javascript, so I'll move this in to that forum.

Also, do you have any code? Unfortunatley, we can't help you unless you provide us with what you have so far or atleast the pseudo code.
Was This Post Helpful? 0
  • +
  • -

#3 geetanjalidewan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 30-October 08

Re: Sorting Array In Java

Posted 30 October 2008 - 11:02 PM

View Postskyhawk133, on 15 Jan, 2005 - 08:34 AM, said:

I'm assuming you mean java not javascript, so I'll move this in to that forum.

Also, do you have any code? Unfortunatley, we can't help you unless you provide us with what you have so far or atleast the pseudo code.

Hi , Could you help me by adding an insert sort method to my code that generates an array of 100 random integers and if possible can I know the time it took to sort?
MANY THANKS
Geetanjali
import java.util.Random;
 
public class RandomArrayToSort {
    public static void main(String[] args) {
        new RandomArrayToSort();
    }
        public void create()
        {
        Random r = new Random();
 
        // generate a uniformly distributed int random numbers
        int[] integers = new int[100];
        for (int i = 0; i < integers.length; i++) {
            integers[i] = r.nextInt();
        }
        
        for (int i : integers) {
            System.out.print(i + ", "+"\n");
        }
        System.out.println("");
 
       
    }


}



** Edit ** :code:
Was This Post Helpful? 0
  • +
  • -

#4 geetanjalidewan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 30-October 08

Re: Sorting Array In Java

Posted 30 October 2008 - 11:44 PM

Can anyone pl help.A buuble sort has been applied to an array of random generated integers array size 100 .However, the records are not coming out sorted properly.keeps saying array out of bounds error and also does not generate the correct sorted output for all terms.
Please help
Thanks
Geetanjali Dewan

import java.util.Random;
 
public class RandomArrayToSort {
    public static void main(String[] args) {
        new RandomArrayToSort();
    }
        public void create()
        {
        Random r = new Random();
 
        // generate a uniformly distributed int random numbers
        int[] integers = new int[100];
        for (int i = 0; i < integers.length; i++) {
            integers[i] = r.nextInt();
        }
        
        for (int i : integers) {
            System.out.print(i +"\n");
        }
        System.out.println("");
 
       
 int last = integers.length;
        
   for ( int i = 0 ; i < last ; i++)  
for (int j = i+1 ; j < last +1; j++) 
if (integers[i] < integers[j])
	   {
               int temp = integers[i];
		integers[i] = integers[j];
		integers[j] = temp;
	   }  
        
   

      System.out.println("Records Sorted");

     
        

}
}



** Edit ** :code:
Was This Post Helpful? 0
  • +
  • -

#5 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Sorting Array In Java

Posted 31 October 2008 - 08:44 AM

The sorting isn't correct. Change the sorting to this:

 
		for(int i=0; i<integers.length-1; i++) { 
			for(int j=0; j<integers.length-1-i; j++) { 
				if(integers[j] > integers[j+1]) {
					int temp = integers[j];
					integers[j] = integers[j+1];
					integers[j+1] = temp;
				}
			}
		}


Was This Post Helpful? 0
  • +
  • -

#6 geetanjalidewan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 30-October 08

Re: Sorting Array In Java

Posted 31 October 2008 - 09:35 AM

Thanks a ton for your time and help.Truely appreciate it.Pl guide further as the data still doesnt appear completely sorted.I made the array size 10 in my code so that I could look at the numbers easily and even then , the output was not sorted.
Im kinda desperate pleeease.Thanks again.
Geetanjali

View PostBigAnt, on 31 Oct, 2008 - 08:44 AM, said:

The sorting isn't correct. Change the sorting to this:

 
		for(int i=0; i<integers.length-1; i++) { 
			for(int j=0; j<integers.length-1-i; j++) { 
				if(integers[j] > integers[j+1]) {
					int temp = integers[j];
					integers[j] = integers[j+1];
					integers[j+1] = temp;
				}
			}
		}


Was This Post Helpful? 0
  • +
  • -

#7 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Sorting Array In Java

Posted 31 October 2008 - 09:47 AM

The sorting should work, when i run it on the following array:
-394590862
-1211847198
-624061471
962627250
-2060369180
941358999
-341520185
1927901582
-1045353644
126383582


The output of the array after the sort is:

-2060369180
-1211847198
-1045353644
-624061471
-394590862
-341520185
126383582
941358999
962627250
1927901582
Records Sorted

which is sorted correctly.
Was This Post Helpful? 0
  • +
  • -

#8 geetanjalidewan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 30-October 08

Re: Sorting Array In Java

Posted 01 November 2008 - 12:19 AM

I get your point.I have pasted your corrected sorting part in my code.When I test it for an array size less than 10 of randomly generated numbers in the array it works, but 10, 100 or 1000, the sorting doesnt.
Am I doing something wrong in the bit where I generate an array of random numbers.
I am submitting my complete code for you to see .Sorry for being so slow in understanding.
Thanks in advance
Geetanjali

My code is as below:


import java.util.Random;

public class RandomArrayToSort {
public static void main(String[] args) {
new RandomArrayToSort();
}
public void create()
{
Random r = new Random();

// generate a uniformly distributed int random numbers
int[] integers = new int[1000];
for (int i = 0; i < integers.length; i++) {
integers[i] = r.nextInt();
}

for (int i : integers) {
System.out.print(i +"\n");
}
System.out.println("");


int last = integers.length;




for(int i=0; i<integers.length-1; i++) {
for(int j=0; j<integers.length-1-i; j++) {
if(integers[j] > integers[j+1]) {
int temp = integers[j];
integers[j] = integers[j+1];
integers[j+1] = temp;
}
}
}
System.out.println("Records Sorted");




}
}



Please help.




View PostBigAnt, on 31 Oct, 2008 - 09:47 AM, said:

The sorting should work, when i run it on the following array:
-394590862
-1211847198
-624061471
962627250
-2060369180
941358999
-341520185
1927901582
-1045353644
126383582


The output of the array after the sort is:

-2060369180
-1211847198
-1045353644
-624061471
-394590862
-341520185
126383582
941358999
962627250
1927901582
Records Sorted

which is sorted correctly.

Was This Post Helpful? 0
  • +
  • -

#9 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Sorting Array In Java

Posted 01 November 2008 - 07:49 AM

I ran your updated code, and the array appears to be sorted when it is any length. I put your code so it would print out the sorted array, and it is sorted when the length is size 1000.
import java.util.Random;

public class RandomArrayToSort {
	public static void main(String[] args) {
		RandomArrayToSort theArray = new RandomArrayToSort();
		theArray.create();
	}
	public void create(){
	Random r = new Random();
	
	// generate a uniformly distributed int random numbers
	int[] integers = new int[1000];
	for (int i = 0; i < integers.length; i++) {
		integers[i] = r.nextInt();
	}
	
	for (int i : integers) {
		System.out.println(i);//System.out.print(i +"\n"); Just use the println() method as it does this for you
	}
	System.out.println(); //System.out.println("");  If you just want to print a new line the argument isn't needed
	
	
	//int last = integers.length;  //Not used, using array's length directly in loops
	
	
	
	
	for(int i=0; i<integers.length-1; i++) {
		for(int j=0; j<integers.length-1-i; j++) {
			if(integers[j] > integers[j+1]) {
				int temp = integers[j];
				integers[j] = integers[j+1];
				integers[j+1] = temp;
			}
		}
	}
	System.out.println("Records Sorted");
	
	System.out.println("\nSorted array:");
	for(int i: integers){
		System.out.println(i); 
	}
	
	
	}
}


Was This Post Helpful? 0
  • +
  • -

#10 geetanjalidewan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 30-October 08

Re: Sorting Array In Java

Posted 02 November 2008 - 06:56 AM

perfect. Thanks a million.
I get it now.
Much appreciated
Geetanjali

View PostBigAnt, on 1 Nov, 2008 - 07:49 AM, said:

I ran your updated code, and the array appears to be sorted when it is any length. I put your code so it would print out the sorted array, and it is sorted when the length is size 1000.
import java.util.Random;

public class RandomArrayToSort {
	public static void main(String[] args) {
		RandomArrayToSort theArray = new RandomArrayToSort();
		theArray.create();
	}
	public void create(){
	Random r = new Random();
	
	// generate a uniformly distributed int random numbers
	int[] integers = new int[1000];
	for (int i = 0; i < integers.length; i++) {
		integers[i] = r.nextInt();
	}
	
	for (int i : integers) {
		System.out.println(i);//System.out.print(i +"\n"); Just use the println() method as it does this for you
	}
	System.out.println(); //System.out.println("");  If you just want to print a new line the argument isn't needed
	
	
	//int last = integers.length;  //Not used, using array's length directly in loops
	
	
	
	
	for(int i=0; i<integers.length-1; i++) {
		for(int j=0; j<integers.length-1-i; j++) {
			if(integers[j] > integers[j+1]) {
				int temp = integers[j];
				integers[j] = integers[j+1];
				integers[j+1] = temp;
			}
		}
	}
	System.out.println("Records Sorted");
	
	System.out.println("\nSorted array:");
	for(int i: integers){
		System.out.println(i); 
	}
	
	
	}
}


Was This Post Helpful? 0
  • +
  • -

#11 geetanjalidewan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 30-October 08

Re: Sorting Array In Java

Posted 20 November 2008 - 09:26 PM

hI .
Its me again.The bubble sort code seemed to work perfectly on my computer all this while.
Now when I try to run it on another computer I get an error in the for loop line
for (int i : integers)
it expects me to write a ; as is written in for loops.What do I do and how come it works just linke this on my laptop?
Secondly , is it possible to know the time taken by a sort code to process when I make my array size large , say 10,0000..Is there a method I should write or should I go to control panel performance and observe ?
Pl help.
Im sure I will get the best solution to both my problems on this amazing forum.
Thanks in advance
Geetanjali






View Postgeetanjalidewan, on 2 Nov, 2008 - 05:56 AM, said:

perfect. Thanks a million.
I get it now.
Much appreciated
Geetanjali

View PostBigAnt, on 1 Nov, 2008 - 07:49 AM, said:

I ran your updated code, and the array appears to be sorted when it is any length. I put your code so it would print out the sorted array, and it is sorted when the length is size 1000.
import java.util.Random;

public class RandomArrayToSort {
	public static void main(String[] args) {
		RandomArrayToSort theArray = new RandomArrayToSort();
		theArray.create();
	}
	public void create(){
	Random r = new Random();
	
	// generate a uniformly distributed int random numbers
	int[] integers = new int[1000];
	for (int i = 0; i < integers.length; i++) {
		integers[i] = r.nextInt();
	}
	
	for (int i : integers) {
		System.out.println(i);//System.out.print(i +"\n"); Just use the println() method as it does this for you
	}
	System.out.println(); //System.out.println("");  If you just want to print a new line the argument isn't needed
	
	
	//int last = integers.length;  //Not used, using array's length directly in loops
	
	
	
	
	for(int i=0; i<integers.length-1; i++) {
		for(int j=0; j<integers.length-1-i; j++) {
			if(integers[j] > integers[j+1]) {
				int temp = integers[j];
				integers[j] = integers[j+1];
				integers[j+1] = temp;
			}
		}
	}
	System.out.println("Records Sorted");
	
	System.out.println("\nSorted array:");
	for(int i: integers){
		System.out.println(i); 
	}
	
	
	}
}


Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • Joined: 20-September 08

Re: Sorting Array In Java

Posted 21 November 2008 - 01:46 AM

Quote

Now when I try to run it on another computer I get an error in the for loop line
for (int i : integers)
it expects me to write a ; as is written in for loops.What do I do and how come it works just linke this on my laptop?


You are working with a different version of Java installed on each.

for (int i : integers)


will only work for Java >= 1.5. Use normal for loops if you want it to work on either
Was This Post Helpful? 0
  • +
  • -

#13 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Sorting Array In Java

Posted 21 November 2008 - 01:03 PM

Quote

Secondly , is it possible to know the time taken by a sort code to process when I make my array size large , say 10,0000..Is there a method I should write or should I go to control panel performance and observe ?


You could do this by calling System.currentTimeMillis() before and after the method was called and then subtracting them to get the execution time.

**Also you may want to check out a benchmarking framework such as JBench.

This post has been edited by BigAnt: 21 November 2008 - 01:07 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1