4 Replies - 5604 Views - Last Post: 05 February 2009 - 06:13 PM Rate Topic: -----

#1 NewProgrammer34  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-January 09

Selection sort

Post icon  Posted 05 February 2009 - 05:20 PM

I am working on a project basically you input a file of double numbers into an arrayList and sort them using selection sort. I have used selection sort for arrays but it seems to be a bit different. I believe all my other code is correct besides the selection sort i just need a little help.


Here is my code

public class SortandPrint
{
	public static void main(String[]args)
	{
		try
		{
			BufferedReader br = new BufferedReader(new FileReader("file.txt"));
			String numbers;
			double lh = 0; 
			double size = 0;
			ArrayList<Double> num = new ArrayList<Double>();
			
						
			numbers = br.readLine();
			while((numbers = br.readLine())!=null)
			{
				num.add(Double.parseDouble(numbers));
			}
			
			
			for (lh = 0; lh < size; lh++);
			{
				double s = lh;
				for (double rh = lh+1; rh < size; rh++)
					if (num.get(rh)< num.get(s) )
						s = rh;
				double t = lh;
				lh = s;
				s = t;
			}
			
		//Print Statements			
			
		
		}
		catch(IOException e) 
		{
			e.printStackTrace();
		} 	
	}	
}



The num.get[rh] and num.get[s] give me an error because .get can only be used with integer. Is there anyway to use selection sort in an arrayList that is double numbers?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Selection sort

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Selection sort

Posted 05 February 2009 - 05:35 PM

Hm...I don't know why it's not working, but try this.

if (num.get(rh).doubleValue() < num.get(s).doubleValue())


I think that might solve your problem. Hope it does! :D

EDIT: For future reference, the get() method does not only work with int variables. It returns a generic type (really what the ArrayList contains, what you specified in the < >). It works with all types assuming you use the right comparison.

This post has been edited by Locke: 05 February 2009 - 05:41 PM

Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Selection sort

Posted 05 February 2009 - 06:08 PM

You are sorting double *values*. Their location in the ArrayList is what you need to change. The locations are indexed by integers. There is an Insertion Sort Java snippet here on the site that I have modified to sort your ArrayList
	int firstOutOfOrder, location;
	double temp;

	for(firstOutOfOrder = 1; firstOutOfOrder < num.size(); firstOutOfOrder++) { //Starts at second term, goes until the end of the array.
		if(num.get(firstOutOfOrder) < num.get(firstOutOfOrder - 1)) { //If the two are out of order, we move the element to its rightful place
			temp = num.get(firstOutOfOrder);
			location = firstOutOfOrder;

			do { //Keep moving down the array until we find exactly where it's supposed to go.
				num.set(location, num.get(location - 1));
				location--;
			}
			while (location > 0 && num.get(location - 1) > temp);

			num.set(location, temp);
		}
	}


This post has been edited by n8wxs: 05 February 2009 - 06:10 PM

Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Selection sort

Posted 05 February 2009 - 06:12 PM

...Oopsies. My bad. This is precisely why I stay away from the Selection Sort. :unsure:
Was This Post Helpful? 0
  • +
  • -

#5 NewProgrammer34  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-January 09

Re: Selection sort

Posted 05 February 2009 - 06:13 PM

Hey thanks for everything the comments really helped I am all finshed with it.

Thanks again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1