python 冒泡排序

11年前
 for i in range(len(self.teamList), 0, -1):              for j in range(0, i - 1):                  if self.teamList[j].getScore() < self.teamList[j + 1].getScore():                      temp = self.teamList[j];'''移动最小到最后一位'''                      self.teamList[j] = self.teamList[j + 1];                      self.teamList[j + 1] = temp;              异常捕获    class teamInfoNotFindError(Exception):      def __init__(self, value):          self.value = value      def __str__(self):          return repr(self.value)         try:      raise teamInfoNotFindError("TeamInfo Not Find")  except teamInfoNotFindError as e:      print(e.value)              类定义    class teamInfo:      teamId = ""      winNum = 0      failNum = 0      midNum = 0      __sorce = -1      ranking = -1      __log=""      def __init__(self, teamId):          self.teamId = teamId      def addWin(self,teamid):          self.winNum = self.winNum + 1          self.__log=self.__log+(teamid+":3--")      def addFail(self,teamid):          self.failNum = self.failNum + 1          self.__log=self.__log+(teamid+":0--")      def addMid(self,teamid):          self.midNum = self.midNum + 1          self.__log=self.__log+(teamid+":1--")      def addScore(self, score,teamid):          if self.isInput(teamid)==-1:              if score == 3:                  self.addWin(teamid)              elif score == 1:                  self.addMid(teamid)              elif score == 0:                  self.addFail(teamid)              else:                  print("输入错误!!!")      def getScore(self):          __sorce = self.winNum * 3 + self.midNum * 1          return __sorce      def addSetRandking(self, ranking):          self.ranking = ranking      def isInput(self,teamid):          currentlog_a=self.__log.split("--")          for i in range(0,len(currentlog_a)):              '''存在返回0'''              if teamid==currentlog_a[i].split(":")[0]:                  print(self.teamId+"与"+currentlog_a[i].split(":")[0]+"比赛结果已录入")                   return 0          return -1         class allTeamInfo:      teamList = []      def addTeam(self, teamInfo):          if len(self.teamList)<=1000:              self.teamList.append(teamInfo)          else:              print("比赛队伍总数<=1000")      '''排序'''      def reSetRandking(self):          for i in range(len(self.teamList), 0, -1):              for j in range(0, i - 1):                  if self.teamList[j].getScore() < self.teamList[j + 1].getScore():                      temp = self.teamList[j];'''移动最小到最后一位'''                      self.teamList[j] = self.teamList[j + 1];                      self.teamList[j + 1] = temp;              '''此时设置ranking'''              self.teamList[i - 1].ranking = i      def selectTeamInfo(self, teamid):          for i in range(0, len(self.teamList)):              if teamid == self.teamList[i].teamId:                  return i          return - 1         #类调用,    def AddGameResult(Team1Id, Team1Score, Team2Id, Team2Score):      '''          输入参数:       Team1Id         队伍1ID                    Team1Score      队伍1得分                    Team2Id         队伍2ID                    Team2Score      队伍2得分          输出参数: 无          返回值:  无          方法说明:                  输入队伍1对队伍2的比赛结果      '''      '''ID相同'''           iT1=int(Team1Id)      iT2=int(Team2Id)      if iT1>=0 and iT1<=65535 and iT2>=0 and iT2<=65535:          if Team1Id!=Team2Id:              t1 = g_allteaminfo.selectTeamInfo(Team1Id)              t2 = g_allteaminfo.selectTeamInfo(Team2Id)              if t1 == -1:                  tem = teamInfo(Team1Id)                  tem.addScore(Team1Score,Team2Id)                  g_allteaminfo.addTeam(tem)              else:                  g_allteaminfo.teamList[t1].addScore(Team1Score,Team2Id)                           if t2 == -1:                  tem = teamInfo(Team2Id)                  tem.addScore(Team2Score,Team1Id)                  g_allteaminfo.addTeam(tem)              else:                  g_allteaminfo.teamList[t2].addScore(Team2Score,Team1Id)          else:              print("不能和自己比赛")      else:          print("team1或team2的ID超过限制")         def GetTeamResult(TeamId, TeamResultList):      '''          输入参数: TeamId            队伍ID          输出参数: TeamResultList    返回队伍的最新赛况,返回一个list,内容如下:                    [队伍ID, 胜的场数, 平的场数, 负的场数, 积分, 名次]                    提醒:调用者不负责清空这个list,必须在此函数内先处理清空                             返回值:  成功返回0,失败返回-1。若没有输入过该队伍的比赛,返回失败          方法说明:                  获取队伍的最新赛况      '''      g_allteaminfo.reSetRandking()      teamindex = g_allteaminfo.selectTeamInfo(TeamId)      if teamindex == -1:          raise teamInfoNotFindError("TeamInfo Not Find")      else:          try:              teaminfo = g_allteaminfo.teamList[teamindex]              reslut = [teaminfo.teamId,teaminfo.winNum,teaminfo.midNum,teaminfo.failNum,teaminfo.getScore(),teaminfo.ranking]              TeamResultList[0]=reslut              return 0          except Exception:              return -1  def Clear():      '''          输入参数: 无          输出参数: 无          返回值:  无          方法说明:                  清空所有队伍以及其赛况信息      '''      pass  g_allteaminfo = allTeamInfo()  TeamResultList = ["","","","","",""]  AddGameResult("0001", 1, "0002", 1)  AddGameResult("0001", 0, "0003", 3)  AddGameResult("0001", 3, "0004", 0)  AddGameResult("0001", 1, "0005", 1)  AddGameResult("0001", 0, "0006", 3)  AddGameResult("0002", 0, "0003", 3)  AddGameResult("0002", 3, "0004", 0)  AddGameResult("0002", 1, "0005", 1)  AddGameResult("0002", 0, "0006", 3)  AddGameResult("0003", 3, "0004", 0)  AddGameResult("0003", 1, "0005", 1)  AddGameResult("0003", 0, "0006", 3)  AddGameResult("0004", 1, "0005", 1)  AddGameResult("0004", 0, "0006", 3)  AddGameResult("0005", 0, "0006", 3)  AddGameResult("0006", 0, "0005", 3)    try:      GetTeamResult("0001",TeamResultList)      print(TeamResultList[0])      GetTeamResult("0002",TeamResultList)      print(TeamResultList[0])      GetTeamResult("0003",TeamResultList)      print(TeamResultList[0])      GetTeamResult("0004",TeamResultList)      print(TeamResultList[0])      GetTeamResult("0005",TeamResultList)      print(TeamResultList[0])      GetTeamResult("0006",TeamResultList)      print(TeamResultList[0])  except teamInfoNotFindError as e:      print(e.value)