Python插入排序演算法

2021-09-17 03:58:43 字數 3331 閱讀 7762

插入排序演算法,簡單直觀的排序演算法.工作原理是通過構建有序序列,對於未排序資料,在已排序序列中從後向前掃瞄,找到相應位置並插入.

python**如下:

# coding:utf-8

import random

class insert:

def main(self):

arr_list = [i for i in range(10)]

random.shuffle(arr_list)

print("raw list: ", arr_list)

self.sort_list(arr_list)

def sort_list(self, iterable):

for i in range(1, len(iterable)):

if iterable[i] < iterable[i - 1]:

self.exchange_element(iterable, i)

print("process list: ", iterable)

self.sort_list(iterable)

@staticmethod

def exchange_element(iterable, index):

iterable[index], iterable[index - 1] = iterable[index - 1], iterable[index]

if __name__ == '__main__':

insert = insert()

insert.main()

結果展示:

raw list: [6, 4, 7, 0, 9, 5, 8, 2, 1, 3]

process list: [4, 6, 7, 0, 9, 5, 8, 2, 1, 3]

process list: [4, 6, 0, 7, 9, 5, 8, 2, 1, 3]

process list: [4, 0, 6, 7, 9, 5, 8, 2, 1, 3]

process list: [0, 4, 6, 7, 9, 5, 8, 2, 1, 3]

process list: [0, 4, 6, 7, 5, 9, 8, 2, 1, 3]

process list: [0, 4, 6, 5, 7, 9, 8, 2, 1, 3]

process list: [0, 4, 5, 6, 7, 9, 8, 2, 1, 3]

process list: [0, 4, 5, 6, 7, 8, 9, 2, 1, 3]

process list: [0, 4, 5, 6, 7, 8, 2, 9, 1, 3]

process list: [0, 4, 5, 6, 7, 2, 8, 9, 1, 3]

process list: [0, 4, 5, 6, 2, 7, 8, 9, 1, 3]

process list: [0, 4, 5, 2, 6, 7, 8, 9, 1, 3]

process list: [0, 4, 2, 5, 6, 7, 8, 9, 1, 3]

process list: [0, 2, 4, 5, 6, 7, 8, 9, 1, 3]

process list: [0, 2, 4, 5, 6, 7, 8, 1, 9, 3]

process list: [0, 2, 4, 5, 6, 7, 1, 8, 9, 3]

process list: [0, 2, 4, 5, 6, 1, 7, 8, 9, 3]

process list: [0, 2, 4, 5, 1, 6, 7, 8, 9, 3]

process list: [0, 2, 4, 1, 5, 6, 7, 8, 9, 3]

process list: [0, 2, 1, 4, 5, 6, 7, 8, 9, 3]

process list: [0, 1, 2, 4, 5, 6, 7, 8, 9, 3]

process list: [0, 1, 2, 4, 5, 6, 7, 8, 3, 9]

process list: [0, 1, 2, 4, 5, 6, 7, 3, 8, 9]

process list: [0, 1, 2, 4, 5, 6, 3, 7, 8, 9]

process list: [0, 1, 2, 4, 5, 3, 6, 7, 8, 9]

process list: [0, 1, 2, 4, 3, 5, 6, 7, 8, 9]

process list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

演算法優化:

# coding:utf-8

import random

class insert:

def main(self):

arr_list = [i for i in range(10)]

random.shuffle(arr_list)

print("raw list: ", arr_list)

self.sort_list(arr_list)

@staticmethod

def sort_list(iterable):

for i in range(len(iterable) - 1):

for j in range(i + 1, len(iterable)):

if iterable[j] < iterable[i]:

temp = iterable[j]

iterable.remove(iterable[j])

iterable.insert(i, temp)

print("process list: ", iterable)

if __name__ == '__main__':

insert = insert()

insert.main()

結果展示:

raw list: [5, 4, 7, 3, 6, 9, 0, 2, 1, 8]

process list: [4, 5, 7, 3, 6, 9, 0, 2, 1, 8]

process list: [3, 4, 5, 7, 6, 9, 0, 2, 1, 8]

process list: [0, 3, 4, 5, 7, 6, 9, 2, 1, 8]

process list: [0, 2, 3, 4, 5, 7, 6, 9, 1, 8]

process list: [0, 1, 2, 3, 4, 5, 7, 6, 9, 8]

process list: [0, 1, 2, 3, 4, 5, 6, 7, 9, 8]

process list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Python插入排序演算法

插入排序 英語 insertion sort 是一種簡單直觀的排序演算法。它的工作原理是通過構建有序序列,對於未排序資料,在已排序序列中從後向前掃瞄,找到相應位置並插入。插入排序在實現上,在從後向前掃瞄過程中,需要反覆把已排序元素逐步向後挪位,為最新元素提供插入空間。插入排序 n len alist...

python 插入排序演算法

描述 插入排序的基本操作就是將乙個資料插入到已經排好序的有序資料中,從而得到乙個新的 個數加一的有序資料,演算法適用於少量資料的排序,時間複雜度為o n 2 是穩定的排序方法。插入演算法把要排序的陣列分成兩部分 第一部分包含了這個陣列的所有元素,但將最後乙個元素除外 讓陣列多乙個空間才有插入的位置 ...

Python演算法 插入排序

插入排序 英語 insertion sort 是一種簡單直觀的排序演算法。它的工作原理是通過構建有序序列,對於未排序資料,在已排序序列中從後向前掃瞄,找到相應位置並插入。插入排序在實現上,通常採用in place排序 即只需用到o 1 的額外空間的排序 因而在從後向前掃瞄過程中,需要反覆把已排序元素...