Solved

Bubble Sort of Real Rundom Numbers

Posted on 2010-08-17
11
570 Views
Last Modified: 2013-12-15
Hi!

I am trying to create a class in Java which:

1. Creates a real random number between 0 & 20 with seed of 123456
2. Creates an array of 30000 different random numbers using the above seed
3. Using a bubble sort to identify the 10 highest elements of the array.

I am lost .... Please assist!
0
Comment
Question by:stevthym
  • 7
  • 4
11 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33454661
stevthym,

Take a deep breath! (I always find it good to get calm first *smile*) Where exactly are you lost in the process and what constraints are you working under (i.e., did your professor restrict the usage of any classes in particular or are you running out of time).

If you post the code you have thus far, I and other Experts who frequently monitor this zone I am certain will be glad to help guide you through the process. If you are getting an Exception, please post the stack trace in a code snippet. If you are not getting errors, but results are not what you expected please advise what you are getting and what you would expect.

If you don't know where to start in general, then here is a reference to the Java Tutorials which covers a lot of introductory topics in very good depth.

http://download.oracle.com/javase/tutorial/index.html

Regards,
Kevin
0
 

Author Comment

by:stevthym
ID: 33455232
Here is my program:
import java.util.Random;
public class Test {
    
    static int arr[] = new int[30000];
    
    public static void bubbleSort(int[] x) {
        int n = x.length;
        for (int pass=1; pass < n; pass++) {
            for (int i=0; i < n-pass; i++) {
                if (x[i] > x[i+1]) {
                    int temp = x[i];  x[i] = x[i+1];  x[i+1] = temp;
                }
            }
        }
    }

    public static void main(String... args) {
        Random r = new Random(123456L);     // use the seed 123456
 
        for (int i = 0 ; i < 30000; i++) {
            arr[i] = r.nextInt(20);
        }
        
        bubbleSort(arr);
        
        for (int i = 0 ; i <= 10; i++) {
            System.out.println(" " + i + " element is " + arr[i]);
            
        }
    }
}

Open in new window

0
 

Author Comment

by:stevthym
ID: 33455242
But... sorting does not work for the first 10 higher numbers and do not be sure how to:

1. Creates a real random number between 0 & 20 with seed of 123456 (i tried to do it with integers)
2. Creates an array of 30000 different random numbers using the above seed
3. Using a bubble sort to identify the 10 highest elements of the array.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33455623
Will take a look at the code and see if I can spot what is wrong. Just one note is that to get 10 records, you last for loop should be i < 10 and not i <= 10 as array item at index 10 is your 11th element.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33455686
Another quick think is might this be working and your top elements are simply 0. Haven't gone through the code, but I compiled what you had and ran it and then got to thinking that nextInt(n) will return a value between 0 (inclusive) and n (exclusive); therefore, 0 is a valid response where as 20 is not. If you are wanting 20 included by not 0 then simply nextInt(20)+1.
0
 

Author Comment

by:stevthym
ID: 33455698
should I see the top ten higer numbers of my array of 30000 numbers?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33455748
And if you are wanting the highest elements that could mean to sort the array lowest to highest integer and take the last 10 values (highest) just remembering that can be same exact number since you have 30000 entries between 0 and 20.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33455758
Try sorting the other way (i.e., x[i] < x[i+1])
0
 

Author Comment

by:stevthym
ID: 33456053
ok thanks!

how to convert the above in real numbers???
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33457553
Ah, I was wondering if you were to use actual real numbers from a mathematical sense as in sqrt(2).

Maybe double will be close. Try nextDouble()*20
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 33457572

import java.util.Random;

public class Q26408241 {
	static double arr[] = new double[30000];

	public static void bubbleSort(double[] x) {
		int n = x.length;
		for (int pass = 1; pass < n; pass++) {
			for (int i = 0; i < n - pass; i++) {
				if (x[i] < x[i + 1]) {
					double temp = x[i];
					x[i] = x[i + 1];
					x[i + 1] = temp;
				}
			}
		}
	}

	public static void main(String... args) {
		Random r = new Random(123456L); // use the seed 123456

		for (int i = 0; i < 30000; i++) {
			arr[i] = r.nextDouble()*20;
		}

		bubbleSort(arr);

		for (int i = 0; i < 10; i++) {
			System.out.println(" " + i + " element is " + arr[i]);

		}
	}
}

Open in new window

0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 77
numbers ascending pyramid 101 195
How can I build my own IDE using ASP.NET MVC? 2 45
MySqlDump not dumping triggers 1 31
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question