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 4 of 4

Thread: help With Java SelectionSort numbers to display ascending and descending Order.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy help With Java SelectionSort numbers to display ascending and descending Order.

    help With Java SelectionSort numbers to display ascending and descending Order.

    This is the program _ i want to put this in descending order,

    Pls help me as soon as possible....

    // program to demonstrate the selection sort
    class SelectionSort
    {
    	// method to display the contents of an array
    	static void displayData(int[] data)	
      {
    		for (int index=0; index != data.length; index++)
    			System.out.println(data[index]+"\t");
    	}
     
      static int positionOfLargest(int[] data, int limit)
      {
        // method to return the position of the largest item 
        // in the data with bounds 0..limit
     
      	int largest = data[0];
      	int indexOfLargest = 0;
     
      	for (int index=1; index <= limit; index++)
          {
      		if (data[index]> largest)	
                {
      			largest = data[index];
      			indexOfLargest = index;
      		}
      	}
      	return indexOfLargest;
      }
     
      public static void selectionSort(int[] data)
      {
        // method to sort the contents of an data into 
        // scending order
     
      	int temporary;
      	int position;
      	int size=data.length;
     
      	for (int index=size-1; index > 0; index--)
          {
      		position=positionOfLargest(data, index);
     
      		// swap numbers
      		if (index != position)
                {
      			temporary = data[index];
      			data[index] = data[position];
      			data[position] = temporary;
    		}
      	}
      }
     
    	public static void main(String[] args)	
         {
    		int[] data = {18,7,15,8,13};
     
    		System.out.println("numbers before being sorted\n");
    		displayData(data);
     
    		selectionSort(data);
     
    		System.out.println("numbers after being sorted\n");
    		displayData(data);
    	}
    }

    Change the sort in descending Order?
    Last edited by helloworld922; October 21st, 2011 at 09:28 AM.


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    71
    My Mood
    Cheerful
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: help With Java SelectionSort numbers to display ascending and descending Order.

    use [highlight = java] your code here [/highlight]
    for posting you code! please

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    71
    My Mood
    Cheerful
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: help With Java SelectionSort numbers to display ascending and descending Order.

    Change the sort in descending Order?
    it is very easy!
    here is the solution!!

    just change this
    if (data[index]> largest)
    with
    if (data[index]< largest)
    is your problem solved?
    regards

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Re: help With Java SelectionSort numbers to display ascending and descending Order.

    i'm very very new to this.


    IT'S SOLVED !!!!!!!!!!! THANK YOU VERY MUCH

    HOPE U'LL HELP ME IN OTHER PROGS' TOOO




Similar Threads

  1. BPEL -> WSDL -> JAVA -> JSP How do I do it? In which order?
    By x_maras in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: June 12th, 2011, 07:21 AM
  2. Replies: 3
    Last Post: June 1st, 2011, 12:47 AM
  3. Display prime numbers from 100 to 200 in Java
    By c.P.u1 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 25th, 2011, 02:14 PM
  4. Replies: 4
    Last Post: November 14th, 2010, 10:44 AM
  5. sorting name using Selectionsort
    By asdfg in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 20th, 2010, 09:44 AM

Tags for this Thread