|
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. 2. Implement and test a class containing a method to perform the bubble sort on an array of integers. Make full use of the lecture materials that give details of how to do this.
import java.util.*; class Sort{ public static void main(String[] args) { int[] intArray = new int[] {7, 2, 6, 3, 8, 4, 9, 1,10,5 };
for (int x : intArray) { System.out.print(x); } System.out.println(); selectionSort(intArray);
for (int x : intArray) { System.out.println(x); }
}
public static void selectionSort(int[] intArray) { for (int out = 0; out < intArray.length - 1; out++) { int min = out; for (int in = out + 1; in < intArray.length; in++) if (intArray[in] < intArray[min]) min = in; swap(intArray, out, min); } }
private static void swap(int[] intArray, int one, int two) { int temp = intArray[one]; intArray[one] = intArray[two]; intArray[two] = temp; } }
|