Pythonå®ç°çå¿«éæåºç®æ³è¯¦è§£
æ¬æå®ä¾è®²è¿°äºPythonå®ç°çå¿«éæåºç®æ³ãå享ç»å¤§å®¶ä¾å¤§å®¶åèï¼å
·ä½å¦ä¸ï¼
å¿«éæåºåºæ¬ææ³æ¯ï¼éè¿ä¸è¶æåºå°è¦æåºçæ°æ®åå²æç¬ç«ç两é¨åï¼å
¶ä¸ä¸é¨åçæææ°æ®é½æ¯å¦å¤ä¸é¨åçæææ°æ®é½è¦å°ï¼ç¶ååææ¤æ¹æ³å¯¹è¿ä¸¤é¨åæ°æ®åå«è¿è¡å¿«éæåºï¼æ´ä¸ªæåºè¿ç¨å¯ä»¥éå½è¿è¡ï¼ä»¥æ¤è¾¾å°æ´ä¸ªæ°æ®åææåºåºåã
å¦åºå[6ï¼8ï¼1ï¼4ï¼3ï¼9]ï¼éæ©6ä½ä¸ºåºåæ°ãä»å³åå·¦æ«æï¼å¯»æ¾æ¯åºåæ°å°çæ°å为3ï¼äº¤æ¢6å3çä½ç½®ï¼[3ï¼8ï¼1ï¼4ï¼6ï¼9]ï¼æ¥çä»å·¦åå³æ«æï¼å¯»æ¾æ¯åºåæ°å¤§çæ°å为8ï¼äº¤æ¢6å8çä½ç½®ï¼[3ï¼6ï¼1ï¼4ï¼8ï¼9]ãéå¤ä¸è¿°è¿ç¨ï¼ç´å°åºåæ°å·¦è¾¹çæ°åé½æ¯å
¶å°ï¼å³è¾¹çæ°åé½æ¯å
¶å¤§ãç¶ååå«å¯¹åºåæ°å·¦è¾¹åå³è¾¹çåºåéå½è¿è¡ä¸è¿°æ¹æ³ã
å®ç°ä»£ç å¦ä¸ï¼
def parttion(v, left, right):
key = v[left]
low = left
high = right
while low < high:
while (low < high) and (v[high] >= key):
high -= 1
v[low] = v[high]
while (low < high) and (v[low] <= key):
low += 1
v[high] = v[low]
v[low] = key
return low
def quicksort(v, left, right):
if left < right:
p = parttion(v, left, right)
quicksort(v, left, p-1)
quicksort(v, p+1, right)
return v
s = [6, 8, 1, 4, 3, 9, 5, 4, 11, 2, 2, 15, 6]
print("before sort:",s)
s1 = quicksort(s, left = 0, right = len(s) - 1)
print("after sort:",s1)
è¿è¡ç»æï¼
before sort: [6, 8, 1, 4, 3, 9, 5, 4, 11, 2, 2, 15, 6]
after sort: [1, 2, 2, 3, 4, 4, 5, 6, 6, 8, 9, 11, 15]
温馨提示:答案为网友推荐,仅供参考