Insertion Sort
#include <stdio.h>
int main() {
int n, i, j, temp;
int arr[100]; // Declare an array to hold up to 100 elements
// Get the number of elements from the user
printf("Enter number of elements:\\n");
scanf("%d", &n);
// Get the elements of the array from the user
printf("Enter %d integers:\\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 1; i < n; i++) {
temp = arr[i]; // Store the current element to be inserted
j = i - 1; // Start comparing with the element before the current one
while (j >= 0 && arr[j] > temp) {
arr[ j + 1] = arr[j];
j = j - 1;
}
arr[ j + 1] = temp;
}
printf("Sorted list in ascending order:\\n");
for (i = 0; i < n; i++) {
printf("%d\\n", arr[i]);
}
return 0;
}
Bubble Sort
#include <stdio.h>
int main() {
int array[100], n, i, j, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
swap = array[j];
array[j] = array[j + 1];
array[j + 1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++) {
printf("%d\n", array[i]);
}
return 0;
}
Selection Sort
#include <stdio.h>
int main() {
int arr[100], num, min_idx, i, j, temp;
printf("Please Enter the total number of elements: ");
scanf("%d", &num);
printf("Please Enter the Array Elements: ");
for (i = 0; i < num; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < num - 1; i++) {
min_idx = i;
for (j = i + 1; j < num; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
if (min_idx != i) {
temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
}
printf("\nSorted Array in Ascending Order: ");
for (i = 0; i < num; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
1. Bubble Sort, Selection Sort, and Insertion Sort all have the same worst-case and
average-case time complexity of O(n²). However, Insertion Sort generally performs
better in practice, especially on nearly sorted data, due to fewer swaps and
comparisons, making it more efficient in average scenarios compared to Bubble Sort
and Selection Sort.
2. Insertion Sort has the best-case time complexity of O(n) when the input array is
already sorted, which is not possible for Bubble Sort and Selection Sort.
3. Bubble Sort and Insertion Sort are stable sorting algorithms, meaning that they
preserve the relative order of equal elements in the sorted array, while Selection Sort
is not stable.
4. In terms of performance, Insertion Sort tends to perform better than Bubble Sort and
Selection Sort for small datasets, while Bubble Sort and Selection Sort may perform
better than Insertion Sort for larger datasets or datasets that are partially sorted.
Overall, each algorithm has its own advantages and disadvantages, and
the choice of which algorithm to use depends on the specific requirements
of the problem at hand.
Enum:
An enum (enumeration) in C is a user-defined data type that consists of a set of named
integral constants. Its primary purpose is to assign meaningful names to integer values,
which makes the code more readable, maintainable, and self-explanatory.
Syntax
The basic syntax for defining an enum is:
enum enum_name {
constant1,
constant2,
...
constantN
};
Key Characteristics
Integer Constants: Although you use names (identifiers), they are internally
represented as integer constants.
Default Values: By default, the compiler assigns integer values starting from 0 to the
first constant, and increments by 1 for each subsequent constant.
Custom Values: You can explicitly assign custom integer values to any enumerator.
Unassigned constants after a custom value will increment from the previous value.
// 1. Define the enum type
enum Level {
LOW,
MEDIUM,
HIGH
};
// 2. Declare a variable of type 'enum Level'
enum Level myVar;
// 3. Assign an enum constant to the variable
myVar = MEDIUM;