Selection Sort problem

selection sort - array

Page 1 of 1

14 Replies - 1360 Views - Last Post: 01 November 2009 - 05:51 PM Rate Topic: -----

#1 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Selection Sort problem

Posted 28 October 2009 - 09:16 PM

Where am i going wrong? this is baffling me!

public class selections {
	
	private void selectsort(int[] a){
		int large = 0;
		int length = a.length-1;
		int temp;
		for(int i = 0; i < a.length; i++){
			while(length > 0){
				if(large > a[i]){
					large = a[i];
				}
				length--;
				
			}
			temp = a[i];
			a[i] = a[length];
			a[length] = temp;
			
		}
		for(int b = 0; b < a.length; b++){
			System.out.print(a[b] + " ");
		}
	}
	
	public static void main(String[] args) {
		
		int[] a = { 23, 45, 12, 20, 10, 4, 46 };
		
		selections ss = new selections();
		ss.selectsort(a);

	}

}

This post has been edited by javabrijon: 29 October 2009 - 02:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Selection Sort problem

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Selection Sort problem

Posted 28 October 2009 - 09:29 PM

Your large variable is set wrong. And your swap is wrong.

This post has been edited by Momerath: 28 October 2009 - 09:32 PM

Was This Post Helpful? 0
  • +
  • -

#3 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Re: Selection Sort problem

Posted 28 October 2009 - 10:37 PM

View PostMomerath, on 28 Oct, 2009 - 08:29 PM, said:

Your large variable is set wrong. And your swap is wrong.


