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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Getting the same answer for selection sort

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by jps; August 4th, 2013 at 11:11 PM. Reason: code tags


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Getting the same answer for selection sort

    Please post your properly indented code in code tags.

    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. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default 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. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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?
    Improving the world one idiot at a time!

Similar Threads

  1. Java selection sort
    By maple1100 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 29th, 2013, 09:13 PM
  2. optimized selection sort
    By IHeartProgramming in forum Algorithms & Recursion
    Replies: 3
    Last Post: December 9th, 2012, 08:02 PM
  3. [SOLVED] Selection sort not working
    By killer3p0 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 7th, 2012, 12:24 PM
  4. Selection sort code needs reverse
    By steel55677 in forum Algorithms & Recursion
    Replies: 7
    Last Post: September 27th, 2011, 06:40 AM
  5. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM

Tags for this Thread