I'm writing a small algorithm and I have to calculate all combinations of items in an array without repetitions. Until now I have used this code below but I now need to speed up this process because it takes too long. I have tried to implement concurrency with Swift (the code will run on a Mac) but unfortunately it doesn't work.
The algorithm I'm using was taken from http://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/ and then converted from C to Swift. Could you please help me out to solve this problem?
func printCombination(arr: [Int], r: Int) {
trips.removeAll()
var data: [Int] = []
for _ in 1...r
{
data.append(Int())
}
combinationUtil(arr: arr, r: r, index: 0, data: data, i: 0)
}
func combinationUtil(arr: [Int], r: Int, index: Int, data: [Int], i: Int) {
var data: [Int] = data
if (index == r)
{
for j in 0..<r {
array.append(data[j])
}
return
}
if (i >= arr.count) {
return
}
data[index] = arr[i]
combinationUtil(arr: arr, r: r, index: index + 1, data: data, i: i + 1)
combinationUtil(arr: arr, r: r, index: index, data: data, i: i + 1)
}
/* arr[] ---> Input Array
r ---> Size of a combination to be printed
index ---> Current index in data[]
data[] ---> Temporary array to store current combination
i ---> index of current element in arr[] */