All pastes #793160 Raw Edit

netsurf

public text v1 · immutable
·published 2007-11-22 00:09 UTC
rendered paste body
//1.Implement and test a class containing a method to perform the selection sort on an array of integers. Make full use of the lecture materials that give details of how to do this.
import java.util.*;
public class part1
{
	public static void main (String[] args)
	{
		 int [] Mlist = {10, 8, 6, 11, 7};
		// For i = n downto 2
		for (int i = Mlist.length; i >= 2; --i)
		{
	  		 // Find the position of the largest item in the sublist 
        		// of the first i items from the list
			int posBig = findPosBig( Mlist, i ); 
	  	 	// Swap the largest item with the ith item; i.e. item at position i -1
	   		swapArrayItems( Mlist, i-1, posBig ); 
		}
		for ( int i = Mlist.length; i == 0; --i)
		{
			System.out.printf("%3d", Mlist[i]);
		}
	}

 	public static void swapArrayItems ( int [] list, int pos1, int pos2 )
 	{
  	      int temp = list[pos1];
  	      list[pos1] = list[pos2];
	       list[pos2] = temp;
 	}
 	 public static int findPosBig ( int [] sublist, int numItems )
 	{      // check if biggest by considering other elements
 	       int posBiggest = 0;
 	       for (int i=1; i < numItems; i++)
 	       {
 	     		if ( sublist[i] > sublist[posBiggest] )
			{
		 		 posBiggest = i;
			}
  	       }
  	      return posBiggest;
	 }
}