二分法 選擇排序 快速排序

2021-09-23 18:52:20 字數 3188 閱讀 3186

最近看了《演算法**》,覺得很棒,做一些總結記錄。

# -*- coding: utf-8 -*-

import random

__author__ = "chenk"

class algorithme:

""""""

def __init__(self):

self.low = 0

self.high = 10000

def get_random_num(self):

"""return a random num. the num is between 0 and 10000."""

return random.randint(self.low, self.high)

def get_list(self, num):

"""returns an array of the number of executions."""

num_list = list()

while num:

num -= 1

return num_list

def dichotomy(self):

"""二分法猜數字: a給出乙個整數num(0num:

high = mid

elif mid < num:

low = mid

else:

print("you got it. it takes %d times." % count)

break

mid = (low + high) // 2

def get_max_num(self, array):

"""from the array get the max num."""

max_num = 0

for num in array:

if num > max_num:

max_num = num

return max_num

def selection_sort(self):

"""選擇排序(從大到小):每次從陣列中取最大的值,迴圈 n/2 次。

num_list = self.get_list(20)

sorted_list = list()

print("選擇排序目標陣列:\n", num_list)

for i in range(len(num_list)):

max_num = self.get_max_num(num_list)

num_list.remove(max_num)

return sorted_list

def quicksort(self, array):

"""快速排序(從大到小).

if len(array) < 2:

return array

else:

pivot = array[0]

left = [i for i in array[1:] if i >= pivot]

right = [i for i in array[1:] if i < pivot]

return self.quicksort(left) + [pivot] + self.quicksort(right)

if __name__ == "__main__":

algorithme = algorithme()

for i in range(10):

algorithme.dichotomy()

sorted_list = algorithme.selection_sort()

print("排序後的值:\n", sorted_list)

quicksort_list = algorithme.get_list(10)

print("快速排序目標陣列:\n", quicksort_list)

quicksort = algorithme.quicksort(quicksort_list)

print("排序後的值:\n", quicksort)

上述執行**的結果為:

the random num is 3432.

you got it. it takes 11 times.

the random num is 9185.

you got it. it takes 12 times.

the random num is 8764.

you got it. it takes 11 times.

the random num is 6344.

you got it. it takes 12 times.

the random num is 667.

you got it. it takes 13 times.

the random num is 5982.

you got it. it takes 12 times.

the random num is 7408.

you got it. it takes 12 times.

the random num is 450.

you got it. it takes 12 times.

the random num is 2013.

you got it. it takes 12 times.

the random num is 5428.

you got it. it takes 14 times.

選擇排序目標陣列:

[42, 607, 2544, 3854, 4845, 9129, 1208, 1286, 7588, 9414, 6669, 3927, 8941, 6775, 9866, 2526, 9410, 9463, 1048, 2010]

排序後的值:

[9866, 9463, 9414, 9410, 9129, 8941, 7588, 6775, 6669, 4845, 3927, 3854, 2544, 2526, 2010, 1286, 1208, 1048, 607, 42]

快速排序目標陣列:

[4563, 6788, 628, 4224, 4286, 4563, 4503, 9094, 5353, 8843]

排序後的值:

[9094, 8843, 6788, 5353, 4563, 4563, 4503, 4286, 4224, 628]

二分法快速排序

我在實現二分法快速排序的時候,最初的程式是這樣的。include using namespace std void qsort int arr,int left,int right while l r arr l value l if arr l value while l r arr l valu...

二分法 氣泡排序 選擇排序

二分法是一種效率比較高的搜尋方法 假設有乙個1 100之間的數字,你來猜這個數是多少,每猜一次可以得到三種 回答 正確 大了或小了。如何保證用最少的次數猜對?很多人會想到先猜50,如果猜大了,說明答案比50小,然後猜25.用這種方法,每次都可以將數字的範圍縮小一半,對於1 100之間的任何數,最多都...

C 快速(二分法)排序

快速 二分法 排序的思想是將陣列劃分為兩邊,以某個節點v為界 設這個節點的值為 v 在節點v左邊的所有元素都小於 v,在節點v右邊的所有元素都大於v.這樣不停地劃分,到最後整個陣列就是有序的了。具體分成兩邊的思路為 起始下標為start,結束下標為 end 選擇v a end 作為中介點 先從sta...