should it be if(a[i] > large) { large = a[i];

how is the swap wrong?
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Selection Sort problem

Posted 28 October 2009 - 11:49 PM

View Postjavabrijon, on 28 Oct, 2009 - 09:37 PM, said:

should it be if(a[i] > large) { large = a[i];

What is large the 2nd time through the outer loop?

View Postjavabrijon, on 28 Oct, 2009 - 09:37 PM, said:

how is the swap wrong?
What value does i and length have when you do the swap? Try pretending you are the computer and run through each line of code recording what each variable is at each line. Use the numbers 2, 3, 1 for your array to sort.
Was This Post Helpful? 0
  • +
  • -

#5 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Re: Selection Sort problem

Posted 29 October 2009 - 01:27 AM

View Postjavabrijon, on 28 Oct, 2009 - 09:37 PM, said:

View PostMomerath, on 28 Oct, 2009 - 08:29 PM, said:

Your large variable is set wrong. And your swap is wrong.


should it be if(a[i] > large) { large = a[i];

how is the swap wrong?



definetly not getting this!..
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Selection Sort problem

Posted 29 October 2009 - 06:33 AM

Basically, Selection Sort works by finding the maximum value in the list and swapping it with the last value. It then looks at the list, excluding the last element, repeating this process until it has reached the first element. If I was you, I would start your outer for loop at the end of the array, decrementing to 0; and change your inner loop to a for loop, iterating from 0 to length-i-1. From there, find the max element and store its index in the max field and go from there.

I actually wrote a snippet on this if you want to check it out!

This post has been edited by macosxnerd101: 29 October 2009 - 06:35 AM

Was This Post Helpful? 0
  • +
  • -

#7 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Re: Selection Sort problem

Posted 29 October 2009 - 03:24 PM

View Postmacosxnerd101, on 29 Oct, 2009 - 05:33 AM, said:

Basically, Selection Sort works by finding the maximum value in the list and swapping it with the last value. It then looks at the list, excluding the last element, repeating this process until it has reached the first element. If I was you, I would start your outer for loop at the end of the array, decrementing to 0; and change your inner loop to a for loop, iterating from 0 to length-i-1. From there, find the max element and store its index in the max field and go from there.

I actually wrote a snippet on this if you want to check it out!

where can i find the snippet?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Selection Sort problem

Posted 29 October 2009 - 04:13 PM

Hit the link under my name says My Contributions. It will take you to a page with a list of snippets I've submitted. Selection Sort is there.
Was This Post Helpful? 1
  • +
  • -

#9 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Re: Selection Sort problem

Posted 29 October 2009 - 05:03 PM

View Postmacosxnerd101, on 29 Oct, 2009 - 03:13 PM, said:

Hit the link under my name says My Contributions. It will take you to a page with a list of snippets I've submitted. Selection Sort is there.


if you didnt see i commented on your selection sort ..just mainly needto know what "i" is in the first loop.
for(max = 1, n = x.length-1; i < n; n--){
		 for(int j = 0; j < n; j++){
			  if(x[j] > x[max]) max = j;
			  }
		 int temp = x[max];
		 x[max] = x[n];
		 x[n] = temp;  
	  }


Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Selection Sort problem

Posted 29 October 2009 - 05:06 PM

Take another look at the snippet. :)
Was This Post Helpful? 0
  • +
  • -

#11 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Re: Selection Sort problem

Posted 29 October 2009 - 05:32 PM

View Postmacosxnerd101, on 29 Oct, 2009 - 04:06 PM, said:

Take another look at the snippet. :)


i get an "i is unresolved" message on eclipse;

for(max = 1, n = x.length-1; i < n; n--){   
		 ...  
	  }

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Selection Sort problem

Posted 29 October 2009 - 05:35 PM

It appears I made a mistake when submitting it a couple months ago, and it got past inspection (which it probably shouldn't have). I've revised it. Sorry for the inconvenience! :)
public void selectionSort(int[] x){
   for(int max = 1, n = x.length-1; n > 0; n--){
		 for(int j = 0; j < n; j++){
			  if(x[j] > x[max]) max = j;
			  }
		 int temp = x[max];
		 x[max] = x[n];
		 x[n] = temp;  
	  }
	  
}

Was This Post Helpful? 0
  • +
  • -

#13 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Re: Selection Sort problem

Posted 29 October 2009 - 06:39 PM

View Postmacosxnerd101, on 29 Oct, 2009 - 04:35 PM, said:

It appears I made a mistake when submitting it a couple months ago, and it got past inspection (which it probably shouldn't have). I've revised it. Sorry for the inconvenience! :)
public void selectionSort(int[] x){
   for(int max = 1, n = x.length-1; n > 0; n--){
		 for(int j = 0; j < n; j++){
			  if(x[j] > x[max]) max = j;
			  }
		 int temp = x[max];
		 x[max] = x[n];
		 x[n] = temp;  
	  }
	  
}

:) all good.

 public class selections {
	
	private void selectsort(int[] a){
		int max;
		int length;
		int temp;
		for(max = 1, length = a.length-1;  length > 0; length--){
			for(int j = 0; j < length; j++){
				if(a[j] > a[max]){
					max = j;
				}
			}
			temp = a[max];
			a[max] = a[length];
			a[length] = temp;
			
		}
		for(int b = 0; b < a.length; b++){
			System.out.print(a[b] + " ");
		}
	}
	
	public static void main(String[] args) {
		
		int[] a = { 23, 45, 12, 20 };
		
		selections ss = new selections();
		ss.selectsort(a);

	}

}

my array is {23, 45, 12, 20}

Quote

output: "20 12 23 45 "
.. still not working
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Selection Sort problem

Posted 30 October 2009 - 09:56 AM

Sorry for the runaround. This will work:

public static void selectionSort(int[] x){
   	  for(int i = x.length-1; i > -1; i--){
   		  int max = i;
   		  for(int j = 0; j < i; j++){
   			  if(x[j] > x[max]) max = j;
   			  }
   		  int temp = x[i];
   		  x[i] = x[max];
   		  x[max] = temp;
   		  }//end i
   		   	
	  }
   

Was This Post Helpful? 0
  • +
  • -

#15 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Re: Selection Sort problem

Posted 01 November 2009 - 05:51 PM

View Postmacosxnerd101, on 30 Oct, 2009 - 08:56 AM, said:

Sorry for the runaround. This will work:

public static void selectionSort(int[] x){
   	  for(int i = x.length-1; i > -1; i--){
   		  int max = i;
   		  for(int j = 0; j < i; j++){
   			  if(x[j] > x[max]) max = j;
   			  }
   		  int temp = x[i];
   		  x[i] = x[max];
   		  x[max] = temp;
   		  }//end i
   		   	
	  }
   

cheers mate, funny thing is i solved it a couple days ago, same code aswell. (well after your previous code that is) gave me a good hint
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1