Giter Club home page Giter Club logo

sorting_visualization's Introduction

Sorting Visualization and Audibilization

Latest Update2 (05/04/2019)

  • Great News: Now we can get the Voice of Sorting Algorithm simultaneously, and this is so funny that we should all have a try!!!

Latest Update

  • Add three more sorting algorithms: CombSort, RadixSort, MonkeySort
  • Now you can record the whole sorting procedure to *avi videos.
  • Adding -r in command line can get you Re-sampled data.
  • Adding -s in command line can get you Sparse data.

Introduction

This repository is a demo of visualizing 12 types of Sorting Algorithms. It aims to make Sorting Algorithms easier for programmers to understand. Also, you can see the difference of Time Complexity between different sorting algorithms.

Sorting Algorithm AverageTime Complexity Bad Time Complexity Stability
Bubble Sort O(N^2) O(N^2) YES
Insertion Sort O(N^2) O(N^2) YES
Shell Sort O(N^5/4) O(N^2) NO
Selection Sort O(N^2) O(n^2) NO
Heap Sort O(NlogN) O(NlogN) NO
Merge Sort O(NlogN) O(NlogN) YES
Quick Sort O(NlogN) O(N^2) NO
Bucket Sort O(N) O(N) YES
Cycle Sort O(N) O(N^2) NO
Comb Sort O(N^2) O(N^2) NO
Radix Sort O(N) O(N) YES
Monkey Sort O(N!) O(N!) YES

Demos

Dependencies

  • python3.x
  • cv2
  • numpy
  • pygame

Quick Start

  1. Check all dependencies installed

    This command can help you install all the dependent packages

    pip install -r requirements.txt

  2. Clone this repository

    git clone [email protected]:ZQPei/Sort_Visualization.git

  3. Start

    python main.py -l 512 -t BubbleSort

    • -l --length: Array Length
    • -t --sort-type: Sorting Type. Default type is BubbleSort
      • BubbleSort
      • InsertionSort
      • ShellSort
      • SelectionSort
      • HeapSort
      • MergeSort
      • QuickSort
      • BucketSort
      • CycleSort
      • CombSort
      • RadixSort(LSD)
      • MonkeySort
    • -i --interval: Time Interval of next frame
    • -r --resample: Get Resampled Array
    • -s --sparse: Sparse Array
    • -n --no-record: Don't record to *.avi video!
    • --silent: No voice output
    • --sound-interval: Time of sound

May you have fun!

sorting_visualization's People

Contributors

zqpei avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sorting_visualization's Issues

这个堆稍微快点

def HeapSort(ds):

def big_endian(ds, root, end):
    """
    将堆的末端子节点作调整,使得子节点永远小于父节点  
    :param: ds    get_figure2draw  
    :param: root int 开始(list index)  
    :param: end int 结束(list index)  
    """
    arr = ds.data
    child = root * 2 +  1 # 左子节点
    while child <= end: 
        if child + 1 <= end and arr[child] < arr[child + 1] :
        # 判断右child是否存在,如果存在则和另外一个同级节点进行比较
            child += 1
        if arr[root] < arr[child]:
            ds.swap(root, child)
            ## 下钻到下层
            root = child
            child = root * 2 + 1
        else:
            break


def HeapSort(ds):
    """
    堆排序
    """
    Length = ds.length - 1
    first = Length // 2 
    for root in range(first, -1, -1):
    # 建堆
        big_endian(ds, root, Length)
    for end in range(Length, 0, -1):
        # 堆顶是最大的值,放到最末尾,长度-1后继续建堆
        ds.swap(0, end) 
        big_endian(ds, 0, end - 1) 

重复部分可以不用再排序

# print(repeatIdxs)

def CycleSort(ds):
    """
    环排序只适用于整数排序,且数正好范围在[0,N-1]内,且只有少量重复元素,不稳定
    """
    assert isinstance(ds, get_figure2draw), "Type Error"
    assert isinstance(ds.data[0], int), "Type Error"

    Length = ds.length
    dt = ds.data
    # 重复元素的列表
    repeatIdxs = []
    for i in range(Length):
        currIdx = i
        # 查找当前值应该所在的索引 
        shouldIdx = sum([1  if dt[currIdx] > v else 0 for v in  dt ])
        # cycle 跑圈 (忽视重复值)
        while currIdx != shouldIdx and dt[currIdx] != dt[shouldIdx]: # 当前位置不是应该所在的位置的时候
            ds.swap(currIdx, shouldIdx) # 交换 currIdx, shouldIdx 的值
            shouldIdx = sum([1 if dt[currIdx] > v else 0 for v in  dt ])
        # 可能包含重复的值记录
        if dt[currIdx] == dt[shouldIdx] and  currIdx != shouldIdx:
            repeatIdxs.append([currIdx, shouldIdx])
    # 重复元素插值
    for rep in repeatIdxs:
        if dt[rep[0]] == dt[rep[1]]:
            ds.set_val(rep[0], dt[max(rep[0] - 1, 0)])

对了 我把 你的这个项目 自己稍微改了下传gihub可以么?

problem?

Is this problem from the code or from my pc?

PS C:\Users\cruxv\Desktop\Python\Ordinatore Interi\Sorting_Visualization-master> python3 main.py pygame 2.5.2 (SDL 2.28.3, Python 3.11.5) Hello from the pygame community. https://www.pygame.org/contribute.html Preparing sound dict ... Traceback (most recent call last): File "C:\Users\cruxv\Desktop\Python\Ordinatore Interi\Sorting_Visualization-master\main.py", line 55, in <module> ds=DataSeq(Length, time_interval=Interval, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cruxv\Desktop\Python\Ordinatore Interi\Sorting_Visualization-master\src\data.py", line 50, in __init__ self.GetSoundArray() File "C:\Users\cruxv\Desktop\Python\Ordinatore Interi\Sorting_Visualization-master\src\data.py", line 116, in GetSoundArray self.sound_mixer = list(map(pygame.sndarray.make_sound, self.sound_list)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cruxv\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pygame\sndarray.py", line 92, in make_sound return mixer.Sound(array=array) ^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: Array must be 2-dimensional for stereo mixer

question

请问想把这个窗体展示在前端该怎么操作呢

请教一下这个mark1和mark2到底有啥用

你难道没发现这个mark2永远都是None吗,我不知道这个参数是怎么传递过去的,也不知道那个mark2的BLUE有啥用,永远都不可能执行这一行代码,自己试了一下交换的时候的标记也只见到红色的标记,并没有见到蓝色的标记
QQ图片20190823013007

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.