简体   繁体   English

无法似乎弄清楚我写了什么排序算法

[英]Cant seem to figure out what Sorting Algorithm I have written

We recently stared doing sorting algorithms in school. 我们最近开始在学校盯着排序算法。 So I wrote the code below, it works but I don't know whether it is Bubble sort or Selection Sort. 所以我写了下面的代码,它可以工作,但是我不知道它是冒泡排序还是选择排序。

for i in range(0 , len(array)):
  for j in range(0 , len(array) -1):
    if (array[j] > array[j +1]):
      array[j] , array[j + 1] = array[j + 1] , array[j]

Algorithm: Bubble Sort 算法: Bubble Sort

Because it repeatedly swaps the adjacent elements if they are in the wrong order. 因为如果相邻元素顺序错误,它将重复交换相邻元素。

for i in range(0 , len(array)):
    for j in range(0 , len(array) - i -1):
        # traverse the array from 0 to len(array) - i - 1
        # Swap if the element found is greater 
        # than the next element
          if (array[j] > array[j +1]):
              array[j] , array[j + 1] = array[j + 1] , array[j]

There is an error in your second for loop, you need to traverse it to len(array) -i -1 , because at the end of each iteration largest element would be at the end of the array. 第二个for循环中有一个错误,您需要将其遍历到len(array) -i -1 ,因为在每次迭代结束时,最大的元素将在数组的末尾。

Hope this answers your question!! 希望这能回答您的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我已经写了一个排序算法。 这是快速分类吗? - I have written a sorting algorithm. Is this a quicksort? 我有这个 python 代码,我试图将其转换为 javascript 但我似乎无法弄清楚是哪个 if 语句。 使用 - I have this python code that I am trying to turn into javascript but I cant seem to figure out which if statement. to use 我无法弄清楚这段代码有什么问题 - i cant figure out what is wrong in this code 这段代码有什么问题? 我想不通 - What is wrong with this code? I cant figure it out 我正在制作一个 mp3 播放器,我需要一个前进按钮,但似乎无法弄清楚 - I am making a mp3 player , and i need a forward button to it but cant seem to figure it out 我试图找出 .csv 文件的最大值和最小值,但我无法弄清楚我做错了什么 - Im trying to figure out the max and min of a .csv file and i cant figure out what im doing wrong 我似乎无法找出在半圆上找到等距点的正确算法 - I can't seem to figure out the correct algorithm for finding the equidistant points on a semicircle 无法弄清楚这只蜘蛛有什么问题 - Cant figure out what is wrong with this spider TypeError:需要一个整数,我无法弄清楚 - TypeError: an integer is required I cant figure it out 我无法弄清楚的 Pgzero 错误 - Pgzero errors that I cant figure out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM