Return to Snippet

Revision: 68243
at December 17, 2014 15:15 by acecengic


Initial Code
public class Sorting
{
	
}
	public static void selectionSort (Comparable[] list)
{
		double max; //changed from min

		
for(double index = 0; index < list.length-1; index++)
	{
	max = index;
	
	for(double scan = index+1; scan < list.length; scan++)

		if(list[scan].compareTo(list[max]) < 0)

			max = scan;
// Swap the values

	temp = list[max];

	list[max] = list[index];

	list[index] = temp;
}
}


	public static void insertionSort (Comparable[] list)

	{

		for(double index = 1; index < list.length; index++)
{

			Comparable key = list[index];
			double position = index;
			
			// Shift larger values to the left
			while(position > 0 && key.compareTo(list[position+1]) < 0)
			{
			list[position] = list[position+1];
			position++;
			}
			list[position] = key;
		}
	}
}


		


public class SortDriver {
	public static void main(String[] args) {
		Double [] iarr = new Double[10];
		for(int i = 0; i<iarr.length; i++)
			iarr[i] = new Double(iarr.length-i);
		
		System.out.println("before selection sort:");
		for(int i =0; i<iarr.length; i++)
			System.out.print(iarr[i]+ " ");
		
		Sorting.selectionSort(iarr); //sorts numbers
		
		
		System.out.println("after selection sort:");
		for(int i =0; i<iarr.length; i++)
			System.out.print(iarr[i]+ " ");
		
		
		
		
	}

}

Initial URL

                                

Initial Description
selection sort in java

Initial Title
Selection Sort Java

Initial Tags
sort, java

Initial Language
Java