Exemplo n.º 1
0
def LinearSelect(arr, start_idx, end_idx, k):
    n = end_idx - start_idx + 1
    '''
    算法时间复杂度接近o(1)
  '''
    if n < 20:
        sort.QuickSort(arr, start_idx, end_idx)
        return arr[k - 1]
    '''
  每组5个元素进行排序,排序时间复杂度相当于O(1)
  并将每组的中味数放在数组的前边,进行排序,然后选取中位数
  '''
    for i in range(0, n / 5):
        sort.QuickSort(arr, i * 5, i * 5 + 4)
        arr[i], arr[i * 5 + 2] = Swap(arr[i], arr[i * 5 + 2])
    '''
  选取中位数,不需要这么写,递归的调用本方法即可
  sort.QuickSort(arr, 0, n/5)
  m = arr[2]
  '''
    m = LinearSelect(arr, 0, n / 5, n / 10)
    '''
  分割三部分
  '''
    small_n, equal_n = Partition(arr, m, 0, n - 1)
    if small_n > k:
        return LinearSelect(arr, 0, small_n - 1, k)
    elif small_n + equal_n < k:
        return LinearSelect(arr, small_n + equal_n, n - small_n - equal_n - 1,
                            k - small_n - equal_n)
    else:
        return arr[k - 1]
Exemplo n.º 2
0
                else:
                    k += 1
        '''
    k超越j,说明已经分配完毕
    '''
        if k > j:
            return i, j - i + 1

        while i < j and arr[j] > m:
            j -= 1

        if i < j:
            arr[i], arr[j] = Swap(arr[i], arr[j])
            '''
      如果和m相同,那么需要后移
      '''
            if arr[i] < m:
                i += 1
            j -= 1


if __name__ == '__main__':
    print('start...')
    arr = [156, 81, 127, 147, 43, 196, 46, 38, 181]
    k = len(arr) / 2
    m = LinearSelect(arr, 0, len(arr) - 1, k)
    print m

    sort.QuickSort(arr, 0, len(arr) - 1)
    print arr[k - 1]
Exemplo n.º 3
0
    testList.append(randomNum)
sort.IterativeMergeSort(testList)
end = time.time_ns()
runTime = (end - start) / 1000000
print("Run Time: {} ms.".format(runTime))
print()
print()

# Quick Sort
print("Quick Sort")
testList.clear()
start = time.time_ns()
for i in range(LIST_SIZE):
    randomNum = random.randint(0, LIST_SIZE * 10)
    testList.append(randomNum)
sort.QuickSort(testList)
end = time.time_ns()
runTime = (end - start) / 1000000
print("Run Time: {} ms.".format(runTime))

testList.clear()
start = time.time_ns()
for i in range(LIST_SIZE * 10):
    randomNum = random.randint(0, LIST_SIZE * 10)
    testList.append(randomNum)
sort.QuickSort(testList)
end = time.time_ns()
runTime = (end - start) / 1000000
print("Run Time: {} ms.".format(runTime))

testList.clear()
Exemplo n.º 4
0
        print(inList)
    elif sortName[0:3] == 'In':
        print(inList)
        sort.InsertionSort(inList)
        print(inList)
    elif sortName[0] == 'M':
        print(inList)
        sort.MergeSort(inList)
        print(inList)
    elif sortName[0] == 'I':
        print(inList)
        sort.IterativeMergeSort(inList)
        print(inList)
    elif sortName[0] == 'Q':
        print(inList)
        sort.QuickSort(inList)
        print(inList)
    elif sortName[0] == 'S':
        print(inList)
        sort.ShellSort(inList)
        print(inList)
elif output[0] == 'N':
    if sortName[0] == 'B':
        sort.BubbleSort(inList)
    elif sortName[0:3] == 'In':
        sort.InsertionSort(inList)
    elif sortName[0] == 'M':
        sort.MergeSort(inList)
    elif sortName[0] == 'I':
        sort.IterativeMergeSort(inList)
    elif sortName[0] == 'Q':
Exemplo n.º 5
0
def init(item):
    quicksort = sort.QuickSort(item)
    return quicksort
Exemplo n.º 6
0
    numberOfInt = sys.argv[2]
    mylist = []
    for i in range(int(numberOfInt)):
        randNumber = random.randint(1, int(numberOfInt))
        mylist.append(randNumber)

    if len(sys.argv) == 4:
        printString = sys.argv[3]
        if printString == 'PRINT':
            print("List before sorting:\n", mylist)
        else:
            print('you can only input PRINT as the last argument')

    if sortType == 'BubbleSort':
        mySort = sort.BubbleSort(mylist)
    elif sortType == 'InsertionSort':
        mySort = sort.InsertionSort(mylist)
    elif sortType == 'MergeSort':
        mySort = sort.MergeSort(mylist)
    elif sortType == 'IterativeMergeSort':
        mySort = sort.IterativeMergeSort(mylist)
    elif sortType == 'QuickSort':
        mySort = sort.QuickSort(mylist, 0, len(mylist) - 1)
    elif sortType == 'ShellSort':
        mySort = sort.ShellSort(mylist)

    if len(sys.argv) == 4:
        if printString == 'PRINT' and sortType == 'BubbleSort' or sortType == 'InsertionSort' or sortType == 'MergeSort' or sortType == 'IterativeMergeSort' or sortType == 'QuickSort' or sortType == 'ShellSort':
            print("List after sorting:\n ", mylist)
        else:
            print("You need to input sort type correctly!")