//Author: Hongjie Yang
//Date: July 25, 2001
//SortStudent.java
//Demonstrate the selectionsort algorithm
public class SortStudent
{
	//SortbyHeight1: sort student's height in ascending order
	public static void SortbyHeight1(Student[] s)
	{
		int minIndex;
		Student tmp;
		
		//outer loop: look up each element in array
		for (int index=0; index<s.length-1; index++)
		{
			minIndex = index;
			
			//inner loop: finds smallest value
			for (int scanIndex=index+1; scanIndex<s.length; scanIndex++)
			{
				if (s[scanIndex].getHeight()< s[minIndex].getHeight())
				{
					minIndex = scanIndex;
				}
			}
			
			//swap values
			tmp = s[minIndex];
			s[minIndex] = s[index];
			s[index] = tmp;
		} 
	} //end method SortbyHeight1


	//SortbyHeight2: sort student's height in ascending order
	//If there is a tie, smaller studentID stays in front
	public static void SortbyHeight2(Student[] s)
	{
		int minIndex;
		Student tmp;
		
		//outer loop: look up each element in array
		for (int index=0; index<s.length-1; index++)
		{
			minIndex = index;
			
			//inner loop: finds smallest value
			for (int scanIndex=index+1; scanIndex<s.length; scanIndex++)
			{
				if (s[scanIndex].getHeight()< s[minIndex].getHeight())
				{
					minIndex = scanIndex;
				}
				//if there is a tie, smaller studentID goes first
				if (s[scanIndex].getHeight()== s[minIndex].getHeight())
				{
					if (s[scanIndex].getID()< s[minIndex].getID())
						minIndex = scanIndex;
				}		
			}
			
			//swap values
			tmp = s[minIndex];
			s[minIndex] = s[index];
			s[index] = tmp;
		} 
	} //end method SortbyHeight2
	
	//SortbyHeight3: sort female student's height and male student's height
	//differently, all female students stay in front of all male students
	public static void SortbyHeight3(Student[] s)
	{
		int minIndex;
		Student tmp;
		
		//outer loop: look up each element in array
		for (int index=0; index<s.length-1; index++)
		{
			minIndex = index;
			//use an integer shift to take care of gender
			//if it's female student, the height will be added 2000
			//if it's male student, the height will be added 1000
			//make sure all the male student's height are taller than female student's height
			String gender = s[minIndex].getGender();
			int shift_g =  gender.equals("male") ? 2000: 1000;
			
			//inner loop: finds smallest value
			//female students have priority
			for (int scanIndex=index+1; scanIndex<s.length; scanIndex++)
			{
				String scanGender = s[scanIndex].getGender(); 
				//use an integer to take care of gender
				int shift_Sg = scanGender.equals("male")? 2000: 1000;
				if ((s[scanIndex].getHeight()+shift_Sg)
					< (s[minIndex].getHeight()+shift_g))
				{
					minIndex = scanIndex;
					//update the shift
					gender = s[minIndex].getGender();
					shift_g =  gender.equals("male") ? 2000: 1000;
				}
			}
			
			
			//swap values
			tmp = s[minIndex];
			s[minIndex] = s[index];
			s[index] = tmp;
		} 
	} //end method SortbyHeight	

	//another way to do SortbyHeight3
	public static void SortbyHeight4(Student[] s)
	{
		for (int i =0; i<s.length; i++)
		{
			//increase male's height
			if (s[i].getGender().equals("male"))
			{
				s[i].setHeight(s[i].getHeight() + 1000);
			}
		}
		//sort by Height
		SortbyHeight1(s);
		//remove the added height
		for (int i =0; i<s.length; i++)
		{
			//decrease male's height
			if (s[i].getGender().equals("male"))
			{
				s[i].setHeight(s[i].getHeight()- 1000);
			}
		}	
	}	
	
	
/******************INSERTION SORT*********************/	
	//sort according to their ID
	//using INSERTION SORT
	public static void InsertionSortbyID(Student[] s)
	{
		Student tmp;
		
		// outer loop: lookup each element
		for (int i=1; i<s.length; i++)
		{
			for (int j=i; j>0; j--)
			{
				if (s[j].getID() < s[j-1].getID())
				{
					tmp = s[j-1];
					s[j-1] = s[j];
					s[j] = tmp;
				} 	  
			} 
		}
	}//end method InsertionSortbyID
/**************** END INSERTION SORT *******************/	
	
	//Create and sort an array of Student objects
	public static void main(String[] args)
	{
		//Create an array of 5 Student object
		Student[] students = new Student[6];
		students[0] = new Student("mike", "male", 175, 123456);
		students[1] = new Student("tom", "male", 180, 555712);
		students[2] = new Student("wendy", "female", 165, 622142);
		students[3] = new Student("kate", "female", 175, 434233);
		students[4] = new Student("eric", "male", 169, 232142); 
		students[5] = new Student("Mary", "female", 164, 345621);
		
		//print out before sort
		for (int i=0; i<students.length; i++)
		{
			students[i].Display();
		}
		
		//sort students array
		SortbyHeight4(students);
		System.out.println("************after sorting ************");
	
		for (int i=0; i<students.length; i++)
		{
			students[i].Display();
		}
		
		
	}
}
	
		