Вы находитесь на странице: 1из 168

Module IV

QUICK SORT
What is Quick Sort ??

Ranking manner Quick Sort is the most widely used of all the
other sorting algorithms because they are faster and consume
less memory.
It is superior to (bubble sort) and (insertion sort) and (selection
sort) speed and distinct from (merge sort) speed and available
memory in spite of the latter outweigh the three counterparts,
but he also quickly consumes more memory.
What is Quick Sort ??

Division here is to divide the list into two smaller and larger
where there is a so-called (pivot) and it is determined
smaller or larger is not required to be a number of
elements in both sections of equal or arranged in
ascending or descending order, but what is important in
the partitioning phase is to make the smallest of the axis
division in the department and the largest in the last
section.
What is Quick Sort ??

This algorithm is the order of the array at a rate of ( n*log n),


where n is the step number of the elements of the array ..
Meaning:
If we have a array of the element 128, this will Khurazem
Petrtaha in step 896 ..
And this number is very appropriate when compared to
other ranking algorithms
For example, in Bubble sort algorithm will need to step 16384
in any way ..
What is Quick Sort ??

numbers less p numbers greater than or


than p equal to p
Q S
uick ort

41 62 13 84 35 96 57 28 79
Q S
uick ort

Pivot = 41

41 62 13 84 35 96 57 28 79
Q S uick ort

41 62 13 84 35 96 57 28 79

Pivot = 41
Q S uick ort

41 62 13 84 35 96 57 28 79

i j

Pivot = 41
Q S uick ort

41>79 ??
41 62 13 84 35 96 57 28 79

i j

Pivot = 41
Q S uick ort

41>79 ??
41 62 13 84 35 96 57 28 79

i
Fals j

Pivot = 41
Q S uick ort

41 62 13 84 35 96 57 28 79

j--
i j

Pivot = 41
Q S uick ort

41>28 ??
41 62 13 84 35 96 57 28 79

i j

Pivot = 41
Q S uick ort

41>28 ??
41 62 13 84 35 96 57 28 79

i j

Pivot = 41
True
Q S uick ort

41 62 13 84 35 96 57 28 79

i j

Pivot = 41
Q S uick ort

Pivot = j
28 62 13 84 35 96 57 41 79

i j

Pivot = 41
Q S uick ort

i++
28 62 13 84 35 96 57 41 79

i j

Pivot = 41
Q S uick ort

62>41 ??
28 62 13 84 35 96 57 41 79

i j

Pivot = 41
Q S uick ort

62>41 ??
28 62 13 84 35 96 57 41 79

True
i j

Pivot = 41
Q S uick ort

28 62 13 84 35 96 57 41 79

i j

Pivot = 41
Q S uick ort

Pivot = i

28 41 13 84 35 96 57 62 79

i j

Pivot = 41
Q S uick ort

28 41 13 84 35 96 57 62 79

i
j-- j

Pivot = 41
Q S uick ort

41>57 ??
28 41 13 84 35 96 57 62 79

i j

Pivot = 41
Q S uick ort

41>57 ??
28 41 13 84 35 96 57 62 79

i Fals j

Pivot = 41
Q S uick ort

28 41 13 84 35 96 57 62 79

i
j-- j

Pivot = 41
Q S uick ort

28 41 13 84 35 96 57 62 79

i j

Pivot = 41
Q S uick ort

41>96 ??
28 41 13 84 35 96 57 62 79

i j

Pivot = 41
Q S uick ort

41>96 ??
28 41 13 84 35 96 57 62 79

i
Fals j

Pivot = 41
Q S uick ort

28 41 13 84 35 96 57 62 79

j--
i j

Pivot = 41
Q S uick ort

28 41 13 84 35 96 57 62 79

i j

Pivot = 41
Q S uick ort

41>35 ??
28 41 13 84 35 96 57 62 79

i j

Pivot = 41
Q S uick ort

41>35 ??
28 41 13 84 35 96 57 62 79

i j

Pivot = 41 True
Q S uick ort

28 41 13 84 35 96 57 62 79

i j

Pivot = 41
Q S uick ort

