Blame view

quicksort.go 787 Bytes
84543a02b   Marko Tikvić   quicksort
1
  package webutility
28a6cc60c   Marko Tikvić   Integrated logger...
2
  import "sort"
84543a02b   Marko Tikvić   quicksort
3

28a6cc60c   Marko Tikvić   Integrated logger...
4
5
  // QuickSort quicksorts que.
  func QuickSort(que sort.Interface, low, high int, reverse bool) {
84543a02b   Marko Tikvić   quicksort
6
7
8
  	if low >= high {
  		return
  	}
28a6cc60c   Marko Tikvić   Integrated logger...
9
10
11
  	index := quickSortPartition(que, low, high, reverse)
  	QuickSort(que, low, index-1, reverse)
  	QuickSort(que, index+1, high, reverse)
84543a02b   Marko Tikvić   quicksort
12
  }
28a6cc60c   Marko Tikvić   Integrated logger...
13
  func quickSortPartition(que sort.Interface, low, high int, reverse bool) int {
84543a02b   Marko Tikvić   quicksort
14
15
  	i := low - 1
  	for j := low; j <= high-1; j++ {
28a6cc60c   Marko Tikvić   Integrated logger...
16
17
18
19
20
21
22
23
24
25
  		if !reverse {
  			if que.Less(j, high) {
  				i++
  				que.Swap(i, j)
  			}
  		} else {
  			if !que.Less(j, high) {
  				i++
  				que.Swap(i, j)
  			}
84543a02b   Marko Tikvić   quicksort
26
27
28
29
30
  		}
  	}
  	que.Swap(i+1, high)
  	return i + 1
  }
62a0d7a8a   Marko Tikvić   minor changes
31
32
33
34
35
36
37
38
39
40
  
  func BubbleSort(arr []int64) {
  	for i := 0; i < len(arr)-1; i++ {
  		for j := i; j < len(arr); j++ {
  			if arr[i] > arr[j] {
  				arr[i], arr[j] = arr[j], arr[i]
  			}
  		}
  	}
  }