Last active
August 29, 2015 14:04
-
-
Save selfboot/9fec1eb77a5ed14dd53a to your computer and use it in GitHub Desktop.
冒泡排序的python实现。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
original_sequence = raw_input("Enter the sequence: ") | |
sequence_str = original_sequence.split() | |
sort_sequence = [] | |
for number_str in sequence_str: | |
sort_sequence.append(int(number_str)) | |
count = len(sort_sequence) | |
for i in range(count): | |
min_number = sort_sequence[i] | |
index = i | |
scan_start = i +1 | |
while(scan_start < count): | |
if sort_sequence[scan_start] < min_number: | |
min_number = sort_sequence[scan_start] | |
index = scan_start | |
else: | |
pass | |
scan_start += 1 | |
sort_sequence[i], sort_sequence[index] = sort_sequence[index], sort_sequence[i] | |
print sort_sequence | |
# ➜ algorithm ./bubble_sort.py | |
# Enter the sequence: 12 3 4 5 5 6 6 7 8 8 43434 232 | |
# [3, 4, 5, 5, 6, 6, 7, 8, 8, 12, 232, 43434] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment