Skip to content

Instantly share code, notes, and snippets.

@suragch
Created September 22, 2021 04:54
Show Gist options
  • Save suragch/2862d0c3e3e73ad5d1ff184299ff425c to your computer and use it in GitHub Desktop.
Save suragch/2862d0c3e3e73ad5d1ff184299ff425c to your computer and use it in GitHub Desktop.
Homework from Sept 20
/// Homework
///
/// Give me an example of:
///
/// 1. Constant time - O(1)
/// 2. Linear time - O(n)
/// 3. Quadratic time - O(n2)
void main(List<String> arguments) {
final startTime = DateTime.now();
// constantTime(15);
// linearTime(23);
quadraticTime(5);
final endTime = DateTime.now();
print(endTime.difference(startTime));
}
// 1. Constant time
void constantTime(int input) {
print(3 * input);
}
// 2. Linear time
void linearTime(int number) {
var total = 0;
for (var i = 1; i <= number; i++) {
total = total + 1;
}
print(total);
}
// 3. Quadratic time
void quadraticTime(int number) {
var total = 0;
for (var i = 0; i <= number; i++) {
for (var j = 0; j < number; j++) {
total = total + 1;
}
}
print(total)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment