473,387 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Bubblesort

hello,

I am having trouble with a program. It seems easy, but for some reason I am just not getting it. I need to create an array of random numbers and call a bubblesort method that I create myself. I then need to print the sorted array. I created the random array but I cant seem to figure out the sort method, which needs to follow in descending order. This is what I have:

Expand|Select|Wrap|Line Numbers
  1.  public static int[] sorting(int vals[])
  2.   {
  3.    int i,j;
  4.    int max[] = new int [vals.length];
  5.  
  6.    System.arraycopy(vals,0,max,0,vals.length);
  7.  
  8.    for (i = 1; i < vals.length; i++)
  9.       {
  10.        for (j = 1; j < max.length; j++)
  11.          {
  12.            if (vals[j-1] > vals[j])
  13.             {
  14.              max[j-1] = vals[j];
  15.              max[j] = vals[j-1];
  16.             }
  17.          }
  18.       }
  19.   return max;
  20.  }
  21. }
The message I am getting is the random numbers (the number of numbers is defined by the user and i printed the numbers just to see if it worked) along with the message [I@1eed786.

Please help,
Thanks
Apr 12 '08 #1
16 2378
JosAH
11,448 Expert 8TB
You have two problems:

1) You are trying to print an array and get this: [I@1eed786; that's Java's way of
printing arrays: [ == array, I == int ,1ee786 == the hash code of the array. If you
want to print the contents of an array read the API documentation of the Arrays class.

2) Your bubble sort algorithm is like a Swedish Nilfisk vacuum cleaner: it s*cks
big times: it starts off nice by making a copy of the array but then you should
use *only* the copy for the comparisons and swaps.

kind regards,

Jos
Apr 12 '08 #2
hsn
237 100+
i woul prefer if you post the hole code so i can test it.
i am not sure, but i think there is something with the sort algorithm (not sure). the i loop is good but with the j loop why dont you start with the last index of the array and go backward. it would be ;better.
please post the hole code so i can test it.

P.S. PLEASE USE THE CODE TAGS

hsn
Apr 12 '08 #3
hsn
237 100+
a tip to save time. in the i loop and the j loop you are calling the length of the array in every time it loops. it is better to store the size in a variable and use the variable in the loop. you wont feel the difference. but trust me it matters.

good luck

hsn
Apr 12 '08 #4
[HTML]
import java.io.*;

public class Bubble
{
public static void main(String[] args)
throws java.io.IOException
{
String s1, convert;
int i, num;

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

do
{
System.out.print("How many random numbers? ");

s1 = br.readLine();

num = Integer.parseInt(s1);

if (num > 9999 || num < 0)
System.out.println("Not valid.\n");
}
while(num > 9999 || num < 0);

System.out.println();

int mkrand[] = new int [num];

for(i = 0; i < num; i++)
{
mkrand[i] = 1 + (int)(Math.random()* 101);
System.out.print(mkrand[i] + " ");
}
System.out.print(sort(mkrand));

}
public static int[] sort(int vals[])
{
int i,j;
int max[] = new int [vals.length];

System.arraycopy(vals,0,max,0,vals.length);

for (i = 1; i < vals.length; i++)
{
for (j = 1; j < max.length; j++)
{
if (vals[j-1] > vals[j])
{
max[j-1] = vals[j];
max[j] = vals[j-1];
}
}
}
return max;
}
}[/HTML]


Here it is
Apr 12 '08 #5
You have two problems:

1) You are trying to print an array and get this: [I@1eed786; that's Java's way of
printing arrays: [ == array, I == int ,1ee786 == the hash code of the array. If you
want to print the contents of an array read the API documentation of the Arrays class.

2) Your bubble sort algorithm is like a Swedish Nilfisk vacuum cleaner: it s*cks
big times: it starts off nice by making a copy of the array but then you should
use *only* the copy for the comparisons and swaps.

kind regards,

Jos
I am not sure what you mean by API documentation. As you may tell, I'm pretty new to this stuff.
Apr 12 '08 #6
JosAH
11,448 Expert 8TB
I am not sure what you mean by API documentation. As you may tell, I'm pretty new to this stuff.
API == Application Program Interface; the docs describe all the classes and their
methods that come with your Java distribution. You should use them whereever
you can and not reinvent the wheel. Here's a link.

kind regards,

Jos
Apr 12 '08 #7
hsn
237 100+
i am shocked that you don't know about the API. it is the first thing i have learned in java. sownload it, it will help you alot..


hsn
Apr 12 '08 #8
i am shocked that you don't know about the API. it is the first thing i have learned in java. sownload it, it will help you alot..


hsn
Ok, I'm on it. But did you see any thing else wrong with my code?
Apr 12 '08 #9
hsn
237 100+
hello m8.
as Jos said your bubble sort algorithm ******. it is not correct for
88 23 70 6 it gave me
23 88 6 70
you should do some reading about this algorithm and understand it well. do some search and you will understand it it is one of the simplest algorithms.
and you can't say
Expand|Select|Wrap|Line Numbers
  1. System.out.print(sort(mkrand));
you have to go in a loop to print the array.

Good luck

hsn
Apr 12 '08 #10
JosAH
11,448 Expert 8TB
you have to go in a loop to print the array.
No he doesn't; that's what me fIrst remark was about (see my first reply).
He could simply do this:

Expand|Select|Wrap|Line Numbers
  1. System.out.println(Arrays.toString(hisArray));
  2.  
kind regards,

Jos
Apr 12 '08 #11
hsn
237 100+
No he doesn't; that's what me fIrst remark was about (see my first reply).
He could simply do this:

Expand|Select|Wrap|Line Numbers
  1. System.out.println(Arrays.toString(hisArray));
  2.  
kind regards,

Jos
sorry m8 i didn't see that one
Apr 12 '08 #12
sorry m8 i didn't see that one
I tried this:

System.out.println(Arrays.toString(sorting(mkrand) ))

but I got the error message saying that it cannot find symbol
symbol: variable Arrays
Apr 13 '08 #13
I tried this:

System.out.println(Arrays.toString(sorting(mkrand) ))

but I got the error message saying that it cannot find symbol
symbol: variable Arrays
Oh, I also created a new a new selection sort method:
[HTML]
public static int[] sorting(int vals[])
{
for (int i = 0; i < vals.length; i++)
{
int index = i;
for (int j = i+1; j < vals.length; j++)
if (vals[j] > vals[index])
index = j;

int largerNumber = vals[index];
vals[index] = vals[i];
vals[i] = largerNumber;
}
return vals;
}[/HTML]

Can you tell me if this will give me the desired results? I still can't figure out how to print the array. I tried what was suggested but it still did not work.
Apr 13 '08 #14
JosAH
11,448 Expert 8TB
I still can't figure out how to print the array. I tried what was suggested but it still did not work.
You have to import that class, just like every class not in the package java.lang.
This is how you do that: before you define any class add the line:

Expand|Select|Wrap|Line Numbers
  1. java.util.Arrays;
  2.  
How did I know what package this Arrrays class is in? By reading the API docs.

kind regards,

Jos
Apr 13 '08 #15
You have to import that class, just like every class not in the package java.lang.
This is how you do that: before you define any class add the line:

Expand|Select|Wrap|Line Numbers
  1. java.util.Arrays;
  2.  
How did I know what package this Arrrays class is in? By reading the API docs.

kind regards,

Jos
Thanks.

I did read it but I never found the part about converting to a String.
Apr 13 '08 #16
JosAH
11,448 Expert 8TB
Thanks.

I did read it but I never found the part about converting to a String.
But in reply #13 you wrote:

I tried this:

System.out.println(Arrays.toString(sorting(mkrand) ))

but I got the error message saying that it cannot find symbol
symbol: variable Arrays
That simply indicates that the compiler couldn't find the 'Arrays' symbol, i.e. it
couldn't check whether or not there even *was* a method named toString(). That
should've fired up your attention: when a class symbol can't be found you either
misspelt its name or you have forgotten to import it.

kind regards,

Jos
Apr 13 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: kathy | last post by:
I have posted my problem before and still not feel confused. Anyone know what is the problem? In my Win32 dll, I defined: #ifdef DllExport #define UNMANAGED_API __declspec(dllexport) #else...
0
by: magic9baller | last post by:
I have to use bubblesort to sort a singly linked list. (Yes I am a n00b.) The tricky part is that I have to move pointers rather than moving the contents of one node to the other. I think my problem...
20
JavaStudent07
by: JavaStudent07 | last post by:
If you know anything about "Bubble Sorts" please leave it here, the teacher said, "Use bubble sort to sort numbers into decreasing numerical value." Then she started eating lunch and said we were on...
11
by: Trent | last post by:
Running this I see that on first run, both bubble and selection sort have 9 sort counts while insertion sort has ZERO. With a sorted list, should this be ZERO for all? Also bsort and Ssort have...
4
by: Trent | last post by:
Still have problems with this thing. Seems my results are not matching the "correct" example given. The three sets of numbers below the last 3 columns is suppose to be the number of comparisons...
2
by: AZRebelCowgirl73 | last post by:
Ok I think I have added the bubbleSort methods correctly I am not getting any errors and the program runs as if I had not added them yet! My question is this! how would I now in the main method...
7
beacon
by: beacon | last post by:
I'm writing a program as an assignment that takes 5 sorting algorithms and and tests for the amount of time and the number of comparisons it takes to um, sort an array. I have run into some...
8
by: MLH | last post by:
Is there any built-in FN for returning largest value in a group of say 2 - 10 values? Lets say I want a FN something like MaxVal(5, 7, 9, 3, 12) or perhaps MaxVal(79,34). I would wanna see return...
3
by: natvarara | last post by:
#include <stdio.h> #include <string.h> #include <conio.h> #include <ctype.h> #define MAX 30 void getData (FILE*,int,char,int,int,float,float,int,int*); void...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.