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: SORTING ALGORITHMS...

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default SORTING ALGORITHMS...

    public class PAS2 {
     
    	public static void main(String[] args) {
    		int[] x = {1,4,2,6,5,3,4};
    		for(int z : modifiedselectionsort(x)){
    			System.out.print(z + "\t");
    		}
     
    	public static int[] modifiedselectionsort(int[] x){
    		for(int i = 0; i < x.length/2; i++){
    			int min = x[i];
    			int max = x[i];
    			int minindex = i;
    			int maxindex = i;
    			for(int j = i+1; j < x.length-i; j++){
    				if(x[j] < min){
    					min = x[j];
    					minindex = j;
    				}
    				if(x[j] >= max){
    					max = x[j];
    					maxindex = j;
    				}
    			}
    			[SIZE=4][FONT=Comic Sans MS]if (maxindex == i){
    				maxindex = minindex; // Algorithm's approach to max at beginning
    			}[/FONT][/SIZE]
    			int temp1 = x[i];
    			int temp2 = x[x.length-1-i];
    			x[i] = min;
    			x[x.length-1-i] = max;
    			x[minindex] = temp1;
    			x[maxindex] = temp2;
    		}
    		return x;
    	}
    }
    Now the whole problem is with that IF STATEMENT...
    if u don't use it and put {6,4,5} u will get 566 which is wrong but when u add that if statement (modification to adapt maximum numbers at 1st) and presumably everything is fine now but when I use an array like the one am using in this code u get 123381...
    ANYBODY EXPLAIN WHY? ;/


  2. #2

    Default Re: SORTING ALGORITHMS...

    First, read this on Selection Sort: Selection sort - Wikipedia, the free encyclopedia.

    Make sure you are following that algorithm.

    If the length of the arrary is n, you will make n passes. On your (i)th pass, you need to find the (i)th smallest number and insert it at index i.

    In a selection sort, the max doesn't matter, only the min matters.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: SORTING ALGORITHMS...

    or only the max matters and the min doesn't matter (depending on how you want to sort).

  4. #4
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: SORTING ALGORITHMS...

    Actually I appreciate your help both of you...
    but this is a MODIFIED SELECTION SORT
    The purpose is to find min and post it at beginning, find max and post it at the end in every pass...

Similar Threads

  1. Re: Sorting Algorithms
    By shashiwagh in forum Java Theory & Questions
    Replies: 1
    Last Post: March 23rd, 2011, 03:19 PM
  2. Sorting Algorithms
    By Dalisra in forum Java Programming Tutorials
    Replies: 1
    Last Post: November 10th, 2009, 09:24 PM
  3. Sorting Algorithms
    By Dalisra in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: November 10th, 2009, 09:24 PM
  4. Logic to implement a program to display decimal place of a number
    By chronoz13 in forum Algorithms & Recursion
    Replies: 2
    Last Post: June 11th, 2009, 04:53 AM
  5. Java algorithm for bank program to connect database
    By araujo3rd in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 10th, 2008, 01:34 PM