Python排序+去重算法

去重加排序第一个方法 桶排序算法

# -*- coding: utf-8 -*-
# 去重加排序第一个方法 桶排序算法
import random

fenshu = int(input("请输入考试满分:"));
renshu = int(input("请输入考试人数:"));
sortsre = input("倒叙/正序 请选择 Y/N:");
while sortsre!="Y" and sortsre!="N":
    print("您输入的参数有误!");
    sortsre = input("倒叙/正序 请选择 Y/N:");

classmates = [0]*(fenshu+1);

if sortsre=="Y":
    paixu = range(fenshu,-1,-1);
elif sortsre=="N":
    paixu = range(fenshu);

#模拟数据生成
for num in range(fenshu):
    shu = random.randint(0,fenshu);
    classmates[shu] = 1;
    print("%d分" % (shu));

print("");
print("");

#打印数据
for num in paixu:
    if classmates[num] == 1:
        print("%d分" % (num));

去重加排序第二个方法 快速排序算法

# -*- coding: utf-8 -*-
# 去重加排序第二个方法 快速排序算法
import random

fenshu = int(input("请输入考试满分:"));
renshu = int(input("请输入考试人数:"));

classmates = [0]*(renshu);

#模拟数据生成
for num in range(renshu):
    shu = random.randint(0,fenshu);
    classmates[num] = shu;
    print("%d分" % (shu));

print("");
print("");

def chongzu(left,right):
    if left>right:
        return;
    jizhun = classmates[left];
    i = left;
    j = right;
    while i!=j:
        while (classmates[j]>=jizhun) and (i1;
        while (classmates[i]<=jizhun) and (i1;
        if(i1);
    chongzu(i+1,right);

chongzu(0,len(classmates)-1)

#打印数据
quchong = "";
for num in classmates:
    if quchong != num:
        print("%d分" % (num));
        quchong = num;

你可能感兴趣的:(Python学习笔记)