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

Update selectionSort.js #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 18 additions & 31 deletions learn-git/selectionSort.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
/*
This task requires you to use the pseudocode below to implement selection sort
PSEUDOCODE
procedure selection sort
arr : array of items
n : size of arr
temp : temporary var to be used for swaping
var arr = [64, 25, 12, 22, 11];
var n = arr.length;
var temp;

for i = 0 to n
//set current element as minimum
min = i

//check the element to be minimum

for j = i+1 to n
if arr[min] > arr[j] then
min = j;

//swap the minimum element with the current element

temp = arr[i]
arr[i] = arr[min]
arr[min] = temp

end if
end for
end for

end procedure

You can test your solution using [64, 25, 12, 22, 11]
*/
for(var i = 0; i<n;i = i + 1){
var min = i;
for(var j = i+1; j < n;j = j+1){
if(arr[j] < arr[min]){
min = j;

}
}
if(arr[min] !== i){
var temp = arr[i];
arr[i] = arr[min]
arr[min] = temp
}
}
document.write(arr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is good practice to use document.write() I'd recommend console.log() or console.table