Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Getting the same answer for selection sort

I have written a program for selection sort(I think).But I get the input I entered as the output. Please help me with what is wrong?

Code:
```package selecsort;
import java.io.*;
import java.util.*;

public class Selecsort {
int i,n,result;
int arr[]=new int[20];
void get()
{
Scanner rd=new Scanner(System.in);
System.out.println("Enter the number of elements");
n=rd.nextInt();
System.out.println("Enter the elements of the array");
for(i=0;i<n;i++)
arr[i]=rd.nextInt();
}
int biggie(int consize)
{
int max = 0;
int max1=0;
for(i=0;i<consize;i++)
{
if(arr[i]>max1)
{
max1=arr[i];
max=i;
}
}
return max;
}
void sor()
{
int j;
int consize=n;
for(j=n;j>0;j--)
{
result=biggie(consize);
swap(arr[i],arr[result]);
consize--;
}
System.out.println("The sorted array is");
for(i=0;i<n;i++)
System.out.println(+arr[i]);
}
void swap(int x,int y)
{
int temp1;
temp1=x;
x=y;
y=temp1;
}
public static void main(String[] args) {
Selecsort se=new Selecsort();
se.get();
se.sor();
}
}```
Output:
Enter the number of elements
3
Enter the elements of the array
3
45
15
The sorted array is
3
45
15

2. ## Re: Getting the same answer for selection sort

Programmers often say or think, "I always comment the code after I get it working," which is probably code for, "I never comment my code," but it's so much easier to:

1. Successfully write a program that is commented FIRST
2. Understand commented code
3. Maintain commented code
4. Help someone else fix their commented code.

To demonstrate #1, describe how your selection sort algorithm works at the beginning of your class, and then comment the execution of that algorithm in the body of your code. Then if you're still having trouble, come back with your commented code and let us know what you need help with.

Other pointers:

1. Give your classes better names - usually nouns that describe what they ARE
2. Give your methods better names, usually verbs that describe what they DO
3. Give your variables better names, usually nouns that describe what they ARE.
4. Avoid magic numbers or variables that represent magic numbers like n, consize, etc. Use array.length when possible.
5. Ask the user how many numbers, THEN create an array of the proper size (or use ArrayList, but you may not be there yet.)

You don't get extra points for economic use of the alphabet. In fact, you may lose points if being frugal causes confusion.

3. ## Re: Getting the same answer for selection sort

Welcome Dimpy333

Please use code tags when posting code, assistance can be found on the Announcements page

4. ## Re: Getting the same answer for selection sort

Hello.
A Tip: When we are getting erroneous output its better to do the trace of the program.
By taking some sample input and using a pen and pencil we should trace the program and get to the errors if any.
This way we will build code inspection and debugging skills.
There is a minor logical error in your code.
Can you find it out? You may be shocked/surprised when you notice that bug.
To make your problem easier the bug is in the sor().
You entered just three ints. Luckily you survived because you had an integer array of 20 ints. Otherwise you might have ended up with ArrayIndexOutOfBoundsException. To prove my point try entering 20 ints and see.

Syed.

5. ## Re: Getting the same answer for selection sort

```void sor()
{
int j;
int consize=n;
for(j=n;j>0;j--)
{
result=biggie(consize);
swap(arr[i],arr[result]);
consize--;
}```
When does i change?