Pivot = j
28 35 13 84 41 96 57 62 79

i j

Pivot = 41
Q S uick ort

i++
28 35 13 84 41 96 57 62 79

i j

Pivot = 41
Q S uick ort

13>41 ??
28 35 13 84 41 96 57 62 79

i j

Pivot = 41
Q S uick ort

13>41 ??
28 35 13 84 41 96 57 62 79

i
Fals j

Pivot = 41
Q S uick ort

i++
28 35 13 84 41 96 57 62 79

i j

Pivot = 41
Q S uick ort

84>41 ??
28 35 13 84 41 96 57 62 79

i j

Pivot = 41
Q S uick ort

28 35 13 84 41 96 57 62 79

i j

Pivot = 41
Q S uick ort

28 35 13 41 84 96 57 62 79

i j

Pivot = 41
Q S uick ort

28 35 13 41 84 96 57 62 79

ij

Pivot = 41 j++
Q S uick ort

i=j
28 35 13 41 84 96 57 62 79

ij

Pivot = 41
Q S uick ort

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

28 35 13 41 84 96 57 62 79

Pivot = 41
Q S uick ort

a[0] a[1] a[2]

28 35 13 41 84 96 57 62 79

Pivot = 41
Q S uick ort

a[0] a[1] a[2] a[0] a[1] a[2] a[3] a[4]

28 35 13 41 84 96 57 62 79

Pivot = 41
Q S
uick ort

a[0] a[1] a[2] a[0] a[0] a[1] a[2] a[3] a[4]

28 35 13 41 84 96 57 62 79

numbers less than p numbers greater than or equal to p


Q S
uick ort

Now we sort numbers less than p


28 35 13

41 84 96 57 62 79
Q S
uick ort

Pivot = 28

28 35 13

41 84 96 57 62 79
Q S
uick ort

Pivot = 28

28 35 13

41 84 96 57 62 79
Q S uick ort

28 35 13

i j

Pivot = 28 41 84 96 57 62 79
Q S uick ort

28>13 ??
28 35 13

i j

Pivot = 28 41 84 96 57 62 79
Q S uick ort

28>13 ??
28 35 13

i j

True
Pivot = 28 41 84 96 57 62 79
Q S uick ort

28 35 13

i j

Pivot = 28 41 84 96 57 62 79
Q S uick ort

Pivot = j

13 35 28

i j

Pivot = 28 41 84 96 57 62 79
Q S uick ort

i++
13 35 28

i j

Pivot = 28 41 84 96 57 62 79
Q S uick ort

35>28 ??
13 35 28

i j

True
Pivot = 28 41 84 96 57 62 79
Q S uick ort

13 35 28

i j

Pivot = 28 41 84 96 57 62 79
Q S uick ort

13 28 35

i j

Pivot = 28 41 84 96 57 62 79
Q S
uick ort

a[0] a[0] a[0]

13 28 35

41 84 96 57 62 79
Q S uick ort

Now we sort numbers greater than or equal


to p
84 96 57 62 79

13 28 35 41
Q S uick ort

a[0] a[1] a[2] a[3] a[4]

84 96 57 62 79

13 28 35 41
Q S uick ort

Pivot = 84

84 96 57 62 79

13 28 35 41
Q S uick ort

i=84 j=79

84 96 57 62 79

i j

13 28 35 41
Q S uick ort

84>79 ??
84 96 57 62 79

i j

13 28 35 41
Q S uick ort

84>79 ??
84 96 57 62 79

i j

True
13 28 35 41
Q S uick ort

84 96 57 62 79

i j

13 28 35 41
Q S uick ort

79 96 57 62 84

i j

13 28 35 41
Q S uick ort

79 96 57 62 84

i j

13 28 35 41
Q S uick ort

i=96

79 96 57 62 84

i j

13 28 35 41
Q S uick ort

96>84 ??
79 96 57 62 84

i j

13 28 35 41
Q S uick ort

96>84 ??
79 96 57 62 84

i j

13 28 35 41
True
Q S uick ort

79 96 57 62 84

i j

13 28 35 41
Q S uick ort

79 84 57 62 96

i j

13 28 35 41
Q S uick ort

