コード例 #1
0
def cocktailSort(data: list) -> list():
    '''
    This implements cocktail sort.

    Parameters
    ----------
    data : list
        This takes the list to be sorted.

    Returns
    -------
    list()
        This returns the sorted list.

    '''
    #Since range is exclusive, initial values are 1 off respectively
    start = 0
    end = len(data) - 1

    while (end > start):

        #the forward pass
        for index in range(start, end):
            if (data[index] > data[index + 1]):
                tools.swap(data, index, index + 1)
        end -= 1
        #the backwards pass
        for index in range(end, start, -1):
            if (data[index] < data[index - 1]):
                tools.swap(data, index, index - 1)
        start += 1
        print(data)
    return data
コード例 #2
0
def partition(data: list, low: int, high: int) -> int:
    '''
    The partition function of quick sort. It uses the 'high' element as
    the pivot. It rearranges the array so that all values to the left (starting
    from low) are less than the pivot and all values to the right are greater
    (up to high.)
    
    Parameters
    ----------
    data : list
        The list to be partitioned.
    low : int
        The lowest index to be affected.
    high : int
        The highest index to be affected.

    Returns
    -------
    int
        The index of the pivot value.

    '''
    pivot = data[high]
    i = low - 1
    for j in range(low, high):
        if (data[j] < pivot):
            i += 1
            tools.swap(data, i, j)
    tools.swap(data, i + 1, high)
    return i + 1
コード例 #3
0
    def cocktailSort(self):
        '''
        This sorts the interanl list via cocktail sort.

        Returns
        -------
        None.

        '''
        self._renderName("Cocktail Sort")
        start = 0
        end = len(self._data)-1
    
        while (end > start):
        
            #the forward pass
            for index in range(start, end):
                if (self._data[index] > self._data[index+1]):
                    tools.swap(self._data, index, index+1)
                    self._updateImage()
            end-=1
            #the backwards pass
            pygame.time.wait(3)
            for index in range(end, start, -1):
                if (self._data[index] < self._data[index-1]):
                    tools.swap(self._data, index, index-1)
                    self._updateImage()
            start+=1
                
                
                
コード例 #4
0
    def partition(self, data: list, low: int, high: int) -> int:
        '''
        This is th partitioning function for quicksort. It places all values
        higher than the pivot (high index) to the right of
        the pivot, and it places the lower values to the left of the pivot.

        Parameters
        ----------
        data : list
            The list to be acted on.
        low : int
            The lower index of the sub list to be sorted.
        high : int
            The higher index of the sub list to be sorted.

        Returns
        -------
        int
            The new pivot point.

        '''
        pivot = data[high]
        i = low - 1
        for j in range(low, high):
            if (data[j] < pivot):
                i+=1
                tools.swap(data, i, j)
                pygame.time.wait(15)
                self._updateImage()
        tools.swap(data, i+1, high)
        self._updateImage()
        return i+1
コード例 #5
0
    def selectionSort(self):
        '''
        This sorts the internal list via selection sort.

        Returns
        -------
        None.

        '''
        self._renderName("Selection Sort")
        for i in range(0, len(self._data)-1):
            minIndex = i
            for j in range(i+1, len(self._data)-1):
                if (self._data[j] < self._data[minIndex]):
                    minIndex = j
            tools.swap(self._data, minIndex, i)
            pygame.time.wait(50)
            self._updateImage()
コード例 #6
0
def selectionSort(data: list) -> list:
    '''
    Sorts the list via selection sort.

    Parameters
    ----------
    data : list
        The list to be sorted.

    Returns
    -------
    list
        The sorted list.

    '''
    for i in range(0, len(data)):
        minIndex = i
        for j in range(i + 1, len(data)):
            if (data[j] < data[minIndex]):
                minIndex = j
        tools.swap(data, minIndex, i)
    return data
コード例 #7
0
    def insertionSort(self):
        '''
        This sorts the internal list via insertion sort.

        Returns
        -------
        None.

        '''
        self._renderName("Insertion Sort")
        for i in range(1, len(self._data)):
            key = i
            while (key >= 1) and (self._data[key-1] > self._data[key]):
                self._data = tools.swap(self._data, key, key-1)
                key-=1
                self._updateImage()
コード例 #8
0
    def bubbleSort(self):
        '''
        This sorts the internal list via bubble sort.

        Returns
        -------
        None.

        '''
        self._renderName("Bubble Sort")
        for i in range (0, len(self._data)-1):
            currIndex = 0
            while (currIndex < len(self._data) - i - 1): 
                if (self._data[currIndex] > self._data[currIndex+1]):
                    self._data = tools.swap(self._data, currIndex, currIndex+1)
                    self._updateImage()
                currIndex += 1
コード例 #9
0
def insertionSort(data: list) -> list:
    '''
    Sorts the list via insertion sort.

    Parameters
    ----------
    data : list
        The list to be sorted.

    Returns
    -------
    list
        The sorted list.

    '''
    for i in range(1, len(data)):
        key = i
        while (key >= 1) and (data[key - 1] > data[key]):
            data = tools.swap(data, key, key - 1)
            key -= 1
    return data
コード例 #10
0
def bubbleSort(data: list) -> list:
    '''
    Sorts the given list using bubble sort.

    Parameters
    ----------
    data : list
        The list to be sorted.

    Returns
    -------
    list
        The sorted list.

    '''
    length = len(data)
    for i in range (0, length-1):
        currIndex = 0
        while (currIndex < length - i - 1):
            if (data[currIndex] > data[currIndex+1]):
                data = tools.swap(data, currIndex, currIndex+1)
            currIndex += 1
    return data