13 Replies - 711 Views - Last Post: 28 October 2009 - 12:34 PM Rate Topic: -----

#1 javatized  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-October 09

What's wrong with my code.

Posted 28 October 2009 - 06:32 AM

You are required to implement a sorting algorithm which is a variation of index sort.
In this algorithm, you compute the index that each
integer will have in the sorted array. To do so, you construct an array of indices with
each element set initially to zero. Then you loop through the integers in the array, in-
crementing the index of larger integers by one.
For example, you have the following array:
3 2 1 5 4
In the rst pass, we will increment each index holding a value greater than 3. After
nishing the rst pass, the indices array will be equal to:
0 0 0 1 1
After nishing the second pass, the indices array will be equal to:
1 0 0 2 2
After the nth pass, the indices array will be equal to:
2 1 0 4 3


what's wrong with this.

public class indexSort{


public static int[] indexa(int[] x){
int[] y;
y = new int[x.length];

for(int i=0;i<x.length;i++){

for(int j=1;j<x.length;j++){
if((x[i]<x[j]))
{
y[j]=y[j]+1;

}

}

}
return y;
}

public static void main(String[] args) {
int [] x = {3,2,0,5,4};

System.out.print(indexa(x));



}

}

This post has been edited by javatized: 28 October 2009 - 07:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: What's wrong with my code.

#2 joneSi  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 35
  • Joined: 21-August 08

Re: What's wrong with my code.

Posted 28 October 2009 - 07:36 AM

1) Please post your code between code tags:

[ code]
PASTE YOUR CODE HERE
[ /code]


2) Please describe in detail what you think the problem is.

Thank you for helping us help you. :)

This post has been edited by joneSi: 28 October 2009 - 07:41 AM

Was This Post Helpful? 0
  • +
  • -

#3 javatized  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-October 09

Re: What's wrong with my code.

Posted 28 October 2009 - 08:01 AM

View PostjoneSi, on 28 Oct, 2009 - 06:36 AM, said:

1) Please post your code between code tags:

[ code]
PASTE YOUR CODE HERE
[ /code]


2) Please describe in detail what you think the problem is.

Thank you for helping us help you. :)



I'm getting this result [I@150bd4d, I dunno what's that?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,858
  • Joined: 27-December 08

Re: What's wrong with my code.

Posted 28 October 2009 - 08:07 AM

You need to iterate through the array and print out each individual element. When you try to print array x, it will display the memory adress, which is the result you are currently getting.
Was This Post Helpful? 0
  • +
  • -

#5 javatized  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-October 09

Re: What's wrong with my code.

Posted 28 October 2009 - 08:17 AM

View Postmacosxnerd101, on 28 Oct, 2009 - 07:07 AM, said:

You need to iterate through the array and print out each individual element. When you try to print array x, it will display the memory adress, which is the result you are currently getting.

public static void main(String[] args) {
int [] x = {3,2,0,5,4};
for (int i=0;i<x.length;i++){
System.out.print(indexa(x));


do you mean like this?
it prints out five memory locations.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,858
  • Joined: 27-December 08

Re: What's wrong with my code.

Posted 28 October 2009 - 08:21 AM

It does this because you are printing x instead of x[i].
Was This Post Helpful? 0
  • +
  • -

#7 javatized  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-October 09

Re: What's wrong with my code.

Posted 28 October 2009 - 08:26 AM

View Postmacosxnerd101, on 28 Oct, 2009 - 07:21 AM, said:

It does this because you are printing x instead of x[i].



I don't want to print x, I want to print y, x is the input.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,858
  • Joined: 27-December 08

Re: What's wrong with my code.

Posted 28 October 2009 - 08:30 AM

The method indexa() returns an array. Regardless of the array name, printing an array will print out the memory location. You should consider maybe using a temp array to handle the return value from indexa(). Then think about printing each element.
Was This Post Helpful? 0
  • +
  • -

#9 dolphin362  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 7
  • Joined: 27-October 09

Re: What's wrong with my code.

Posted 28 October 2009 - 10:23 AM

I got solution:

----exec: java Program

32154
21043

----operation complete.

My coding is different with your source code.
And, I am using array too.
Was This Post Helpful? 0
  • +
  • -

#10 javatized  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-October 09

Re: What's wrong with my code.

Posted 28 October 2009 - 10:28 AM

View Postdolphin362, on 28 Oct, 2009 - 09:23 AM, said:

I got solution:

----exec: java Program

32154
21043

----operation complete.

My coding is different with your source code.
And, I am using array too.



I just don't know hoe to print y instead of the mamory location.

View Postdolphin362, on 28 Oct, 2009 - 09:23 AM, said:

I got solution:

----exec: java Program

32154
21043

----operation complete.

My coding is different with your source code.
And, I am using array too.



I just don't know hoe to print y instead of the memory location.
Was This Post Helpful? 0
  • +
  • -

#11 dolphin362  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 7
  • Joined: 27-October 09

Re: What's wrong with my code.

Posted 28 October 2009 - 10:39 AM

Yours output is:
----exec: java indexSort

[I@10b62c9
----operation complete.

This post has been edited by dolphin362: 28 October 2009 - 10:39 AM

Was This Post Helpful? 0
  • +
  • -

#12 javatized  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-October 09

Re: What's wrong with my code.

Posted 28 October 2009 - 10:51 AM

View Postdolphin362, on 28 Oct, 2009 - 09:39 AM, said:

Yours output is:
----exec: java indexSort

[I@10b62c9
----operation complete.


yes
Was This Post Helpful? 0
  • +
  • -

#13 dolphin362  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 7
  • Joined: 27-October 09

Re: What's wrong with my code.

Posted 28 October 2009 - 10:58 AM

You wont get correct answer,
if you able to run this program.
Was This Post Helpful? 0
  • +
  • -

#14 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: What's wrong with my code.

Posted 28 October 2009 - 12:34 PM

//in your main()
int x[]=(3,2,0,5,4};
//first print orignal
for(int i=0;i<5;i++)
{
System.out.print(x[i]+" ");
}
System.out.println();
//now change
x=indexa(x);
//now print it
for(int i=0;i<5;i++)
{
System.out.print(x[i]+" ");
}
System.out.println();


This post has been edited by sakshamkum: 28 October 2009 - 12:35 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1