j--
79 84 57 62 96

i j

13 28 35 41
Q S uick ort

84>62 ??
79 84 57 62 96

i j

13 28 35 41
Q S uick ort

84>62 ??
79 84 57 62 96

i j

13 28 35 41
True
Q S uick ort

79 84 57 62 96

i j

13 28 35 41
Q S uick ort

79 62 57 84 96

i j

13 28 35 41
Q S uick ort

i++
79 62 57 84 96

i j

13 28 35 41
Q S uick ort

57>84 ??
79 62 57 84 96

i j

13 28 35 41
Q S uick ort

57>84 ??
79 62 57 84 96

Fals
i j

13 28 35 41
e
Q S uick ort

79 62 57 84 96

i j

13 28 35 41
Q S uick ort

i++
79 62 57 84 96

i j

13 28 35 41
Q S uick ort

i=j
79 62 57 84 96

i j

13 28 35 41
Q S uick ort

a[0] a[1] a[2] a[0] a[0]

79 62 57 84 96

i j

13 28 35 41
Q S uick ort

a[0] a[1] a[2] a[0] a[0]

79 62 57 84 96

numbers greater
numbers less than p than or equal to
p

13 28 35 41
Q S uick ort

79 62 57 96

13 28 35 41 84
Q S uick ort

Now we sort numbers greater than or equal to p

96

13 28 35 41 79 62 57 84
Q S uick ort

i=j=96
96

i j

13 28 35 41 79 62 57 84
Q S uick ort

Now we sort numbers less than p

79 62 57

13 28 35 41 84 96
Q S uick ort

Pivot = 79
79 62 57

i j

13 28 35 41 84 96
Q S uick ort

79>57 ??
79 62 57

i j

13 28 35 41 84 96
Q S uick ort

79>57 ??
79 62 57

i j

13 28 35 41
True 84 96
Q S uick ort

79 62 57

i j

13 28 35 41 84 96
Q S uick ort

57 62 79

i j

13 28 35 41 84 96
Q S uick ort

i++
57 62 79

i j

13 28 35 41 84 96
Q S uick ort

62>79 ??
57 62 79

i j

13 28 35 41 84 96
Q S uick ort

62>79 ??
57 62 79

Fals i j

13 28 35 41
e 84 96
Q S uick ort

i++
57 62 79

ij

13 28 35 41 84 96
Q S uick ort

i=j
57 62 79

ij

13 28 35 41 84 96
Q S uick ort

a[0] a[1] a[0]

57 62 79

13 28 35 41 84 96
Q S uick ort

a[0] a[1] a[0]

57 62 79

numbers less than p

13 28 35 41 84 96
Q S uick ort

57 62

13 28 35 41 79 84 96
Q S uick ort

Now we sort numbers less than p

57 62

13 28 35 41 79 84 96
Q S uick ort

Pivot = 84
i=84 j=79

57 62

i j

13 28 35 41 79 84 96
Q S uick ort

57>62 ??
57 62

i j

False
13 28 35 41 79 84 96
Q S uick ort

j--
57 62

ij

13 28 35 41 79 84 96
Q S uick ort

i=j
57 62

ij

13 28 35 41 79 84 96
Q S uick ort

57 62

numbers greater
than or equal to p

13 28 35 41 79 84 96
Q S uick ort

Now we sort numbers greater than or equal to p

62

13 28 35 41 57 79 84 96
Q S uick ort

Pivot = 79
i=79 j=79

62

i j

13 28 35 41 57 79 84 96
Q S uick ort

i=j
62

i j

13 28 35 41 57 79 84 96
Q S uick ort

62

13 28 35 41 57 79 84 96
Q S
uick ort

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

13 28 35 41 57 62 79 84 96
Algorithm

Quicksort(A, p, r)
{
if (p < r) {
q = Partition(A, p, r)
Quicksort(A, p , q-1)
Quicksort(A, q+1 , r)
}
}
 PARTITION (A, p, r)
x ← A[p] // pivot
i ← p-1
j ← r+1
while TRUE do
Repeat j ← j-1
until A[j] ≤ x ; Then swap ( A[j], x);
Repeat i ← i+1
until A[i] ≥ x; Then swap(A[i],x);
if i < j
then continue;
else
return j
Analysis - Complexity of Quicksort

