Skip to content

Wrong answer in SelectionSort2 #4

@kexi292

Description

@kexi292

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; }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions