0

Trying to print out each iteration of Bubblesort to the label text to show on the screen rather than console.

I tried to assign the label text each time the array is updated but it only shows the last version of the array which is the sorted one

On screen print:

on screen print

What I want to print n the label:

what i want to print on the label

class ViewController: UIViewController {

    @IBOutlet weak var Label2: UILabel!

    public func bubbleSort<T> (_ arrays: [T], _ comparison: (T,T) -> Bool) -> [T]  {
        var array = arrays

        for i in 0..<array.count {
            for j in 1..<array.count-i {
                if comparison(array[j], array[j-1]) {
                    let tmp = array[j-1]
                    array[j-1] = array[j]
                    array[j] = tmp
                    print(array) // prints the array after each iteration to the console
                // inserting Label2.text = "\(array)" doesnt work as I intend it to. 
                }
            }
        }


        return array
    }

    public func bubbleSort<T> (_ elements: [T]) -> [T] where T: Comparable {
        return bubbleSort(elements, <)
    }

}
vacawama
  • 150,663
  • 30
  • 266
  • 294
ahmeto
  • 33
  • 4
  • That's not a single label anymore. Probably best to use a UITableView, containing multiple labels. – Alexander Aug 10 '19 at 02:03
  • Were you wanting to show all steps on the screen at the same time (similar to the console output), or animate the label showing it change until it is sorted? – vacawama Aug 10 '19 at 02:14
  • @vacawama , yes just print the console output to the label or textview. No need for animation at this stage. – ahmeto Aug 10 '19 at 14:33
  • I like Alexander's suggestion. Use a tableView to display the results. Create a `[String]` called `steps` to hold the data for the table. At each step, append `steps.append("\(array)")`. Use that as the dataSource for your tableView displaying `cell.label.text = steps[indexPath.row]`. – vacawama Aug 10 '19 at 14:42
  • 1
    If you want to add multiple lines to a label in a loop then look at [this answer](https://stackoverflow.com/a/57134482/9223839) – Joakim Danielson Aug 10 '19 at 15:27

1 Answers1

0

This would be a good place to use a tableView to display the data.

  1. Add a UITableView to your ViewController. Add a Basic style prototype cell with "cell" as the reuse identifier.
  2. Attach the tableView in the Storyboard to @IBOutlet var tableView: UITableView!.
  3. Set up the tableView.delegate and tableView.dataSource in viewDidLoad().
  4. Add a property var steps = [String] to your ViewController.
  5. At each step of the bubbleSort, append the array to the steps: steps.append("\(array)").
  6. In numberOfRowsInSection(), return steps.count.
  7. In cellForRowAt(), set cell.textLabel?.text = steps[indexPath.row].
  8. Use a didSet property observer for steps to call tableView.reloadData().

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var tableView: UITableView!

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return steps.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = steps[indexPath.row]

        return cell
    }

    var steps = [String]() {
        didSet {
            tableView.reloadData()
        }
    }

    public func bubbleSort<T> (_ arrays: [T], _ comparison: @escaping (T,T) -> Bool) -> [T]  {
        var array = arrays

        for i in 0..<array.count {
            for j in 1..<array.count-i {
                if comparison(array[j], array[j-1]) {
                    let tmp = array[j-1]
                    array[j-1] = array[j]
                    array[j] = tmp
                    print(array) // prints the array after each iteration to the console
                    steps.append("\(array)")
                }
            }
        }

        return array
    }

    public func bubbleSort<T> (_ elements: [T]) -> [T] where T: Comparable {
        return bubbleSort(elements, <)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self
    }

    @IBAction func go(button: UIButton) {
        steps = []
        _ = bubbleSort([33, 45, 25, 356, 5, 90, 14])
    }
}
vacawama
  • 150,663
  • 30
  • 266
  • 294