LAB_2
LAB_2
LAB_2
Code:
import time
import matplotlib.pyplot as plt
import numpy as np
start_time = time.perf_counter()
quick_sort(arr, 0, len(arr) - 1)
end_time = time.perf_counter()
data.append((n, execution_time))
print(f"{n}\t {execution_time:.6f}")
# Generate plot
n_values, time_values = zip(*data)
plt.figure(figsize=(7, 5))
plt.show()
import time
import matplotlib.pyplot as plt
import numpy as np
import random
start_time = time.perf_counter()
quick_sort(arr, 0, len(arr) - 1)
end_time = time.perf_counter()
data.append((n, execution_time))
print(f"{n}\t {execution_time:.6f}")
# Generate plot
n_values, time_values = zip(*data)
plt.figure(figsize=(7, 5))
plt.show()
The best case occurs when we select the pivot as the mean.
Quick Sort ()
{
P =partition();-----n
n
QuickSort(left);-----------
2
n
QuickSort(right);--------
2
}
{
1,∧n=1
T (n)=
2T
n
2()
+ n ,∧n>1
Here
2T ( n2 ) signifies that the a problemof ¿ is divided into 2 problems
n
of ¿( ¿ )each .n: the number of
2
comparisons required to identify the exact position of itself (every element)
T ( n )=2 T ()
n
2
+n n>1−−(1)T ( n )=2 2 T
n
[ ( ) ]
n
+ + n−−(2)
2∗2 2
Substitue eq ( 2 ) ∈eq(1)
Worst Case:
The worst case will occur when the array gets divided into two parts, one part
consisting of N-1 elements and the other and so on.
Quick Sort ()
{
P =partition();-----n
QuickSort(left);----------- (n-1)
QuickSort(right);----0
}
T ( n )=T ( n−1 ) +n T (n-1) is time taken by remaining element except for pivot element.
n: the number of comparisons required to identify the exact position of itself (every
element)