python快速排序_Python快速排序

python快速排序

Here you get python quick sort program and algorithm.

在這裏,您將獲得python快速排序程序和算法。

Quick sort is based on divide and Conquer technique. We divide our array into sub-arrays and that sub-arrays divided into another sub-arrays and so on, until we get smaller arrays. Because it is easy to solve small arrays in compare to a large array.

快速分類是基於分而治之的技術。 我們將數組劃分爲子數組,然後將該子數組劃分爲另一個子數組,依此類推,直到得到更小的數組。 因爲與大數組相比,解決小數組很容易。

Sorting smaller arrays will sort the entire array.

對較小的數組進行排序將對整個數組進行排序。

Python快速排序 (Python Quick Sort)

To understand Quick Sort let’s  take an example:-

爲了理解快速排序,讓我們舉個例子:

(Example)

We have an array [48,44,19,59,72,80,42,65,82,8,95,68]

我們有一個數組[48,44,19,59,72,80,42,65,82,8,95,68]

First of all we take first element and place it at its proper place. We call this element Pivot element.

首先,我們將第一個元素放在適當的位置。 我們稱此元素爲Pivot元素。

Note: We can take any element as Pivot element but for convenience the first element is taken as Pivot.

注意:我們可以將任何元素都用作Pivot元素,但爲方便起見,將第一個元素視爲Pivot。

There are two condition to place Pivot at its proper place.

Pivot放置在適當位置有兩個條件。

  • All the elements to the left of Pivot element should be smaller than

    Pivot元素左側的所有元素都應小於

  • All the elements to the right of Pivot element should be greater than

    Pivot元素右側的所有元素都應大於

In given array, we’ll take first element as Pivot element which is 48. Now place it at its proper place according to first two conditions.

在給定的數組中,我們將第一個元素作爲Pivot元素,即48。現在根據前兩個條件將其放置在適當的位置。

Python Quick Sort 1

Here all the elements to the left is smaller and to right are greater than Pivot.

在此,左側的所有元素都比Pivot小,而右側的元素都大於Pivot。

If you confused that how we placed Pivot at its proper place then wait we’ll discuss it later in partition algorithm.

如果您對如何將Pivot放置在適當位置感到困惑,那麼請稍後再討論分區算法。

Well, now we’ll take two sub-lists/sub-arrays left and right of our Pivot element (48).  Sub-lists will be

好了,現在我們將在Pivot元素的左側和右側獲得兩個子列表/子數組(48)。 子列表將是

[42,44,19,8] and [80,72,65,82,59,95,68]

[42,44,19,8]和[80,72,65,82,59,95,68]

and will also take first element as Pivot element in both and place the their Pivot elements at their proper places using partition algorithm. After this step each of the sub-lists will be divided into two parts then new sub-lists will be divided into another two parts and so on until we reached the end.

並且還將第一個元素都用作Pivot元素,並使用分區算法將其Pivot元素放置在適當的位置 在此步驟之後,每個子列表將被分爲兩部分,然後新的子列表將被分爲另外兩個部分,依此類推,直到到達結尾爲止。

Let’s see how it will look like.

讓我們看看它的外觀。

Python Quick Sort 2

Pivot element represented by Green color.

樞軸元素以綠色表示。

We can write a recursive function for this procedure. There would be two terminating conditions. When the sub-list has only 1 element or when no sub-list is formed. If the value of low is equal to up then there will be only one element in the sub-list and if the value of low exceeds up then no sub-list is formed. So our algorithm will be.

我們可以爲此過程編寫一個遞歸函數。 將有兩個終止條件。 當子列表只有1個元素時或沒有子列表形成時。 如果low的值等於up,則子列表中將只有一個元素;如果low的值超過up,則不會形成子列表。 因此我們的算法將是。

算法 (Algorithm)

Quick[array, low,up]

快速[數組,低,上]

Where low and up are lower and upper bound of the array.

其中至多是下限和上限的陣列構成。

STEP 1:    if low>=up then return

步驟1:如果從低到高,則返回

STEP 2:    set piv_loc = call partition(array,low,up)

