# What's wrong with my code.

Page 1 of 1

## 13 Replies - 981 Views - Last Post: 28 October 2009 - 12:34 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=134987&amp;s=5585363fb0d3db48a826b66fc98754b2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 javatized

Reputation: 0
• 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

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

## Re: What's wrong with my code.

Posted 28 October 2009 - 07:36 AM

[ code]
[ /code]

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

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

### #3 javatized

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

## Re: What's wrong with my code.

Posted 28 October 2009 - 08:01 AM

joneSi, on 28 Oct, 2009 - 06:36 AM, said:

[ code]
[ /code]

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

I'm getting this result [[email protected], I dunno what's that?

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11792
• Posts: 44,315
• 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.

### #5 javatized

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

## Re: What's wrong with my code.

Posted 28 October 2009 - 08:17 AM

macosxnerd101, 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.

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11792
• Posts: 44,315
• 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].

### #7 javatized

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

## Re: What's wrong with my code.

Posted 28 October 2009 - 08:26 AM

macosxnerd101, 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.

### #8 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11792
• Posts: 44,315
• 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.

### #9 dolphin362

Reputation: -1
• 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.

### #10 javatized

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

## Re: What's wrong with my code.

Posted 28 October 2009 - 10:28 AM

dolphin362, 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.

dolphin362, 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.

### #11 dolphin362

Reputation: -1
• 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

[[email protected]
----operation complete.

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

### #12 javatized

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

## Re: What's wrong with my code.

Posted 28 October 2009 - 10:51 AM

dolphin362, on 28 Oct, 2009 - 09:39 AM, said:

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

[[email protected]
----operation complete.

yes

### #13 dolphin362

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

## Re: What's wrong with my code.

Posted 28 October 2009 - 10:58 AM

if you able to run this program.

### #14 sakshamkum

Reputation: 19
• 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