Python数据结构

排序

冒泡排序

# 用while循环实现
a = [5, 4, 3, 2, 1]
i = 0
while i < len(a) - 1:
    j = 0
    while j < len(a) - 1 - i:
        if a[j] > a[j+1]:
            a[j], a[j+1] = a[j+1], a[j]
        j += 1
    i += 1
# 用for循环实现
a = [5, 4, 3, 2, 1]
for i in range(len(a)-1):
    for j in range(len(a)-1-i):
        if a[j] > a[j+1]:
            a[j], a[j+1] = a[j+1], a[j]

插入排序

a = [5, 4, 3, 2, 1]
i = 1
while i < len(a):
    t = a[i]
    j = i - 1
    while j >= 0:
        if a[j] > t:
            a[j+1] = a[j]
        else:
            break
        j -= 1
    a[j+1] = t
    i += 1

选择排序

# 用while循环实现
a = [5, 4, 3, 2, 1]
i = 0
while i < len(a) - 1:
    min = i
    j = i + 1
    while j < len(a):
        if a[min] > a[j]:
            min = j
        j += 1
    if min != i:
        a[min], a[i] = a[i], a[min]
    i += 1
# 用for循环实现
a = [5, 4, 3, 2, 1]
for i in range(0, len(a)-1):
    min = i
    for j in range(i+1, len(a)):
        if a[min] > a[j]:
            min = j
    if min != i:
        a[min], a[i] = a[i], a[min]

快速排序

a = [5, 4, 3, 2, 1]
def partion(a, low, high):
    key = a[low]
    while low < high:
        while low < high and a[high] >= key:
            high -= 1
        a[low] = a[high]
        while low < high and a[low] <= key:
            low += 1
        a[high] = a[low]
    a[low] = key
    return low
def sort(a, low, high):
    if low < high:
        mid = partion(a, low, high)
        sort(a, low, mid-1)
        sort(a, mid+1, high)
sort(a, 0, len(a)-1)

查找

折半查找

a = [1, 2, 3, 4, 5]      # 列表要是有序的
def zheban(a, low, high, data):      # a为列表,low为起始位置,high为结束位置,data为要查找的元素
    while low <= high:
        mid = (low+high)//2
        if a[mid] == data:
            return mid
        elif a[mid] > data:
            high = mid - 1
        elif a[mid] < data:
            low = mid + 1
    return -1

折半查找(用递归的方法实现)

a = [1, 2, 3, 4, 5]      # 列表要是有序的
def dgzb(a, low, high, data):      # a为列表,low为起始位置,high为结束位置,data为要查找的元素
    if low <= high:
        mid = (low + high) // 2
        if a[mid] == data:
            return mid
        elif a[mid] < data:
            return dgzb(a, mid+1, high, data)
        elif a[mid] > data:
            return dgzb(a, low, high-1, data)
    return -1

链表

包含链表元素的插入、删除以及链表倒序

class Node():
    def __init__(self, num):
        self.num = num
        self.next = None
class LinkList():
    def __init__(self):
        self.head = None
    def append(self, node):      # 向链表中添加元素
        if self.head == None:
            self.head = node
        else:
            p = self.head
            while p.next != None:
                p = p.next
            p.next = node
    def insert(self):      # 向链表中插入元素
        n = int(input("请输入要插入的位置:"))
        data = int(input("请输入要插入的数据:"))
        if n == 1:
            one = Node(data)
            one.next = self.head
            self.head = one
        else:
            i = 1
            p = self.head
            while i < n - 1 and p != None:
                p = p.next
                i += 1
            if p == None:
                print("位置有误")
            else:
                one = Node(data)
                one.next = p.next
                p.next = one
    def delete(self):      # 删除链表中的元素
        if self.head == None:
            print("链表空了")
        else:
            n = int(input("请输入要删除的位置:"))
            if n == 1:
                print("您删除的元素是:", self.head.num)
                self.head = self.head.next
            else:
                i = 1
                p = self.head
                while i < n - 1 and p != None:
                    p = p.next
                    i += 1
                if p == None or p.next == None:
                    print("位置有误")
                else:
                    print("您删除的元素是:", p.next.num)
                    p.next = p.next.next
    def reverse(self):      # 链表倒序
        pre = None
        current = self.head
        next = self.head.next
        while next != None:
            current.next = pre
            pre = current
            current = next
            next = next.next
        current.next = pre
        self.head = current
    def dayin(self):      # 打印链表
        p = self.head
        while p != None:
            print(p.num, end=" ")
            p = p.next
        print("")
bbb = LinkList()
for i in range(1, 10):
    aaa = Node(i)
    bbb.append(aaa)
while True:
    bh = int(input("请输入编号:"))
    if bh == 1:
        bbb.insert()
    elif bh == 2:
        bbb.delete()
    elif bh == 3:
        bbb.dayin()
    elif bh == 4:
        bbb.reverse()
    elif bh == 0:
        exit(0)

循环队列

class Xhdl():
    def __init__(self, size):      # size为队列的长度
        self.queue = [None]*size
        self.tou = 0
        self.wei = 0
        self.size = size
    def rudui(self):
        if (self.wei + 1) % self.size == self.tou:
            print("满了")
        else:
            data = int(input("请输入入队元素:"))
            self.queue[self.wei] = data
            self.wei = (self.wei + 1) % self.size
    def chudui(self):
        if self.tou == self.wei:
            print("空了")
        else:
            print("出队元素是:", self.queue[self.tou])
            self.tou = (self.tou + 1) % self.size
    def dayin(self):
        if self.wei > self.tou:
            for i in range(self.tou, self.wei):
                print(self.queue[i], end=" ")
            print("")
        elif self.tou > self.wei:
            for i in range(self.tou, self.size):
                print(self.queue[i], end=" ")
            for i in range(self.wei):
                print(self.queue[i], end=" ")
            print("")
        else:
            print("空的")
dl = Xhdl(10)
while True:
    pd = int(input("请输入编号:"))
    if pd == 1:
        dl.rudui()
    elif pd == 2:
        dl.chudui()
    elif pd == 3:
        dl.dayin()
    else:
        exit(0)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值