algorithm implementations in swift