Getting the same answer for selection sort

• August 4th, 2013, 02:05 AM
Dimpy333
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:
Code java:

```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
• August 4th, 2013, 04:18 AM
GregBrannon
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.
• August 4th, 2013, 11:29 AM
jps
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
• August 4th, 2013, 01:53 PM
syedbhai
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.
• August 4th, 2013, 07:49 PM
Junky
Re: Getting the same answer for selection sort
Code :

```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?