The document discusses various sorting algorithms including bubble sort and insertion sort. Bubble sort compares adjacent elements and swaps them if they are out of order, making multiple passes through the list. Insertion sort inserts elements into the sorted portion of the list one by one. Both algorithms have quadratic time complexity in the worst case but insertion sort performs better on partially sorted lists.
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
Chapter2.1 .pptx
1. Agenda
• Sorting
• Definition & terminology
• Bubble sort
• Algorithm of bubble sort
• Function to implement bubble sort
• Advantages & disadvantages of bubble sort
• Efficiency of bubble sort
• Insertion sort
2. Sorting
• It is the process of arranging elements in
ascending or descending order of the key
values.
• Sorting techniques:
1. Bubble sort
2. Insertion sort
3. Selection sort
4. Quick sort
5. Merge sort
3. Bubble sort
• Bubble sort is a simple sorting algorithm. This
sorting algorithm is comparison-based
algorithm in which each pair of adjacent
elements is compared and the elements are
swapped if they are not in order.
• This algorithm is not suitable for large data
sets as its average and worst case complexity
are of Ο(n2) where n is the number of items.
6. Algorithm for bubble sort
1. Start
2. Pass=1
3. i=0
4. if x[i]>x[i+1]
5. i=i+1
6. If i<=n-1-pass
goto step 4
7. Pass = Pass+1
8. if Pass < n
goto step 3
9. Stop.
7. Function to implement bubble sort
// A function to implement bubble sort
void bubbleSort (int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
8. Function to print an array
• /* Function to print an array */
• void printArray(int arr[], int size)
• {
• int i;
• for (i=0; i < size; i++)
• printf("%d ", arr[i]);
• printf("n");
• }
9. Advantages of bubble sort
• It is simple sorting method.
• No addition data structure is required.
• It is in-place sorting method.
• It is stable sorting method.
10. Disadvantages of bubble sort
• It is very inefficiency method. O(n2)
• Even of the element are in sorted order,
a[n-1] pass will be done.
12. Insertion sort
• Insertion sort works similarly as we sort cards in a
card game.
• We assume that the first card is already sorted then,
we select an unsorted card. If the unsorted card is
greater than the card in hand, it is placed on the
right otherwise, to the left. In the same way, other
unsorted cards are taken and put at their right place.
• Insertion sort is a sorting algorithm that places an
unsorted element at its suitable place in each
iteration.
13.
14.
15. Algorithm for Insertion sort
1. Start
2. j=1 i.e unsorted position starts from 1
3. Key=x[j]
4. i=j-1 i.e. sorted position starts from1
5. While x[i]>key and i>=0
x[i+1]=key i.e. insert key in correct position
6. j=j+1 next unsorted element
7. If j<n
goto step 3
6. Stop.
17. .
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are greater than key, to one position
ahead of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
18. Advantages and disadvantages of Insertion sort
Advantages :
• It is simple sorting method.
• No addition data structure is required.
• It is in-place sorting method.
• It is stable sorting method.
• Best case time complexity is o(n)
• Most suitable sorting method is if the element are already
sorted.
Disadvantages:
• worst case time complexity is O(n2)