4

I am writing an algorithm to find the lowest number in an array however my print statement keeps saying that the lowest number is 0. I have the following:

var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]

func findMin(numbers: NSArray) {

    var minValue = numbers[0]
    var isSmallest: Bool

    for i in 0...numbers.count {
        isSmallest = true
        for j in 0...numbers.count {
            if i > j {
                isSmallest = false
            }
        }

        if isSmallest {
            minValue = i
        }

    }
    print("Smallest value in the list is \(minValue)")
}

findMin(numbers: list as NSArray)

My print statement returns as:

"Smallest value in the list is 0\n"

I feel like the algorithm is correct. Any ideas?

EDIT: Answered my own question

I was iterating over indices and not actual values. Thanks to one of the users in the comments. The correct code should be:

var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]

func findMin(numbers: NSArray) {

    var minValue = numbers[0]
    var isSmallest: Bool

    for i in list {
        isSmallest = true
        for j in list {
            if i > j {
                isSmallest = false
            }
         }

        if isSmallest {
            minValue = i
        }

    }

    print("Smallest value in the list is \(minValue)")
}

findMin(numbers: list as NSArray)
butter_baby
  • 858
  • 2
  • 10
  • 24

9 Answers9

16

Simply

let list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
let minValue = list.min()
vadian
  • 274,689
  • 30
  • 353
  • 361
2

For logic use try this

    var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
    var minValue = list[0]
    for num in list {
        minValue = (num  < minValue) ? num : minValue
    }
    print("Smallest value in the list is \(minValue)")

For direct get min value by property

let list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
let minValue = list.min()
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
  • 1
    `minValue = min(num, minValue)` would probably be acceptable to an instructor :) You should also deal with an empty list :) – David Berry Mar 25 '17 at 06:01
1

Or you could just use

var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
list.min() // returns 2
1

If you'd like to find the min value without an extra loop, try this:

var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]

func findMin(numbers: NSArray) {

    var minValIdx = 0
    var minValue = numbers[0] as! Int

    for i in 1..<numbers.count {
        if (numbers[i] as! Int) < minValue {
            minValue = numbers[i] as! Int
            minValIdx = i
        }
    }

    print("Smallest value in the list is \(minValue)")
}

findMin(numbers: list as NSArray)
Mark Mucha
  • 1,560
  • 2
  • 12
  • 18
1

You can use this code in Swift for manual algorithm:

let list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
    var smallest = list[0]
    for item in list {
        if (item < smallest) {
            smallest = item
        }
    }
    print("smallest number is: \(smallest)")

And if you want Swift to do the hard work then use this:

let smallest = list.min()
print("smallest number is: \(smallest)")
0

here it is your solution

let numbers = [1, 6, 3, 9, 4, 6]

let min = minElement(numbers) // 1

let position = find(array, min)// it will return index  
Arjun Bhoot
  • 302
  • 2
  • 12
0

Just to throw a few more options out there, assuming you have to actually show some logic:

func min<T:Comparable>(_ elements:[T]) -> T? {
    guard let first = elements[0] else {
        return nil
    }
    return elements.reduce(first, min)
}

print(min(list))

or put it in an extension, this is essentially the definition of Array.min

extension Array where Element : Comparable {
    func smallest() -> Element? {
        guard let first = self.first else {
            return nil
        }

        // Use 'Swift.min' to get to the global function since Array
        // already has a min function
        return reduce(first, Swift.min)
    }
}

print(list.smallest())
David Berry
  • 40,941
  • 12
  • 84
  • 95
0

You can use this code: it is in C#

 var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
            int minVal = list[0];

        for (int i = 1; i < list.Length; i++)
        {
            if (list[i] < minVal)
            {
                minVal = intArray[i];
            }
        }
anis programmer
  • 989
  • 1
  • 10
  • 25
0

To find the minimum element in the sequence, Swift 3 have an istance method called min():

var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
let minElem = list.min()

If the sequence has no elements, returns nil.

This method can be used also for a list of floats:

let heights = [67.5, 65.7, 64.3, 61.1, 58.5, 60.3, 64.9]
let minHeight = heights.min()

You can find the official document referece here

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133