Presume a value as initial, update its value in the loop












0















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?










share|improve this question























  • 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


















0















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?










share|improve this question























  • 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
















0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 6:40









JawSawJawSaw

4,57811837




4,57811837













  • 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







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














3 Answers
3






active

oldest

votes


















1














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





share|improve this answer
























  • It is called minimum search.

    – JawSaw
    Nov 22 '18 at 7:36



















1














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.






share|improve this answer































    1














    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





    share|improve this answer























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


      }
      });














      draft saved

      draft discarded


















      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









      1














      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





      share|improve this answer
























      • It is called minimum search.

        – JawSaw
        Nov 22 '18 at 7:36
















      1














      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





      share|improve this answer
























      • It is called minimum search.

        – JawSaw
        Nov 22 '18 at 7:36














      1












      1








      1







      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





      share|improve this answer













      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






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 22 '18 at 6:56









      enamoriaenamoria

      617621




      617621













      • 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





      It is called minimum search.

      – JawSaw
      Nov 22 '18 at 7:36













      1














      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.






      share|improve this answer




























        1














        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.






        share|improve this answer


























          1












          1








          1







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 6:55









          mohor chattmohor chatt

          926




          926























              1














              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





              share|improve this answer




























                1














                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





                share|improve this answer


























                  1












                  1








                  1







                  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





                  share|improve this answer













                  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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 6:57









                  BernardLBernardL

                  2,37111130




                  2,37111130






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      'app-layout' is not a known element: how to share Component with different Modules

                      android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                      WPF add header to Image with URL pettitions [duplicate]