コード例 #1
0
def startAlgorithm():
    global data
    if not data: return

    if algMenu.get() == 'Quick Sort':
        quick_sort(data, 0, len(data) - 1, drawData, speedScale.get())

    elif algMenu.get() == 'Bubble Sort':
        bubble_sort(data, drawData, speedScale.get())

    elif algMenu.get() == 'Insertion Sort':
        insertion_sort(data, drawData, speedScale.get())

    elif algMenu.get() == 'Merge Sort':
        merge_sort(data, drawData, speedScale.get())

    elif algMenu.get() == 'Selection Sort':
        selection_sort(data, drawData, speedScale.get())

    elif algMenu.get() == 'Selection Sort':
        selection_sort(data, drawData, speedScale.get())

    elif algMenu.get() == 'Radix Sort':
        radix_sort(data, drawData, speedScale.get())
    drawData(data, ['green' for x in range(len(data))])
 def reinsert(self, old_paths, new_paths, population):
     # 父子融合
     tmp_paths = old_paths
     tmp_paths.extend(new_paths)
     # 优胜劣汰
     quick_sort(tmp_paths)
     return tmp_paths[:population]
コード例 #3
0
def satrtAlgo():
    global data
    if not data: return
    if (algMenu.get() == 'Quick Sort'):
        quick_sort(data, 0, len(data) - 1, drawData, speedScale.get())
    elif (algMenu.get() == 'Bubble Sort'):
        bubble_sort(data, drawData, speedScale.get())
    elif (algMenu.get() == 'Merge Sort'):
        merge_sort(data, drawData, speedScale.get())
    drawData(data, ['green' for x in range(len(data))])
コード例 #4
0
def StartAlgo():
    global data
    if not data: return
    if algMenu.get() == "Quick Sort":
        quick_sort(data, 0, len(data) - 1, draw_data, speedScale.get())

    if algMenu.get() == "Bubble Sort":
        bubbleSort(data, draw_data, speedScale.get())
    elif algMenu.get() == "Merge Sort":
        merge_sort(data, draw_data, speedScale.get())
    draw_data(data, ["yellow" for x in range(len(data))])
コード例 #5
0
def startAlgo():
    global data
    if not data: return
    if algMenu.get() == 'Quick Sort':
        quick_sort(data, 0,
                   len(data) - 1, drawData, speedScale.get(), sizeEntry.get())
    elif algMenu.get() == 'Bubble Sort':
        bubble_sort(data, drawData, speedScale.get(), sizeEntry.get())
    elif algMenu.get() == 'Merge Sort':
        start_mergeSort(data, drawData, speedScale.get(), sizeEntry.get())
    drawData(data, sizeEntry.get(), ['navy' for x in range(len(data))])
コード例 #6
0
def startAlgorithm():
    global data
    #check which algorithm is selected
    if algoMenu.get() == 'Bubble Sort':
        bubbleSort(data, draw, speed.get())
    elif algoMenu.get() == 'Merge Sort':
        mergeSort(data, draw, speed.get())
    elif algoMenu.get() == 'Selection Sort':
        selectionSort(data, draw, speed.get())
    elif algoMenu.get() == 'Insertion Sort':
        insertionSort(data, draw, speed.get())
    elif algoMenu.get() == 'Quick Sort':
        quick_sort(data, 0, len(data) - 1, draw, speed.get())
        draw(data, ['green' for x in range(len(data))])
コード例 #7
0
def StartAlgorithm():
    global data

    if not data: return

    if algMenu.get() == 'Quick Sort':
        quick_sort(data, 0, len(data) - 1, drawData, speedScale.get())
        drawData(data, ['green' for x in range(len(data))])

    elif algMenu.get() == 'Bubble Sort':
        bubble_sort(data, drawData, speedScale.get())

    elif algMenu.get() == 'Merge Sort':
        print("Merge Sort Selected")
コード例 #8
0
def startAlgorithm():
    global array
    if not array:
        return

    # get the speed of the algorithm visualiser
    sizeNum = sizeEntry.get()
    speed = 10 / (sizeNum * pow(sizeNum, 0.5))

    # visualise an algorithm
    if algoMenu.get() == "Insertionsort":
        insertion_sort(array, drawArray, speed)
    if algoMenu.get() == "Mergesort":
        merge_sort(array, drawArray, speed)
    if algoMenu.get() == "Bubblesort":
        bubble_sort(array, drawArray, speed)
    if algoMenu.get() == "Quicksort":
        quick_sort(array, drawArray, speed)
    if algoMenu.get() == "Heapsort":
        heap_sort(array, drawArray, speed)
