Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jasonbot

macrumors 68020
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
heres what i got so far... i dont quite have arrays down so far so thats whats my problem right now... i haven't got to sorting it out yet but i am first trying to generate n amount of numbers between 0 - 100... what am i doing wrong?

Code:
import java.io.*;
public class Sorting
{
        public static void main(String[] args)
        throws java.io.IOException
        {

                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

                String h;
                int i;

                do
                {
                        System.out.print("How many numbers am I gonna sort? ");
                        h = br.readLine();

                        i = Integer.parseInt(h);
                }
                while(i >= 10000 || (i % 2.0) == 1);

                int order[] = new int[i];
                order = (int[]) (Math.random() * 101);

                System.out.println(order);
        }
}

Your array isn't even in the loop. Create a for loop using I as a counter then put order=(int)(Math.random()*101); inside the for loop. This will create and put your 100 random numbers in your array. As a beginner coder myself I like your input stream reader
 

Gelfin

macrumors 68020
Sep 18, 2001
2,165
5
Denver, CO
A few notes...

1. Are you sure you are not allowed to use the built-in array sort method to accomplish this? If you're in an introductory Java class and you walk in having implemented your own bubble sort when that isn't a requirement, your instructor is going to suspect you've plagiarized some code from somewhere.

2. Is it a requirement of your assignment that the length of your array is an even number under 10000? I only ask because it's what your code does, and you didn't mention it.

3. Your input value test accepts zero or negative numbers. Since these do not make sense for an array length, it probably isn't what you want.

4. If your user puts in an invalid value, he just sees the same prompt again, with no indication why. Provide more information upfront about the restrictions on possible input values, and if you can, tell the user that his input was not valid before prompting him again.

5. Your prompt is worded using a very casual tone. Some instructors will care. Later on, all employers will care. Be formal.

6. You printed your original array, albeit with linebreaks between numbers, but then later you said you don't know how to print your sorted array. This confuses me. What difference do you think is relevant?
 

Gelfin

macrumors 68020
Sep 18, 2001
2,165
5
Denver, CO
Clarifying the assignment fixes most of my concerns except:

1. Your prompt says "gonna" where the specified prompt says "going to". If I were your very strict professor, I'd dock points for that.

2. Your code accepts negative numbers, which the specification forbids, and rejects positive odd numbers, which the specification does not mention. Have the painkillers led you to confuse "positive" with "even"? Understandable in a way, but you should fix this.

BONUS: Don't worry about this if you're short on time, because the specification doesn't say to explicitly, but if you have time to go for suckup points, when the user inputs something invalid tell him why it's invalid before displaying the specified prompt again.

I don't see anything immediately wrong with your bubble sort, but I haven't run it or checked it thoroughly. I'm just assuming it works. You shouldn't.

Conceptually, a bubble sort is more or less the simplest kind of sort you can do. It does several passes over the array. There isn't any need to break the algorithm up this way in implementation, but it makes more sense to think of the inner loop as its own function, "pushBiggestNumberToEnd." That's all it does, and it does it by checking each number against its immediate neighbor to the right. The bigger number always ends up to the right of the smaller number. In this way the big number floats out to the position you want it in.

The outer loop simply calls pushBiggestNumberToEnd on a shrinking subset of the entire array. The first pass operates on the entire array, [0..n]. Once the first pass is done, you know the last number in the array (at n) is in the right position, so you can ignore it.

The next pass calls pushBiggestNumberToEnd on the subset of the array [0..n-1]. This has the effect of pushing the second biggest number in the array to the position n-1. This is the right position for that number, so the outer loop now runs on [0..n-2] and so forth until it has sorted the entire array.

Selection Sort is another simple sort algorithm that works sort of like an inside-out version of bubble sort. The inner "function" in the selection sort would be "swapSmallestWithFirst." This function examines each element in an array (or portion of an array) and keeps track of the location of the smallest number it encounters. When it reaches the end of the array, it swaps the number at the remembered location with the number at the first location in the array. At the end of a pass, you know that the first number in the portion of the array you're working on is the smallest number in the entire portion.

Thus the outer loop in Selection Sort behaves like in Bubble Sort, except it shrinks at the small end instead of the big end. It puts the correct number into position 0, then position 1 and so forth until it has sorted the entire array.
 

Jasonbot

macrumors 68020
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
Code:
//

import java.util.*;

public class srt {

    public static void main (String args[]) {
        int a [] ={12,3,4,345,123,13,45,123123,43,1};
		int x;
		
		str.sort(a[1]);
		
		do{
			i++;
			System.out.println( a [i]);
			x= a[i];
		}while (x!=null);
			
    }
	public static void sort(int[] a, int a[0], int a[10]){
		
	}
}

sorry to interrupt the thread, where is my sort messing up?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.