-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathQuickSort.rb
71 lines (63 loc) · 1.54 KB
/
QuickSort.rb
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Quick Sort
#Time-complexity::
# Worst case (array already sorted/reverse sorted or too many duplicates): O(n^2),
# Average case : O(nlogn), Best case(when partition always results middle element as pivot):O(nlogn)
#Auxiliary Space: O(1), not Stable
#preferred for sorting arrays over merge sort
#Algorithm
# quicksort(A, lo, hi) is
# if lo < hi then
# p := partition(A, lo, hi)
# quicksort(A, lo, p)
# quicksort(A, p + 1, hi)
# partition(A, lo, hi) is
# pivot := A[lo]
# i := lo – 1
# j := hi + 1
#loop forever
# do
# i := i + 1
# while A[i] < pivot
#do
# j := j – 1
#while A[j] > pivot
#if i >= j then
# return j
#swap A[i] with A[j]
def quick_sort(a,lo,hi)
if lo<hi
p=partition(a,lo,hi)
quick_sort(a,lo,p-1)
quick_sort(a,p+1,hi)
end
return a
end
def partition(a,lo,hi)
i=lo
j=hi+1
pivot= a[lo]
while true
#Loop to increment i
begin
i+=1
break if i==hi
end while a[i]<pivot
#Loop to increment j
begin
j-=1
break if j==lo
end while a[j]>pivot
# break the loop if pointers cross
break if i>=j
#Swap arr[i] and arr[j]
temp=a[i]
a[i]=a[j]
a[j]=temp
end
# Swap arr[lo] with arr[j]
temp=a[lo]
a[lo]=a[j]
a[j]=temp
return j
end
quick_sort([12,3,1,2,4,70,89,3,3],0,8)