步驟2 :設置piv_loc =調用分區(array,low,up)

                   [calling partition to find the location of pivot]

                   [調用分區以查找樞軸的位置]

STEP 3:    call Quick(array,low,piv_loc-1)

步驟3:調用Quick(array,low,piv_loc-1)

                        [Calling Quick for left sub-list]

                        [爲左側子列表快速調用]

STEP 4:    call Quick(array,piv_loc+1, up)

步驟4:呼叫Quick(array,piv_loc + 1,向上)

                        [Calling Quick for right sub-list]

                        [快速調用右側子列表]

Partition [array, low, up]

分區[數組,低,上]

This algorithm is to find the location of pivot element and return it.

該算法是找到樞軸元素的位置並將其返回。

STEP 1:  set i = low+1

步驟1:將i設爲低+1

Set j = up

設置j =向上

Set pivot = array[low]

設置數據透視=數組[低]

STEP 2:  while(i <= j)

步驟2: while(i <= j)

{

{

While(( array[i] < pivot ) and ( i < up ))

While(((array [i] <樞軸)和(i <up))

Increase i by 1

使我增加1

While ( array[j] > pivot )

而(數組[j]>樞軸)

Decrease j by 1

將j減1

If ( i < j )

如果(i <j)

{

{

Swap the value of array[i] with array[j]

用array [j]交換array [i]的值

Increase i by 1

使我增加1

Decrease j by 1

將j減1

}

}

Else

其他

Increase i by 1

使我增加1

}

}

STEP 3:    set array[low] = array[j]

步驟3:     設置array [low] = array [j]

Set array[j] = pivot

設置數組[j] =樞軸

Return j

返回j

時間複雜度 (Time Complexity)

Best Case: O (n log2n)

最佳情況: O(n log 2 n)

Average Case: O (n log n)

平均情況: O(n log n)

Worst Case: O (n2)

最壞的情況: O(n 2 )

Python快速排序程序 (Program for Quick Sort in Python)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Array = [48,44,19,59,72,80,42,65,82,8,95,68]
low = 0
up = len(Array) - 1
def partition(Array,low,up):
i = low+1
j = up
pivot = Array[low]
while(i<=j):
while(Array[i]<pivot and i<up):
i = i+1
while(Array[j]>pivot):
j = j-1
if(i<j):
Array[i],Array[j] = Array[j],Array[i]
i = i+1
j = j-1
else:
i = i+1
Array[low] = Array[j]
Array[j] = pivot
return j
def quick(Array,low,up):
if(low>=up):
return
piv_loc = partition(Array,low,up)
quick(Array,low,piv_loc-1)
quick(Array,piv_loc+1,up)
quick(Array,low,up)
for i in Array:
print i,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Array = [ 48 , 44 , 19 , 59 , 72 , 80 , 42 , 65 , 82 , 8 , 95 , 68 ]
low = 0
up = len ( Array ) - 1
def partition ( Array , low , up ) :
i = low + 1
j = up
pivot = Array [ low ]
while ( i <= j ) :
while ( Array [ i ] < pivot and i < up ) :
i = i + 1
while ( Array [ j ] > pivot ) :
j = j - 1
if ( i < j ) :
Array [ i ] , Array [ j ] = Array [ j ] , Array [ i ]
i = i + 1
j = j - 1
else :
i = i + 1
Array [ low ] = Array [ j ]
Array [ j ] = pivot
return j
def quick ( Array , low , up ) :
if ( low >= up ) :
return
piv_loc = partition ( Array , low , up )
quick ( Array , low , piv_loc - 1 )
quick ( Array , piv_loc + 1 , up )
quick ( Array , low , up )
for i in Array :
print i ,

Output

輸出量

8 19 42 44 48 59 65 68 72 80 82 95

8 19 42 44 48 59 65 68 72 80 82 95

Comment below if you have doubts related to above python quicksort tutorial.

如果您對以上python quicksort教程有疑問,請在下面評論。

翻譯自: https://www.thecrazyprogrammer.com/2017/12/python-quick-sort.html

python快速排序