コード例 #9
0
def binary(lis, elem):
    comparisons = 0
    array_accesses = 0
    (comp1, access1) = quick_sort(lis, 0, len(lis) - 1, 0, 0)
    comparisons += comp1
    access1 += access1
    (result, comparisons,
     array_accesses) = binary_search(lis, 0,
                                     len(lis) - 1, elem, comparisons,
                                     array_accesses)
    if result != -1:
        print("Binary search:")
        print("Element is present at index: " + str(result))
        print("No. comparisons: " + str(comparisons) +
              ", no. array accesses: " + str(array_accesses))
    else:
        print("Element is not present in array")
コード例 #10
0
ファイル: cmpTime.py プロジェクト: JZ10UJS/zj_files
data1.sort()
e0 = time.clock()
print 'The python sorted:', e0-s0

s7 = time.clock()
shellSort.shellSort(data7)
e7 = time.clock()
print 'shellSort:', e7-s7

s1 = time.clock()
mergeSort.mergeSort(data1)
e1 = time.clock()
print 'mergeSort:', e1-s1

s5 = time.clock()
quickSort.quick_sort(data5,0,len(data5))
e5 = time.clock()
print 'quick_sort:', e5-s5

s6 = time.clock()
quickSort.qSort(data6)
e6 = time.clock()
print 'qSort:', e6-s6

s3 = time.clock()
insertionSort.insertionSort(data3)
e3 = time.clock()
print 'insertionSort:', e3-s3

