@@ -9,51 +9,50 @@ package main
9
9
10
10
import "fmt"
11
11
12
-
13
- func Swap (a * int , b * int ){
12
+ func Swap (a * int , b * int ) {
14
13
t := * a
15
14
* a = * b
16
15
* b = t
17
16
}
18
17
19
18
func Partition (arr []int , start , end int ) int {
20
-
19
+
21
20
pivot := arr [end ]
22
21
23
22
//Index of smaller element
24
23
var i int = (start - 1 )
25
24
26
- for j := start ; j <= end - 1 ; j ++ {
25
+ for j := start ; j <= end - 1 ; j ++ {
27
26
//If current element is smaller than or equal to pivot
28
- if ( arr [j ] <= pivot ) {
27
+ if arr [j ] <= pivot {
29
28
i ++
30
29
Swap (& arr [i ], & arr [j ])
31
30
}
32
31
}
33
32
34
- Swap (& arr [i + 1 ], & arr [end ])
33
+ Swap (& arr [i + 1 ], & arr [end ])
35
34
36
35
return (i + 1 )
37
36
}
38
37
39
38
/*The main function that implements QuickSort
40
- arr[] -> Array to be sorted
41
- start -> Starting index
42
- end -> Ending index
39
+ arr[] -> Array to be sorted
40
+ start -> Starting index
41
+ end -> Ending index
43
42
*/
44
43
func QuickSort (arr []int , start , end int ) {
45
- if ( start < end ) {
44
+ if start < end {
46
45
//pi is partitioning index, arr[p] is now at right place
47
46
var pi int = Partition (arr , start , end )
48
47
49
48
//Separately sort elements before partition and after partition
50
- QuickSort (arr , start , pi - 1 )
51
- QuickSort (arr , pi + 1 , end )
49
+ QuickSort (arr , start , pi - 1 )
50
+ QuickSort (arr , pi + 1 , end )
52
51
}
53
52
}
54
53
55
- func PrintArray (arr []int , size int ){
56
- for i := 0 ; i < size ; i ++ {
54
+ func PrintArray (arr []int , size int ) {
55
+ for i := 0 ; i < size ; i ++ {
57
56
fmt .Printf ("%d " , arr [i ])
58
57
}
59
58
fmt .Printf ("\n " )
@@ -63,8 +62,8 @@ func main() {
63
62
arr := []int {10 , 7 , 8 , 9 , 1 , 5 }
64
63
var n int = len (arr )
65
64
66
- QuickSort (arr , 0 , n - 1 )
65
+ QuickSort (arr , 0 , n - 1 )
67
66
68
67
fmt .Println ("Sorted array is: " )
69
68
PrintArray (arr , n )
70
- }
69
+ }
0 commit comments