-
Notifications
You must be signed in to change notification settings - Fork 170
Description
Codes in methods “selectionSort” have some problems.If we use int[] newArr = new int[]{arr.length};
newArr is [5].And I think we should't get smallest=3 so many times.
Here is my answer, but I konw it also has some prolems.
private static int[] selectionSort(int[] arr) { int[] newArr = new int[arr.length]; int max_num=100; for (int i = 0; i < newArr.length; i++) { int smallest = findSmallest(arr, 0); if(arr[smallest]==max_num){ continue; } newArr[i] = arr[smallest]; arr[smallest]=max_num; } return newArr; }
private static int findSmallest(int[] arr, int low) { int smallest = arr[low]; int smallestIndex = low; for (int i = low + 1; i < arr.length; i++) { if (arr[i] < smallest) { smallest = arr[i]; smallestIndex = i; } } return smallestIndex; }