1
+ /*
2
+
3
+ TOPIC - Space & Time Complexity - Quick Sort
4
+
5
+ -----------------------------------------------------------------------------------------------------
6
+ // Time Complexity
7
+ ___________
8
+ |_|_|_|_|_|X| size = n
9
+ | p (pivot)
10
+ _____|_____
11
+ |_|_|X|_|_|_|
12
+ p
13
+
14
+
15
+ If T(n) is the total time to sort the complete array.
16
+
17
+ Then this time include 3 parts:
18
+ divide divide partition function
19
+ T(n) = T(n-p) + T(p-1) + k*n
20
+
21
+ Now, assume p (i.e pivot) is always at MID,
22
+ So, p = n/2
23
+ T(n) = T(n -_n_) + T(_n_- 1) + k*n
24
+ 2 2
25
+ T(n) = T(n/2) + T(n/2) + k*n
26
+ T(n) = 2T(n/2) + k*n
27
+
28
+ Now, the recurrence is, T(n) = 2*T(n/2) + k*n
29
+ divide partition function
30
+
31
+ Lets solve the recurrence:-
32
+ T(n) = 2T(n/2) + k*n
33
+ 2T(n/2) = 4T(n/4) + 2k*n/2 (after multiplying and dividing both side by 2)
34
+ 4T(n/4) = 8T(n/8) + 4k*n/4 (after multiplying and dividing both side by 2)
35
+ ...
36
+ ...
37
+ T(1) = 0 + k*n (Note: when n=1, time taken to divide is 0)
38
+ ----------------------------- // By adding all the above equations on both sides & cancellation, we can get the total time
39
+ logn
40
+ T(n) = ∑ k*n
41
+ i=1
42
+ (NOTE: From coming from array size n to 1, we have taken logn steps)
43
+ = kn*logn
44
+ = nlogn
45
+ = O(nlogn)
46
+
47
+ Time Complexity = O(nlogn) (For Average Case)
48
+
49
+ -----------------------------------------------------------------------------------------------------
50
+ _________
51
+ Worst Case => |1|2|3|4|5|
52
+ p
53
+
54
+ divide divide partition function
55
+ T(n) = T(n-p) + T(p-1) + k*n
56
+
57
+ putting p = n-1
58
+ T(n) = T(n-n-1) + T(n-1-1) + k*n
59
+ = T(1) + T(n-2) + k*n
60
+ = T(n-1) + k*n
61
+
62
+ Now, the recurrence is, T(n) = T(n-1) + k*n
63
+
64
+ Lets solve the recurrence:-
65
+ T(n) = T(n-1) + k*n
66
+ T(n-1) = T(n-2) + k*(n-1)
67
+ ...
68
+ ...
69
+ T(1) = 0 + k
70
+ ------------------------- (after adding & cancellation)
71
+ T(n) = k + 2k + 3k +...+ (n-1)k + nk)
72
+ = k*(1+2+3+...n)
73
+ = O(n^2)
74
+
75
+ Time Complexity = O(nlogn) (For Worst Case)
76
+ (NOTE: To overcome this worst case using "Randomized Quick Sort"
77
+ In which we shuffle the entire array before sorting)
78
+
79
+ -----------------------------------------------------------------------------------------------------
80
+ // Space Complexity
81
+
82
+ We know quick sort is a Inplace Sorting Algorithm (i.e it doesn't use extra array like merge sort)
83
+
84
+ Space Complexity = Recursive Call stack
85
+ = (At any give time, we can have atmost logN function calls in call stack)
86
+ = O(logn)
87
+
88
+ So, Space Complexity = O(logn)
89
+
90
+ -----------------------------------------------------------------------------------------------------
91
+ Thus,
92
+ Time Complexity = O(nlogn) (For average case)
93
+ = O(n^2) (For Worst Case) (Solution: Use Randomized Quick Sort)
94
+ Space Complexity = O(logn)
95
+
96
+
97
+ -----------------------------------------------------------------------------------------------------
98
+ */
0 commit comments