6 Sorting Algorithms
6 Sorting Algorithms
Time Complexity:
Worst-case time complexity: 𝑂( 𝑛2) — when the list is in reverse order.
Best-case time complexity: 𝑂( 𝑛) — when the list is already sorted
Average-case time complexity: 𝑂( 𝑛2).
Bubble Sort Cont’d
Let the elements of array are – void bubbleSort (int arr[], int n){
int i, j;
15 12 26 4 18 bool swapped;
First Pass (i=0) for (i = 0; i < n - 1; i++) {
swapped = false;
15(j) 12(j+1) 26 4 18 for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
12 15(j) 26(j+1) 4 18 swap(&arr[j], &arr[j + 1]);
swapped = true;
12 15 26(j) 4(j+1) 18 }
}
12 15 4 26(j) 18(j+1) if (swapped == false)
break;
12 15 4 18 26 }
}
Bubble Sort Cont’d
void bubbleSort (int arr[], int n){
12 4 15 18 26 int i, j;
bool swapped;
Second Pass (i=1) for (i = 0; i < n - 1; i++) {
swapped = false;
12(j) 15(j+1) 4 18 26 for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
12 15(j) 4(j+1) 18 26 swap(&arr[j], &arr[j + 1]);
swapped = true;
12 4 15(j) 18(j+1) 26 }
}
12 4 15 18 26 if (swapped == false)
break;
}
}
Bubble Sort Cont’d
void bubbleSort (int arr[], int n){
12 4 15 18 26 int i, j;
bool swapped;
Third Pass (i=2) for (i = 0; i < n - 1; i++) {
swapped = false;
12(j) 4(j+1) 15 18 26 for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
4 12(j) 15(j+1) 18 26 swap(&arr[j], &arr[j + 1]);
swapped = true;
}
4 12 15 18 26 }
if (swapped == false)
break;
}
}
Bubble Sort Cont’d
void bubbleSort (int arr[], int n){
4 12 15 18 26 int i, j;
bool swapped;
Fourth Pass (i=3) for (i = 0; i < n - 1; i++) {
swapped = false;
4(j) 12(j+1) 15 18 26 for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
4 12 15 18 26 swap(&arr[j], &arr[j + 1]);
swapped = true;
}
}
if (swapped == false)
break;
}
}
Insertion Sort Algorithm
Insertion sort is a simple sorting algorithm that works by iteratively inserting each element
of an unsorted list into its correct position in a sorted portion of the list.
Algorithm:
Start from the second element of the list.
Compare the current element with the previous elements of the sorted section.
Move all the elements that are greater than the current element one position to the right to make space
for the current element.
Insert the current element into the correct position in the sorted section.
Repeat this process until all elements are processed.
Time Complexity:
Worst-case time complexity: 𝑂( 𝑛2) — when the list is in reverse order.
Best-case time complexity: 𝑂( 𝑛) — when the list is already sorted
Average-case time complexity: 𝑂( 𝑛2).
Insertion Sort Cont’d
Let the elements of array are – void insertionSort (int arr[], int n)
{
15 12 26 4 18 for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
Key = 12
while (j >= 0 && arr[j] > key) {
15(j) 12 26 4 18 arr[j + 1] = arr[j];
j = j - 1;
15 15 26 4 18 }
arr[j + 1] = key;
12 15 26 4 18 }
}
Insertion Sort Cont’d
Key = 26 void insertionSort (int arr[], int n)
{
12 15(j) 26 4 18 for (int i = 1; i < n; ++i) {
int key = arr[i];
12 15 26 4 18 int j = i - 1;
Home Task
Link_1
Link_2
Thank You