我的代码测试未通过我的冒泡排序算法

时间:2019-04-11 16:31:34

标签: python algorithm

问题: 冒泡排序是一种算法,它对长度为N的序列进行排序,以便检查两个相邻元素以更改其位置。气泡排序可以进行N次,如下所示。

将第一个值与第二个值进行比较,如果第一个值较大,则更改位置。 将第二个值与第三个值进行比较,如果第二个值较大,它将更改其位置。 ... 比较N-1和N-值,如果N-1的值较大则更改位置。 “ Supchan”我当然知道气泡排序的结果。但是,由于N非常大,因此需要花费很长时间来执行上述步骤K次。编写一个程序,以帮助您找到气泡排序的中间过程。

我的代码

def bubble(list):
    temp = 0
    for i in range(0, len(list)): 
        for j in range(i+1, len(list)): 
            if (list[i] > list[j]):
                temp = list[i]
                list[i] = list[j]
                list[j] = temp
    return list


numbers = input()
items = [int(num) for num in numbers.split()]

print(bubble(items))

测试条件


N and K are given in the first line.

The second line gives the status of the first sequence. That is, N integers forming the first sequence are given in turn, with spaces between them.

1 ≤ N ≤ 100,000
1 ≤ K ≤ N
Each term in the sequence is an integer from 1 to 1,000,000,000.

输入和输出

input: 3 1 2 5 4
output: 1 2 3 4 5

我编写的代码似乎运行良好。但是,编码测试的等级拒绝了我。

我无法理解原因,因为原因未列出。我的代码有问题吗?

1 个答案:

答案 0 :(得分:0)

尝试更改行

Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 77, in raise_last_exception
    raise _exception[0](_exception[1]).with_traceback(_exception[2])
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/apps/config.py", line 134, in create
    % (mod_path, cls_name, ', '.join(candidates))
django.core.exceptions.ImproperlyConfigured: 'upload.apps' does not contain a class 'uploadConfig'. Choices are: 'UploadConfig'.

Traceback (most recent call last):
  File "manage.py", line 19, in <module>
    execute_from_command_line(sys.argv)
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 95, in handle
    self.run(**options)
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 102, in run
    autoreload.run_with_reloader(self.inner_run, **options)
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 579, in run_with_reloader
    start_django(reloader, main_func, *args, **kwargs)
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 564, in start_django
    reloader.run(django_main_thread)
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 272, in run
    get_resolver().urlconf_module
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/urls/resolvers.py", line 564, in urlconf_module
    return import_module(self.urlconf_name)
  File "/home/edwardkim/syseng/venv/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/edwardkim/syseng/Foram_Tagger_Team43_Software/projectsite/projectsite/urls.py", line 23, in <module>
    path('search/', include('search.urls')),
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/home/edwardkim/syseng/venv/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/edwardkim/syseng/Foram_Tagger_Team43_Software/projectsite/search/urls.py", line 3, in <module>
    from . import views
  File "/home/edwardkim/syseng/Foram_Tagger_Team43_Software/projectsite/search/views.py", line 2, in <module>
    from upload.models import Img
  File "/home/edwardkim/syseng/Foram_Tagger_Team43_Software/projectsite/upload/models.py", line 3, in <module>
    class ImgParent(models.Model):
  File "/home/edwardkim/syseng/venv/lib/python3.6/site-packages/django/db/models/base.py", line 111, in __new__
    "INSTALLED_APPS." % (module, name)
RuntimeError: Model class upload.models.ImgParent doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS. 

numbers = input()

测试输入的格式可能是

numbers = raw_input()

代替

1 5 6 4 3

使用'1 5 6 4 3' 运行时可能会导致错误