T(N) = T(i) + T(N - i -1) + cN


The time to sort the file is equal to
the time to sort the left partition with i elements, plus
the time to sort the right partition with N-i-1 elements, plus
the time to build the partitions
Worst-case Analysis

 This happens when the pivot is the smallest (or the largest) element.
 Then one of the partitions is empty, and we repeat recursively the procedure for
N-1 elements
 The pivot is the smallest element
 T(N) = T(N-1) + cN, N > 1
 Then
 T(N-1) = T(N-2) + c(N-1)
 T(N-2) = T(N-3) + c(N-2)
 T(N-3) = T(N-4) + c(N-3)
 T(2) = T(1) + c.2
Add all equations:
T(N) + T(N-1) + T(N-2) + … + T(2) =
= T(N-1) + T(N-2) + … + T(2) + T(1) + c(N) + c(N-1) + c(N-2) + …
+ c.2
T(N) = T(1) + c(2 + 3 + … + N)
T(N) = 1 + c(N(N+1)/2 -1)

Therefore T(N) = O(N2)


Best-case analysis:

 The best case is when the pivot is the median of the array,
and then the left and the right part will have same size.
 There are logN partitions, and to obtain each partitions we do N comparisons
 The pivot is in the middle,
 Then
 T(N) = 2T(N/2) + cN
Divide by N:
T(N) / N = T(N/2) / (N/2) + c
Then:
T(N/2) / (N/2) = T(N/4) / (N/4) + c
T(N/4) / (N/4) = T(N/8) / (N/8) + c
……
T(2) / 2 = T(1) / (1) + c
Add all equations:
T(N) / N + T(N/2) / (N/2) + T(N/4) / (N/4) + …. + T(2) / 2 =
= (N/2) / (N/2) + T(N/4) / (N/4) + … + T(1) / (1) +
c.logN
After crossing the equal terms:
T(N)/N = T(1) + cLogN = 1 + cLogN
T(N) = N + NcLogN

Therefore T(N) = O(NlogN)


Merge Sort
In the Beginning…

Invented by
John von Neumann (1903-
1957)
Follows divide and
conquer paradigm.
Developed merge sort for
EDVAC in 1945
The key to Merge Sort is merging two sorted lists into
one, such that if you have two lists
L1 = { 3 8 9 } L2 = { 1 5 7 }
the resulting list is
merge(L1, L2) = { 1 3 5 7 8 9 }
Divide And Conquer

1.Divide: Divide the unsorted list into two sub lists of about
half the size.
2.Conquer: Sort each of the two sub lists recursively until we
have list sizes of length 1,in which case the list itself is
returned.
3.Combine: Merge the two-sorted sub lists back into one
sorted list.
a problem of size n

subproblem 1 subproblem 2
of size n/2 of size n/2

a solution to a solution to
subproblem 1 subproblem 2

a solution to
the original problem
MERGE-SORT

STEPS:
on a problem of size 1 do nothing
on a problem of size at least 2
Split the sequence into two halves
Sort one half of the numbers
Sort the second half of the numbers
Merge the two sorted lists
MERGE
•Keep track of smallest element in each sorted half.
•Insert smallest of two elements into auxiliary array.
•Repeat until done.

smallest smallest

A G L O R H I M S T

auxiliary array
MERGE

smallest smallest

A G L O R H I M S T

A G

auxiliary array
MERGE

smallest smallest

A G L O R H I M S T

A G H

auxiliary array
MERGE

smallest smallest

A G L O R H I M S T

A G H I

auxiliary array
MERGE

smallest smallest

A G L O R H I M S T

A G H I L

auxiliary array
MERGE

smallest smallest

A G L O R H I M S T

A G H I L M

auxiliary array
MERGE

smallest smallest

A G L O R H I M S T

A G H I L M O

auxiliary array
MERGE

smallest smallest

A G L O R H I M S T

A G H I L M O R

auxiliary array
MERGE

first half
exhausted smallest

A G L O R H I M S T

