5 Replies - 562 Views - Last Post: 28 July 2012 - 06:51 PM Rate Topic: -----

#1 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Do not understand selection sort code

Posted 28 July 2012 - 03:45 PM

I do not understand the selection sort code and how it works to put numbers in acceding order. I do not understand why its array.length-1 for the size, doesn't it need to search the last array element as well? I understand the process how the lowest number is switched eg. 1, 5 ,7, 2 would become 1, 2, 7, 5 teb 1 , 2, 5, 7, but I just do not understand the code behind it.

public static void selectionSort(int[] array)
{
	int startScan;
	int index;
	int minIndex;
	int minvalue;
	
	for (startScan = 0; startScan < (array.length-1); startScan++)
	{
		minIndex = startScan;
		minValue = array[startScan];
		
		for (index = startScan + 1; index < array.length; index++)
		{
			if (array[index] < minValue)
			{
				minValue = array[index]
				minindex = index;
			}
		}
	
	
	array[minIndex] = array[startScan];
	array[startScan] = minValue;
	}
}



The only way a binary search would work is if it is sorted in acending order so that would be the slection sort, so that would go before the binary search? Also for the binary search why is the last number array.length-1 and not just array.length.

public static int binarySearch(int[] array, int value)
{
	int first;
	int last;
	int middle;
	int position;
	boolean found;
	
	first = 0;
	last = array.length -1;
	position = -1;
	found = false;
	
	while (!found && first <= last)
	{
		middle = (first + last ) / 2;
		
		if (array[middle] == value)
		{
			found = true;
			position = middle;
		}
		else if (array[middle] > value)
		{
			last = middle -1;
		}
		else
		{
			first = middle +1;
		}
	}
	
	return position;
}




Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Do not understand selection sort code

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: Do not understand selection sort code

Posted 28 July 2012 - 03:49 PM

The way the selection sort algorithm works is by choosing the smallest number as the first element in the array. Then it evaluates the rest of the elements, updating the smallest number as necessary. The smallest number is then swapped with the first element in the list, completing one iteration of the outer loop. Since element 0 is in place, we only need to evaluate elements 1 through length-1.

Quote

The only way a binary search would work is if it is sorted in acending order so that would be the slection sort, so that would go before the binary search?

The sort would need to occur before the search.

Quote

Also for the binary search why is the last number array.length-1 and not just array.length.

Arrays are indexed from through length-1. So if you have 10 elements, they are indexed from 0-9. If you access index 10, you will go out of bounds.
Was This Post Helpful? 0
  • +
  • -

#3 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Re: Do not understand selection sort code

Posted 28 July 2012 - 05:17 PM

but doesn't the array.length always incorporate from 0 to that number?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: Do not understand selection sort code

Posted 28 July 2012 - 05:24 PM

No. If you have ten elements, that's the length. Those elements are indexed 0-9.
Was This Post Helpful? 0
  • +
  • -

#5 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Re: Do not understand selection sort code

Posted 28 July 2012 - 06:50 PM

thank you!
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: Do not understand selection sort code

Posted 28 July 2012 - 06:51 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1