This time I will write the code for selection sort.
SelectionSort.java
public class SelectionSort {
	public static void main(String args[]) {
		int[] array = {2,3,5,4,1};
		sort(array);
		for(int i=0;i<array.length;i++) {
			System.out.print(array[i]);
		}
	}
	public static void sort(int[] array) {
		int n = array.length;
		for (int i =0;i<n-1;i++) {
			int lowest = i;
			int lowkey = array[i];
			for(int j=i+1;j<n;j++) {
				if(array[j]<lowkey) {
					lowest = j;
					lowkey=array[j];
				}
			}
			int temp = array[i];
			array[i]=array[lowest];
			array[lowest] = temp;
		}
	}
}
Next time I'll try insertion sort.
Recommended Posts