A G H I L M O R S

auxiliary array
MERGE

first half
exhausted smallest

A G L O R H I M S T

A G H I L M O R S T

auxiliary array
MERGE

first half second half


exhausted exhausted

A G L O R H I M S T

A G H I L M O R S T

auxiliary array
MERGE ( A, 5,8,11 )

4 5 6 7 8 9 10 11 12

... 1 2 7 9 3 5 8 ...

A k

1 2 7 9  3 5 8 

L i R j
4 5 6 7 8 9 10 11 12

... 1 2 7 9 3 5 8 ...

A k

1 2 7 9  3 5 8 

L i R j
4 5 6 7 8 9 10 11 12

... 1 2 3 9 3 5 8 ...

A k

1 2 7 9  3 5 8 

L i R j
4 5 6 7 8 9 10 11 12

... 1 2 3 5 3 5 8 ...

A k

1 2 7 9  3 5 8 

L i R j
4 5 6 7 8 9 10 11 12

... 1 2 3 5 7 5 8 ...

A k

1 2 7 9  3 5 8 

L i R j
4 5 6 7 8 9 10 11 12

... 1 2 3 5 7 8 8 ...

A k

1 2 7 9  3 5 8 

L i R j
4 5 6 7 8 9 10 11 12

... 1 2 3 5 7 8 8 ...

A k

1 2 7 9  3 5 8 

L i R j
4 5 6 7 8 9 10 11 12

... 1 2 3 5 7 8 9 ...

A k

1 2 7 9  3 5 8 

L i R j
Example 2:

99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

4 0
99 6 86 15 58 35 86 0 4

Merge 4 0
6 99 15 86 35 58 0 4 86

99 6 86 15 58 35 86 0 4

Merge
Merge Sort Example

6 15 86 99 0 4 35 58 86

6 99 15 86 58 35 0 4 86

Merge
0 4 6 15 35 58 86 86 99

6 15 86 99 0 4 35 58 86

Merge
Pros:

It is a stable sort, and there is no worst-case scenario.


It is faster, the temp array holds the resulting array until both
left and right sides are merged into the temp array, then the
temp array is appended over the input array.
It is used in tape drives to sort data - its good with parallel
processing.
Cons:

The memory requirement is doubled.


Takes longer to merge because if the next element is
in the right side then all of the elements must be
moved down.
Algorithm
 MergeSort(arr[], l, r)
 If r > l
 1. Find the middle point to divide the array into two halves: middle
m = (l+r)/2
 2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
 3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
 4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
merge(int arr[], int l, int m, int r) {
n1 = m - l + 1; n2 = r – m;
L[n1], R[n2]; /* create temp arrays */
/* Copy data to temp arrays L[] and R[] */

for(i = 0; i < n1; i++)


L[i] = arr[l + i];
for(j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; j = 0; k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j])
arr[k] = L[i]; i++;
else
arr[k] = R[j]; j++;
k++;
}
/* Copy the remaining elements of L[], if there are any */

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there are any */

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}
complexity of merge sort

 Time to merge sort N elements = time to merge sort N/2


elements plus
time to merge two arrays each N/2 elements
 T(1) = 1
 T(N) = 2T(N/2) + N
Divide (2) by N:
 T(N) / N = T(N/2) / (N/2) + 1
T(N/2) / (N/2) = T(N/4) / (N/4) +1
T(N/4) / (N/4) = T(N/8) / (N/8) +1
T(N/8) / (N/8) = T(N/16) / (N/16) +1
……
T(2) / 2 = T(1) / 1 + 1
add equations
T(N) / N + T(N/2) / (N/2) + T(N/4) / (N/4) + … + T(2)/2 =
T(N/2) / (N/2) + T(N/4) / (N/4) + ….+ T(2) / 2 + T(1) / 1 + LogN
(LogN is the sum of 1s in the right-hand sides)
After crossing the equal term, we get
 T(N)/N = T(1)/1 + LogN
T(1) is 1, hence we obtain
T(N) = N + NlogN = O(NlogN)
Hence the complexity of the MergeSort algorithm is O(NlogN)

Вам также может понравиться