Presume a value as initial, update its value in the loop
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am learning selection sort algorithms
from typing import List
def find_smallest(arr:List) -> int:
smallest = arr[0] #set pivot
smallest_index = 0
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
return smallest_index
def selection_sort(arr) -> List:
new_arr =
for i in range(len(arr)):
smallest = find_smallest(arr)
new_arr.append(arr.pop(smallest))
return new_arr
I am curious about the function find_smallest
,
it firstly presume arr[0] as the smallest and initiate the loop.
I know the complete code is called selection sort algorithms,
How about the presume and update its value in the loop, is there an terminology for it?
python algorithm
add a comment |
I am learning selection sort algorithms
from typing import List
def find_smallest(arr:List) -> int:
smallest = arr[0] #set pivot
smallest_index = 0
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
return smallest_index
def selection_sort(arr) -> List:
new_arr =
for i in range(len(arr)):
smallest = find_smallest(arr)
new_arr.append(arr.pop(smallest))
return new_arr
I am curious about the function find_smallest
,
it firstly presume arr[0] as the smallest and initiate the loop.
I know the complete code is called selection sort algorithms,
How about the presume and update its value in the loop, is there an terminology for it?
python algorithm
If you don't assume that the first value is the smallest, what can you compare other items to? This just initiates the algorithm.
– roganjosh
Nov 22 '18 at 6:56
add a comment |
I am learning selection sort algorithms
from typing import List
def find_smallest(arr:List) -> int:
smallest = arr[0] #set pivot
smallest_index = 0
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
return smallest_index
def selection_sort(arr) -> List:
new_arr =
for i in range(len(arr)):
smallest = find_smallest(arr)
new_arr.append(arr.pop(smallest))
return new_arr
I am curious about the function find_smallest
,
it firstly presume arr[0] as the smallest and initiate the loop.
I know the complete code is called selection sort algorithms,
How about the presume and update its value in the loop, is there an terminology for it?
python algorithm
I am learning selection sort algorithms
from typing import List
def find_smallest(arr:List) -> int:
smallest = arr[0] #set pivot
smallest_index = 0
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
return smallest_index
def selection_sort(arr) -> List:
new_arr =
for i in range(len(arr)):
smallest = find_smallest(arr)
new_arr.append(arr.pop(smallest))
return new_arr
I am curious about the function find_smallest
,
it firstly presume arr[0] as the smallest and initiate the loop.
I know the complete code is called selection sort algorithms,
How about the presume and update its value in the loop, is there an terminology for it?
python algorithm
python algorithm
asked Nov 22 '18 at 6:40
JawSawJawSaw
4,87511939
4,87511939
If you don't assume that the first value is the smallest, what can you compare other items to? This just initiates the algorithm.
– roganjosh
Nov 22 '18 at 6:56
add a comment |
If you don't assume that the first value is the smallest, what can you compare other items to? This just initiates the algorithm.
– roganjosh
Nov 22 '18 at 6:56
If you don't assume that the first value is the smallest, what can you compare other items to? This just initiates the algorithm.
– roganjosh
Nov 22 '18 at 6:56
If you don't assume that the first value is the smallest, what can you compare other items to? This just initiates the algorithm.
– roganjosh
Nov 22 '18 at 6:56
add a comment |
3 Answers
3
active
oldest
votes
I think bubble sort
is the answer. I've never think about the bubble loop
as an assumption about the smallest, until I see your question :D
def sort(arr):
for i in range(len(arr)):
# we presume a[i] is the smallest one. Then we update by compare it with the rest of the list
for j in range(i + 1, len(arr)):
if arr[i] > arr[j]: # if our assumption is wrong (arr[i] is not the smallest), update it with arr[j] (which is smaller than arr[i])
swap(arr[i], arr[j])
# After this `for j` loop, arr[i] will be the smallest value of the list
It is called minimum search.
– JawSaw
Nov 22 '18 at 7:36
add a comment |
No.There is no terminology for it unlike quick sort where we choose a pivot and compare elements.
Out of the topic but a interesting fact on selection sort is
The good thing about selection sort is it never makes more than O(n) swaps and can be useful when memory write is a costly operation.
add a comment |
It presumes the first index of the list to be the smallest value, and runs down the list to see if there are any smaller values, and when it does find a smaller value, it updates the smallest
, it does this till the end of the list to make sure you find the smallest value in the entire list, in the example you provided it returns the index of smallest value in the list.
I added 2 print
statements which should give you an idea on how its working:
from typing import List
def find_smallest(arr:List) -> int:
smallest = arr[0] #set pivot
smallest_index = 0
print("presumed smallest {}".format(smallest)) #print presumed
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
print("updated smallest {}".format(smallest)) #print updates to smallest
return smallest_index
And the results:
find_smallest([7,6,1,3,8,9,0])
>>presumed smallest 7
updated smallest 6
updated smallest 1
updated smallest 0
6
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425193%2fpresume-a-value-as-initial-update-its-value-in-the-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think bubble sort
is the answer. I've never think about the bubble loop
as an assumption about the smallest, until I see your question :D
def sort(arr):
for i in range(len(arr)):
# we presume a[i] is the smallest one. Then we update by compare it with the rest of the list
for j in range(i + 1, len(arr)):
if arr[i] > arr[j]: # if our assumption is wrong (arr[i] is not the smallest), update it with arr[j] (which is smaller than arr[i])
swap(arr[i], arr[j])
# After this `for j` loop, arr[i] will be the smallest value of the list
It is called minimum search.
– JawSaw
Nov 22 '18 at 7:36
add a comment |
I think bubble sort
is the answer. I've never think about the bubble loop
as an assumption about the smallest, until I see your question :D
def sort(arr):
for i in range(len(arr)):
# we presume a[i] is the smallest one. Then we update by compare it with the rest of the list
for j in range(i + 1, len(arr)):
if arr[i] > arr[j]: # if our assumption is wrong (arr[i] is not the smallest), update it with arr[j] (which is smaller than arr[i])
swap(arr[i], arr[j])
# After this `for j` loop, arr[i] will be the smallest value of the list
It is called minimum search.
– JawSaw
Nov 22 '18 at 7:36
add a comment |
I think bubble sort
is the answer. I've never think about the bubble loop
as an assumption about the smallest, until I see your question :D
def sort(arr):
for i in range(len(arr)):
# we presume a[i] is the smallest one. Then we update by compare it with the rest of the list
for j in range(i + 1, len(arr)):
if arr[i] > arr[j]: # if our assumption is wrong (arr[i] is not the smallest), update it with arr[j] (which is smaller than arr[i])
swap(arr[i], arr[j])
# After this `for j` loop, arr[i] will be the smallest value of the list
I think bubble sort
is the answer. I've never think about the bubble loop
as an assumption about the smallest, until I see your question :D
def sort(arr):
for i in range(len(arr)):
# we presume a[i] is the smallest one. Then we update by compare it with the rest of the list
for j in range(i + 1, len(arr)):
if arr[i] > arr[j]: # if our assumption is wrong (arr[i] is not the smallest), update it with arr[j] (which is smaller than arr[i])
swap(arr[i], arr[j])
# After this `for j` loop, arr[i] will be the smallest value of the list
answered Nov 22 '18 at 6:56
enamoriaenamoria
654722
654722
It is called minimum search.
– JawSaw
Nov 22 '18 at 7:36
add a comment |
It is called minimum search.
– JawSaw
Nov 22 '18 at 7:36
It is called minimum search.
– JawSaw
Nov 22 '18 at 7:36
It is called minimum search.
– JawSaw
Nov 22 '18 at 7:36
add a comment |
No.There is no terminology for it unlike quick sort where we choose a pivot and compare elements.
Out of the topic but a interesting fact on selection sort is
The good thing about selection sort is it never makes more than O(n) swaps and can be useful when memory write is a costly operation.
add a comment |
No.There is no terminology for it unlike quick sort where we choose a pivot and compare elements.
Out of the topic but a interesting fact on selection sort is
The good thing about selection sort is it never makes more than O(n) swaps and can be useful when memory write is a costly operation.
add a comment |
No.There is no terminology for it unlike quick sort where we choose a pivot and compare elements.
Out of the topic but a interesting fact on selection sort is
The good thing about selection sort is it never makes more than O(n) swaps and can be useful when memory write is a costly operation.
No.There is no terminology for it unlike quick sort where we choose a pivot and compare elements.
Out of the topic but a interesting fact on selection sort is
The good thing about selection sort is it never makes more than O(n) swaps and can be useful when memory write is a costly operation.
answered Nov 22 '18 at 6:55
mohor chattmohor chatt
1246
1246
add a comment |
add a comment |
It presumes the first index of the list to be the smallest value, and runs down the list to see if there are any smaller values, and when it does find a smaller value, it updates the smallest
, it does this till the end of the list to make sure you find the smallest value in the entire list, in the example you provided it returns the index of smallest value in the list.
I added 2 print
statements which should give you an idea on how its working:
from typing import List
def find_smallest(arr:List) -> int:
smallest = arr[0] #set pivot
smallest_index = 0
print("presumed smallest {}".format(smallest)) #print presumed
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
print("updated smallest {}".format(smallest)) #print updates to smallest
return smallest_index
And the results:
find_smallest([7,6,1,3,8,9,0])
>>presumed smallest 7
updated smallest 6
updated smallest 1
updated smallest 0
6
add a comment |
It presumes the first index of the list to be the smallest value, and runs down the list to see if there are any smaller values, and when it does find a smaller value, it updates the smallest
, it does this till the end of the list to make sure you find the smallest value in the entire list, in the example you provided it returns the index of smallest value in the list.
I added 2 print
statements which should give you an idea on how its working:
from typing import List
def find_smallest(arr:List) -> int:
smallest = arr[0] #set pivot
smallest_index = 0
print("presumed smallest {}".format(smallest)) #print presumed
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
print("updated smallest {}".format(smallest)) #print updates to smallest
return smallest_index
And the results:
find_smallest([7,6,1,3,8,9,0])
>>presumed smallest 7
updated smallest 6
updated smallest 1
updated smallest 0
6
add a comment |
It presumes the first index of the list to be the smallest value, and runs down the list to see if there are any smaller values, and when it does find a smaller value, it updates the smallest
, it does this till the end of the list to make sure you find the smallest value in the entire list, in the example you provided it returns the index of smallest value in the list.
I added 2 print
statements which should give you an idea on how its working:
from typing import List
def find_smallest(arr:List) -> int:
smallest = arr[0] #set pivot
smallest_index = 0
print("presumed smallest {}".format(smallest)) #print presumed
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
print("updated smallest {}".format(smallest)) #print updates to smallest
return smallest_index
And the results:
find_smallest([7,6,1,3,8,9,0])
>>presumed smallest 7
updated smallest 6
updated smallest 1
updated smallest 0
6
It presumes the first index of the list to be the smallest value, and runs down the list to see if there are any smaller values, and when it does find a smaller value, it updates the smallest
, it does this till the end of the list to make sure you find the smallest value in the entire list, in the example you provided it returns the index of smallest value in the list.
I added 2 print
statements which should give you an idea on how its working:
from typing import List
def find_smallest(arr:List) -> int:
smallest = arr[0] #set pivot
smallest_index = 0
print("presumed smallest {}".format(smallest)) #print presumed
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
print("updated smallest {}".format(smallest)) #print updates to smallest
return smallest_index
And the results:
find_smallest([7,6,1,3,8,9,0])
>>presumed smallest 7
updated smallest 6
updated smallest 1
updated smallest 0
6
answered Nov 22 '18 at 6:57
BernardLBernardL
2,42411231
2,42411231
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425193%2fpresume-a-value-as-initial-update-its-value-in-the-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you don't assume that the first value is the smallest, what can you compare other items to? This just initiates the algorithm.
– roganjosh
Nov 22 '18 at 6:56