Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

选择排序 #23

Open
hubvue opened this issue May 13, 2019 · 0 comments
Open

选择排序 #23

hubvue opened this issue May 13, 2019 · 0 comments
Labels

Comments

@hubvue
Copy link
Owner

hubvue commented May 13, 2019

从数组的开头开始,将第一个元素和其他元素比较,最小的元素会被放到数组第一个位置,再从第二个位置继续。

const selectionStor = arr => {
    let temp,
        min,
        len = arr.length;
    for(let i = 0; i < len -1; i ++){
        min = i;
        for(let j = i+1; j < len ; j ++){
            if(arr[j]<arr[min]){
                min = j;
            }
        }
        temp = arr[i];
        arr[i] = arr[min];
        arr[min] = temp;

    }
    return arr;
}
console.log(selectionStor([1,4,3,5,9,7,3,3,5,3,2]))
@hubvue hubvue added the 排序 label May 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant