Dsa Khushi

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Aim:-

To write a program to sort an entered array with insertion sort or selection sort using switch case.

Insertion Sort:-

Insertion sort is a simple sorting algorithm that works by repeatedly inserting elements into their
correct position in a sorted list. The algorithm starts with the first element in the list and compares it
to the elements that come before it. If the element is smaller than the previous element, it is
swapped with the previous element. This process continues until the element is in its correct
position.

Insertion sort is a simple and easy-to-understand algorithm. It is also a stable sorting algorithm,
which means that it preserves the original order of elements with equal keys. However, insertion sort
is not very efficient for large lists. There are other sorting algorithms, such as merge sort and quick
sort, that are more efficient for large lists.

Time Complexity:-

The time complexity of insertion sort can be divided into three cases:

• Best case: The best case occurs when the list is already sorted. In this case, the algorithm
only needs to make one comparison per element, for a total of n comparisons. Therefore,
the time complexity in the best case is O(n).
• Average case: The average case occurs when the list is in random order. In this case, the
algorithm needs to make n-1 comparisons for the first element, n-2 comparisons for the
second element, and so on. Therefore, the time complexity in the average case is O(n^2).
• Worst case: The worst case occurs when the list is in reverse order. In this case, the
algorithm needs to make n-1 comparisons for the first element, n-2 comparisons for the
second element, and so on, for a total of n(n-1)/2 comparisons. Therefore, the time
complexity in the worst case is O(n^2).
Selection Sort:-

The selection is a straightforward process of sorting values. In this method, to sort the data in
ascending order, the 0th element is compared with all other elements. If the 0th element is found to
be greater than the compared element, the two values get interchanged. In this way after the first
iteration, the smallest element is placed at 0th position. The technique is repeated until the full array
gets sorted.

Selection sort is a simple and easy-to-understand algorithm. It is also an in-place sorting algorithm,
which means that it does not require additional memory. Therefore, selection sort can be a good
choice for sorting small lists or lists that are stored in a limited amount of memory.

Time Complexity:-

The time complexity of selection sort can be divided into three cases:

• Best Case: The best case for selection sort occurs when the list is already sorted. In this case,
the algorithm only needs to make one comparison per element, for a total of n comparisons.
Therefore, the time complexity in the best case is O(n).

• Average case : The average case for selection sort occurs when the list is in random order. In
this case, the algorithm needs to make n-1 comparisons for the first element, n-2
comparisons for the second element, and so on. Therefore, the time complexity in the
average case is O(n^2).

• Worst case: The worst case for selection sort occurs when the list is in reverse order. In this
case, the algorithm needs to make n-1 comparisons for the first element, n-2 comparisons
for the second element, and so on, for a total of n(n-1)/2 comparisons. Therefore, the time
complexity in the worst case is O(n^2).
Conclusion:

Insertion sort and selection sort are both simple sorting algorithms that are easy to understand and
implement. They are both in-place sorting algorithms, which means that they do not require
additional memory. However, they both have a time complexity of O(n^2) in the average and worst
cases. This means that they can be inefficient for large lists.

Insertion sort is a good choice for sorting small lists or lists that are already partially sorted. It is also
a stable sorting algorithm, which means that it preserves the original order of elements with equal
keys.

Selection sort is a good choice for sorting lists that are stored in a limited amount of memory. It is
also a simple and easy-to-understand algorithm. However, it is not as efficient as some other sorting
algorithms, such as merge sort and quick sort.

For large lists, merge sort and quick sort are more efficient sorting algorithms than insertion sort and
selection sort. However, insertion sort and selection sort can be a good choice for small lists or lists
that have specific requirements, such as being stable or in-place.
Code:-

#include <stdio.h>
#include <stdlib.h>
int main()
{
int exit = 0, size,exit1 = 0,choice1, choice2, i, j;
int nums[10];
int sorted[10];
int min, minIndex,temp;
while (exit == 0) {
printf("Choose an Option\n1. Selection Sort\n2. Insertion Sort\n3. Quit\n");
scanf("%d", &choice1);
switch (choice1) {
case 1:
exit1 = 0;
while (exit1 == 0) {
printf("Choose an Option\n1. Enter Array\n2. Display\n3. Sort Array\n4.
Quit\n");
scanf("%d", &choice2);
switch (choice2) {
case 1:
printf("Enter Size of Array: \n");
scanf("%d", &size);
printf("Enter Elements of array: ");
for (i = 0; i<size; i++) {
printf("%d. ", i+1);
scanf("%d", &nums[i]);
}
break;
case 2:
printf("\n[");
for (i = 0; i<size;i++) {
printf("%d ", nums[i]);
}
printf("]\n");
break;
case 3:
for (i = 0; i<size; i++) {
minIndex = i;
if (i<size-1) {
for (j = i+1; j<size; j++) {
if (nums[minIndex] > nums[j]) {
minIndex = j;
}
}
}
temp = nums[minIndex];
nums[minIndex] = nums[i];
nums[i] = temp;
}
printf("Sorted Array: \n[");
for (i = 0; i<size;i++) {
printf("%d ", nums[i]);
}
printf("]\n");
break;
default:
exit1 = 1;
break;
}
}
break;
case 2:
exit1 = 0;
while (exit1 == 0) {
printf("Choose an Option\n1. Enter Array\n2. Display\n3. Sort Array\n4.
Quit\n");
scanf("%d", &choice2);
switch (choice2) {
case 1:
printf("Enter Size of Array: \n");
scanf("%d", &size);
printf("Enter Elements of array: ");
for (i = 0; i<size; i++) {
printf("%d. ", i+1);
scanf("%d", &nums[i]);
}
break;
case 2:
printf("\n[");
for (i = 0; i<size;i++) {
printf("%d ", nums[i]);
}
printf("]\n");
break;
case 3:
for (i = 1; i< size; i++) {
for (j = i; j>=1; j--) {
if (nums[j] < nums[j-1]) {
temp = nums[j];
nums[j] = nums[j-1];
nums[j-1] = temp;
}
}
}
printf("\n[");
for (i = 0; i<size;i++) {
printf("%d ", nums[i]);
}
printf("]\n");
break;
default:
exit1 = 1;
break;
}
}
break;
default:
exit = 1;
break;
}
}
return 0;
}
Output:-

Choose an Option
1. Selection Sort
2. Insertion Sort
3. Quit
2
Choose an Option
1. Enter Array
2. Display
3. Sort Array
4. Quit
1
Enter Size of Array:
3
Enter Elements of array: 1. 54
2. 98
3. 12
Choose an Option
1. Enter Array
2. Display
3. Sort Array
4. Quit
2

[54 98 12 ]
Choose an Option
1. Enter Array
2. Display
3. Sort Array
4. Quit
3

[12 54 98 ]
Choose an Option
1. Enter Array
2. Display
3. Sort Array
4. Quit
4
Choose an Option
1. Selection Sort
2. Insertion Sort
3. Quit
1
Choose an Option
1. Enter Array
2. Display
3. Sort Array
4. Quit
1
Enter Size of Array:
3
Enter Elements of array: 1. 78
2. 95
3. 46
Choose an Option
1. Enter Array
2. Display
3. Sort Array
4. Quit
2

[78 95 46 ]
Choose an Option
1. Enter Array
2. Display
3. Sort Array
4. Quit
3
Sorted Array:
[46 78 95 ]
Choose an Option
1. Enter Array
2. Display
3. Sort Array
4. Quit
4
Choose an Option
1. Selection Sort
2. Insertion Sort
3. Quit
3

You might also like