~~~ 𝑻𝒉𝒆 π‘Ίπ’†π’“π’Šπ’†π’” 𝒐𝒇 π‘©π’Šπ’ˆ 𝑢 ~~~𝑷𝒂𝒓𝒕 – 𝑰


int getElement(int arr[], int index) {
return arr[index];
}
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
typedef struct Node {
int data;
struct Node *next;
} Node;

void insertAtHead(Node **head, int value) {
Node *newNode = malloc(sizeof(Node));
newNode->data = value;
newNode->next = *head;
*head = newNode;
}

In this example, the loop will always run 10 times, no matter what. The time it takes is constant, hence O(1).

void fixedLoop() {
for (int i = 0; i < 10; i++) {
printf("This is iteration %d\n", i);
}
}

void nestedFixedLoops() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
printf("This is iteration (%d, %d)\n", i, j);
}
}
}

void loopWithEarlyExit(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("This is iteration %d\n", i);

if (arr[i] == 0) {
break;
}
}
}

Leave a Reply