self.arrayset.resetColumnColor(index) def buildHeap(self): middle = len(self.arrayset.numList) // 2 self.checkpause(self.pause) for index in range(middle,-1,-1): self.dfs(index,len(self.arrayset.numList)) def swap(self,index1,index2): self.arrayset.numList[index1],self.arrayset.numList[index2] = \ self.arrayset.numList[index2],self.arrayset.numList[index1] self.arrayset.swapNumRect(index1,index2) height1,height2 = self.arrayset.getColumnHeight(index1),self.arrayset.getColumnHeight(index2) self.arrayset.setColumnHeight(index1,height2) self.arrayset.setColumnHeight(index2,height1) if __name__ == '__main__': array = getNumList(count = 100) import copy,random b = copy.deepcopy(array) b.sort(reverse=True) kth = random.randint(0,100) if kth < 0: kth = 0 if kth >= len(b): kth = len(b) - 1 print('the whole array sorted :',b) print('the %dth ele is %d' % (kth,b[kth - 1])) showSort(array,HeapKthThread,'heap-kth',kth = kth)
time.sleep(pause) if smallIndex != index: self.lock.acquire() self.arrayset.numList[index],self.arrayset.numList[smallIndex] = \ self.arrayset.numList[smallIndex],self.arrayset.numList[index] height1,height2 = self.arrayset.getColumnHeight(index),self.arrayset.getColumnHeight(smallIndex) self.arrayset.setColumnHeight(index,height2) self.arrayset.setColumnHeight(smallIndex,height1) self.arrayset.swapNumRect(index,smallIndex) self.lock.release() self.checkpause(pause) time.sleep(pause) self.lock.acquire() self.arrayset.resetColumnColor(smallIndex) self.lock.release() self.checkpause(pause) time.sleep(pause) self.checkpause(pause) time.sleep(pause) self.lock.acquire() self.arrayset.fixColumnColor(index) self.lock.release() self.checkpause() time.sleep(pause) self.arrayset.setOrderly() print(self.arrayset.numList) if __name__ == '__main__': array = [3,2,1,0,0,1,2,3] showSort([],SelectionThread,'selection')
self.arrayset.resetColumnColor(curindex) self.lock.release() self.checkpause() time.sleep(pause) break self.arrayset.curColumnColor(index) self.checkpause() time.sleep(pause) self.lock.acquire() self.arrayset.resetColumnColor(tempIndex) self.lock.release() self.checkpause() time.sleep(pause) curindex -= 1 tempIndex -= 1 self.lock.acquire() self.arrayset.resetColumnColor(tempIndex) self.lock.release() self.checkpause() time.sleep(pause) for index in range(len(self.arrayset.columnColorList)): self.arrayset.fixColumnColor(index) self.arrayset.setOrderly() if __name__ == '__main__': # arrayset = ArraySet([3,2,1,0,0,1,7,2,3]) # showSort([3,2,1,0,0,1,7,2,3],InsertSortThread) showSort(None, InsertSortThread, 'insert')
def merge(self, left, middle, right): index1 = left index2 = middle + 1 res = [] while index1 <= middle and index2 <= right: if self.arrayset.numList[index1] <= self.arrayset.numList[index2]: res.append(self.arrayset.numList[index1]) index1 += 1 else: res.append(self.arrayset.numList[index2]) index2 += 1 if index1 <= middle: while index1 <= middle: res.append(self.arrayset.numList[index1]) index1 += 1 if index2 <= right: while index2 <= right: res.append(self.arrayset.numList[index2]) index2 += 1 index = left for num in res: self.arrayset.numList[index] = num index += 1 if __name__ == '__main__': # array = getNumList(count=10) # th = MergeThread(ArraySet(array),None) # th.start() showSort(None, MergeThread, 'merge')
time.sleep(self.pause) if index != maxInd: self.swap(index,maxInd) self.lock.acquire() self.arrayset.resetColumnColor(index) if leftInd < last: self.arrayset.resetColumnColor(leftInd) if rightInd < last: self.arrayset.resetColumnColor(rightInd) self.lock.release() self.dfs(maxInd,last) self.arrayset.resetColumnColor(index) def buildHeap(self): middle = len(self.arrayset.numList) // 2 self.checkpause(self.pause) for index in range(middle,-1,-1): self.dfs(index,len(self.arrayset.numList)) def swap(self,index1,index2): self.arrayset.numList[index1],self.arrayset.numList[index2] = \ self.arrayset.numList[index2],self.arrayset.numList[index1] self.arrayset.swapNumRect(index1,index2) height1,height2 = self.arrayset.getColumnHeight(index1),self.arrayset.getColumnHeight(index2) self.arrayset.setColumnHeight(index1,height2) self.arrayset.setColumnHeight(index2,height1) if __name__ == '__main__': showSort(None,HeapThread,'heap')
self.lock.release() self.checkpause() time.sleep(pause) self.lock.acquire() if latterIndex != index: self.arrayset.resetColumnColor(latterIndex) self.lock.release() self.checkpause() time.sleep(pause) latterIndex = curIndex curIndex -= step self.lock.acquire() # if step == 1: # self.arrayset.fixColumnColor(latterIndex) # else: self.arrayset.resetColumnColor(index) self.arrayset.resetColumnColor(latterIndex) if curIndex >= 0: self.arrayset.resetColumnColor(curIndex) self.lock.release() self.checkpause() time.sleep(pause) for index in range(len(self.arrayset.numList)): self.arrayset.fixColumnColor(index) self.arrayset.setOrderly() if __name__ == '__main__': array = [3, 2, 1, 0, 0, 1, 2, 3, 7, 4] showSort([], ShellThread, 'shell')
lastIndex = len(self.arrayset.numList) - 1 while lastIndex > -1: for index in range(0,lastIndex): colorTemp1 = self.arrayset.columnColorList[index] colorTemp2 = self.arrayset.columnColorList[index + 1] self.checkpause() self.lock.acquire() self.arrayset.columnColorList[index] = red if self.arrayset.numList[index] > self.arrayset.numList[index + 1]: self.arrayset.numList[index], self.arrayset.numList[index + 1] = self.arrayset.numList[index + 1], self.arrayset.numList[index] self.arrayset.columnHeightList[index], self.arrayset.columnHeightList[index + 1] = self.arrayset.columnHeightList[index + 1], self.arrayset.columnHeightList[index] self.arrayset.columnColorList[index + 1] = red self.arrayset.swapNumRect(index,index + 1) # please remember to swap 2 columns' information. self.lock.release() time.sleep(pausetime) self.checkpause() self.lock.acquire() self.arrayset.columnColorList[index] = colorTemp1 self.arrayset.columnColorList[index + 1] = colorTemp2 self.lock.release() self.checkpause() self.arrayset.columnColorList[lastIndex] = blue lastIndex -= 1 self.checkpause() self.arrayset.columnColorList[0] = blue self.arrayset.setOrderly() return if __name__ == '__main__': showSort(None,BubbleSortThread,'bubble')
self.lock.acquire() self.arrayset.curColumnColor(left) self.lock.release() self.checkpause(self.pause) time.sleep(self.pause) if self.arrayset.numList[left] <= base: self.lock.acquire() self.arrayset.resetColumnColor(left) self.lock.release() self.checkpause(self.pause) time.sleep(self.pause) left += 1 else: break self.swapNum(left,right) self.arrayset.swapNumRect(left, right) if left != right: self.lock.acquire() self.arrayset.resetColumnColor(right) self.lock.release() self.checkpause(self.pause) time.sleep(self.pause) height1, height2 = self.arrayset.getColumnHeight(left), self.arrayset.getColumnHeight(right) self.arrayset.setColumnHeight(left, height2) self.arrayset.setColumnHeight(right, height1) return left if __name__ == '__main__': showSort(None,QuickThread,'quick')