s8 = time.clock()
insertionSort.insertSort(data8, 0, len(data8))
コード例 #11
0
    bucket_sort(temp, how_many // 100)
    temp = array[:]
    comb_sort(temp)
    temp = array[:]
    counting_sort(temp)
    temp = array[:]
    heap_sort(temp)
    temp = array[:]
    insertion_sort(temp)
    temp = array[:]
    shell_sort(temp)
    temp = array[:]
    lsd_radix_sort(temp)
    temp = array[:]
    pigeonhole_sort(temp)
    temp = array[:]
    selection_sort(temp)
    temp = array[:]
    (comparisons, array_accesses, additional_space) = merge_sort(temp)
    print("Merge sort:")
    print("No. comparisons: " + str(comparisons) + ", no. array accesses: " +
          str(array_accesses) + ", no. additional space required: " +
          str(additional_space))
    temp = array[:]
    (comparisons, array_accesses) = quick_sort(temp, 0, len(temp) - 1, 0, 0)
    additional_space = 0
    print("Quick sort:")
    print("No. comparisons: " + str(comparisons) + ", no. array accesses: " +
          str(array_accesses) + ", no. additional space required: " +
          str(additional_space))
コード例 #12
0
    paths = alg.init_population(starting_point, objectives, Kca)

    for p in range(len(paths)):
        paths[p].fitness = alg.get_fitness(paths[p], occ_grid, pri_grid,
                                           starting_point, end_point,
                                           privacy_sum, obstacle_num)

    max_p = max(paths, key=lambda x: x.fitness)

    max_f = -5
    count = 0
    fitnv = []
    print(len(paths))

    for i in range(max_generation):
        quick_sort(paths)
        if max_f < paths[0].fitness:
            max_f = paths[0].fitness
            print('\033[94m Current maximum fitness:\033[0m\033[92m ' +
                  str(max_f) + '\033[0m\033[94m, Generation:\033[0m\033[92m ' +
                  str(i) + ' \033[0m')
            for j in range(len(paths[0].points)):
                print(paths[0].points[j])
            print("the generation", i, len(paths[0].points))
            alg.get_fitness(paths[0], occ_grid, pri_grid, starting_point,
                            end_point, privacy_sum, obstacle_num)
        '''
        p1 = alg.tournament_select(paths)
        p2 = alg.tournament_select(paths)

        new_path = []
コード例 #13
0
ファイル: TEST.py プロジェクト: anthonybench/Algorithms
print("Selection Sort Timer: ", end-start, "s", sep='')
print("List Returned:", test1)
space(1)
border('M')

# MERGE SORT TEST
start = time.time()
merge_sort(test2)
end = time.time()
border('M')
space(1)
print("Merge Sort Timer: ", end-start, "s", sep='')
print("List Returned:", test2)
space(1)
border('M')

# QUICK SORT TEST
start = time.time()
quick_sort(test3, 0, MAX - 1)
end = time.time()
border('M')
space(1)
print("Quick Sort Timer: ", end-start, "s", sep='')
print("List Returned:", test3)
space(1)

# print closing
border('D')
space(3)

コード例 #14
0
ファイル: bit_search.py プロジェクト: vbirdchong/LearnPython
	if list[middle] < value:
		return bit_search(list, value, middle+1, high)
	elif list[middle] > value:
		return bit_search(list, value, low, middle-1)
	else:
		return middle


def bit_search_by_loop(list, value, low, high):
	if low > high:
		return -1

	while(True):
		middle = (low + high) / 2
		if (list[middle] < value):
			low = middle + 1
		elif (list[middle] > value):
			high = middle - 1
		else:
			return middle


if __name__ == '__main__':
	lis = [3, 987, 6, 22, 9, 333, 22, 44, 9999, 555, 999, 1234, 1235, 1255, 1288]
	val = 1235
	sortList = quick_sort(lis)
	ret = bit_search_by_loop(sortList, val, 0, len(lis))
	print sortList
	print ret, lis[ret]

コード例 #15
0
ファイル: source.py プロジェクト: rossmfadams/AlgorithmsP2
def main():
    from quickSort import q_count

    sys.setrecursionlimit(10000)

    # instantiate write to file
    write_file = WriteToFile()
    merge_sort = MergeSort()

    # 100 list
    first_list, second_list, third_list, fourth_list = arrayGenerator.generate_random_list_number(100)
    print("{}, {}, {}, {}".format(len(first_list), len(second_list), len(third_list), len(fourth_list)))

    # sorting 100 list with bubblesort
    start_time = time.perf_counter()
    first_list, comparisons = bubbleSort.bubble_sort(first_list)
    end_time = time.perf_counter()
    print("Bubble Sort time: %s seconds" % (end_time - start_time))
    print("Bubble Sort comparisions: ", comparisons)

    # Creating the semisorted list
    first_list = arrayGenerator.randomizer(first_list)

    # sorting 100 semisorted list with bubblesort
    start_time_semisorted = time.perf_counter()
    first_list, semisorted_comparisons = bubbleSort.bubble_sort(first_list)
    end_time_semisorted = time.perf_counter()
    print("Bubble Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    print("Bubble Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Bubble Sort data to the file
    write_file.write_to_file('Bubble Sort', 'O(n^2)', 'O(n)', 100, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)

    # sorting 100 list with selectionsort
    start_time = time.perf_counter()
    second_list, comparisons = selectionSort.selection_sort(second_list)
    end_time = time.perf_counter()
    print("Selection Sort time: %s seconds" % (end_time - start_time))
    print("Selection Sort comparisions: ", comparisons)

    # Creating the semisorted list
    second_list = arrayGenerator.randomizer(second_list)

    # sorting 100 semisorted list with selectionsort
    start_time_semisorted = time.perf_counter()
    second_list, semisorted_comparisons = selectionSort.selection_sort(first_list)
    end_time_semisorted = time.perf_counter()
    print("Selection Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    print("Selection Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Selection Sort data to the file
    write_file.write_to_file('Selection Sort', 'O(n^2)', 'O(n^2)', 100, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)
    
    # sorting 100 list with mergesort
    start_time = time.perf_counter()
    third_list = merge_sort.merge_sort(third_list)
    end_time = time.perf_counter()
    print("Merge Sort time: %s seconds" % (end_time - start_time))
    comparisons = merge_sort.count
    print("Merge Sort comparisions: ", merge_sort.count)

    # Creating the semisorted list
    third_list = arrayGenerator.randomizer(third_list)

    # sorting 100 semisorted list with mergesort
    start_time_semisorted = time.perf_counter()
    third_list = merge_sort.merge_sort(third_list)
    end_time_semisorted = time.perf_counter()
    print("Merge Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    semisorted_comparisons = merge_sort.count
    print("Merge Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Merge Sort data to the file
    write_file.write_to_file('Merge Sort', 'O(nlog(n))', 'O(nlog(n))', 100, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)
   
    # sorting 100 list with quicksort
    start_time = time.perf_counter()
    quickSort.quick_sort(fourth_list)
    end_time = time.perf_counter()
    print("Quick Sort time: %s seconds " % (end_time - start_time))
    comparisons = quickSort.q_count
    print("Quick Sort comparisions: ", quickSort.q_count)


    # Creating the semisorted list
    fourth_list = arrayGenerator.randomizer(fourth_list)

    # sorting 100 semisorted list with quicksort
    start_time_semisorted = time.perf_counter()
    quickSort.quick_sort(fourth_list)
    end_time_semisorted = time.perf_counter()
    print("Quick Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    semisorted_comparisons = quickSort.q_count
    print("Quick Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Quick Sort data to the file
    write_file.write_to_file('Quick Sort', 'O(n^2)', 'O(nlog(n))', 100, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)


    # 1000 list
    first_list, second_list, third_list, fourth_list = arrayGenerator.generate_random_list_number(1000)
    print("{}, {}, {}, {}".format(len(first_list), len(second_list), len(third_list), len(fourth_list)))
    
    # sorting 1000 list with bubblesort
    start_time = time.perf_counter()
    first_list, comparisons = bubbleSort.bubble_sort(first_list)
    end_time = time.perf_counter()
    print("Bubble Sort time: %s seconds" % (end_time - start_time))
    print("Bubble Sort comparisions: ", comparisons)

    # Creating the semisorted list
    first_list = arrayGenerator.randomizer(first_list)

    # sorting 1000 semisorted list with bubblesort
    start_time_semisorted = time.perf_counter()
    first_list, semisorted_comparisons = bubbleSort.bubble_sort(first_list)
    end_time_semisorted = time.perf_counter()
    print("Bubble Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    print("Bubble Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Bubble Sort data to the file
    write_file.write_to_file('Bubble Sort', 'O(n^2)', 'O(n)', 1000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)

    # sorting 1000 list with selectionsort
    start_time = time.perf_counter()
    second_list, comparisons = selectionSort.selection_sort(second_list)
    end_time = time.perf_counter()
    print("Selection Sort time: %s seconds" % (end_time - start_time))
    print("Selection Sort comparisions: ", comparisons)

    # Creating the semisorted list
    second_list = arrayGenerator.randomizer(second_list)

    # sorting 1000 semisorted list with selectionsort
    start_time_semisorted = time.perf_counter()
    second_list, semisorted_comparisons = selectionSort.selection_sort(first_list)
    end_time_semisorted = time.perf_counter()
    print("Selection Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    print("Selection Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Selection Sort data to the file
    write_file.write_to_file('Selection Sort', 'O(n^2)', 'O(n^2)', 1000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)
    
    # sorting 1000 list with mergesort
    start_time = time.perf_counter()
    third_list = merge_sort.merge_sort(third_list)
    end_time = time.perf_counter()
    print("Merge Sort time: %s seconds" % (end_time - start_time))
    print("Merge Sort comparisions: ", merge_sort.count)

    # Creating the semisorted list
    third_list = arrayGenerator.randomizer(third_list)

    # sorting 1000 semisorted list with mergesort
    start_time_semisorted = time.perf_counter()
    third_list = merge_sort.merge_sort(third_list)
    end_time_semisorted = time.perf_counter()
    print("Merge Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    semisorted_comparisons = merge_sort.count
    print("Merge Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Merge Sort data to the file
    write_file.write_to_file('Merge Sort', 'O(nlog(n))', 'O(nlog(n))', 1000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)

    # sorting 1000 list with quicksort
    start_time = time.perf_counter()
    quickSort.quick_sort(fourth_list)
    end_time = time.perf_counter()
    print("Quick Sort time: %s seconds " % (end_time - start_time))
    comparisons = quickSort.q_count
    print("Quick Sort comparisions: ", quickSort.q_count)

    # Creating the semisorted list
    fourth_list = arrayGenerator.randomizer(fourth_list)

    # sorting 1000 semisorted list with quicksort
    start_time_semisorted = time.perf_counter()
    quickSort.quick_sort(fourth_list)
    end_time_semisorted = time.perf_counter()
    print("Quick Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    semisorted_comparisons = quickSort.q_count
    print("Quick Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Quick Sort data to the file
    write_file.write_to_file('Quick Sort', 'O(n^2)', 'O(nlog(n))', 1000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)

    # 10000 list
    first_list, second_list, third_list, fourth_list = arrayGenerator.generate_random_list_number(10000)
    print("{}, {}, {}, {}".format(len(first_list), len(second_list), len(third_list), len(fourth_list)))

    # sorting 10000 list with bubblesort
    start_time = time.perf_counter()
    first_list, comparisons = bubbleSort.bubble_sort(first_list)
    end_time = time.perf_counter()
    print("Bubble Sort time: %s seconds" % (end_time - start_time))
    print("Bubble Sort comparisions: ", comparisons)

    # Creating the semisorted list
    first_list = arrayGenerator.randomizer(first_list)

    # sorting 10000 semisorted list with bubblesort
    start_time_semisorted = time.perf_counter()
    first_list, semisorted_comparisons = bubbleSort.bubble_sort(first_list)
    end_time_semisorted = time.perf_counter()
    print("Bubble Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    print("Bubble Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Bubble Sort data to the file
    write_file.write_to_file('Bubble Sort', 'O(n^2)', 'O(n)', 10000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)

    # sorting 10000 list with selectionsort
    start_time = time.perf_counter()
    second_list, comparisons = selectionSort.selection_sort(second_list)
    end_time = time.perf_counter()
    print("Selection Sort time: %s seconds" % (end_time - start_time))
    print("Selection Sort comparisions: ", comparisons)

    # Creating the semisorted list
    second_list = arrayGenerator.randomizer(second_list)

    # sorting 10000 semisorted list with selectionsort
    start_time_semisorted = time.perf_counter()
    second_list, semisorted_comparisons = selectionSort.selection_sort(first_list)
    end_time_semisorted = time.perf_counter()
    print("Selection Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    print("Selection Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Selection Sort data to the file
    write_file.write_to_file('Selection Sort', 'O(n^2)', 'O(n^2)', 10000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)
    
    # sorting 10000 list with mergesort
    start_time = time.perf_counter()
    third_list = merge_sort.merge_sort(third_list)
    end_time = time.perf_counter()
    print("Merge Sort time: %s seconds" % (end_time - start_time))
    print("Merge Sort comparisions: ", merge_sort.count)

    # Creating the semisorted list
    third_list = arrayGenerator.randomizer(third_list)

    # sorting 10000 semisorted list with mergesort
    start_time_semisorted = time.perf_counter()
    third_list = merge_sort.merge_sort(third_list)
    end_time_semisorted = time.perf_counter()
    print("Merge Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    semisorted_comparisons = merge_sort.count
    print("Merge Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Merge Sort data to the file
    write_file.write_to_file('Merge Sort', 'O(nlog(n))', 'O(nlog(n))', 10000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)

    # sorting 10000 list with quicksort
    start_time = time.perf_counter()
    quickSort.quick_sort(fourth_list)
    end_time = time.perf_counter()
    print("Quick Sort time: %s seconds " % (end_time - start_time))
    comparisons = quickSort.q_count
    print("Quick Sort comparisions: ", quickSort.q_count)

    # Creating the semisorted list
    fourth_list = arrayGenerator.randomizer(fourth_list)

    # sorting 10000 semisorted list with quicksort
    start_time_semisorted = time.perf_counter()
    quickSort.quick_sort(fourth_list)
    end_time_semisorted = time.perf_counter()
    print("Quick Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    semisorted_comparisons = quickSort.q_count
    print("Quick Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Quick Sort data to the file
    write_file.write_to_file('Quick Sort', 'O(n^2)', 'O(nlog(n))', 10000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)

    write_file.close()
コード例 #16
0
        return bit_search(list, value, middle + 1, high)
    elif list[middle] > value:
        return bit_search(list, value, low, middle - 1)
    else:
        return middle


def bit_search_by_loop(list, value, low, high):
    if low > high:
        return -1

    while (True):
        middle = (low + high) / 2
        if (list[middle] < value):
            low = middle + 1
        elif (list[middle] > value):
            high = middle - 1
        else:
            return middle


if __name__ == '__main__':
    lis = [
        3, 987, 6, 22, 9, 333, 22, 44, 9999, 555, 999, 1234, 1235, 1255, 1288
    ]
    val = 1235
    sortList = quick_sort(lis)
    ret = bit_search_by_loop(sortList, val, 0, len(lis))
    print sortList
    print ret, lis[ret]