Professional Documents
Culture Documents
aRani1
aRani1
Present by.
Selection sort Bubble Sort is the simplest Sorting Algorithm that works
Types of Sorting
by repeatedly swapping the adjacent elements if they
are in the wrong order. This algorithm is not suitable for
large data sets as its average and worst-case time
Bubble sort complexity is quite high.
Stape 1 Stape 2
Stape 3 Stape 4
Final Output
SELECTION SORT
Complexity Analysis :
Time Complexity:
The time complexity of Selection Sort is O(N2) as there are two nested loops,
• One loop to select an element of Array one by one = O(N)
• Another loop to compare that element with every other Array element = O(N)
• Therefore overall complexity = O(N) * O(N) = O(N*N) = O(N2)
Auxiliary Space:
O(1) as the only extra memory used is for temporary variables while
swapping two values in Array. The selection sort never makes more than
O(N) swaps and can be useful when memory writing is costly.
Bubble Sort Implementation by C
Implementation :
Stape 1 Stape 2
Stape 3
Bubble SORT
Complexity Analysis :
Time Complexity:
2
O(N )
For Modified Bubble Sort
Auxiliary Space:
O(1)
Comparison between Bubble sort & Modified Bubble sort
• Bubble sort is a simple sorting • Modified Bubble Sort is an optimized version of the basic
algorithm that repeatedly steps bubble sort algorithm.
through the list to be sorted. • It includes a flag that tracks whether any swaps were
• It compares adjacent elements and made in a pass through the list.
swaps them if they are in the • If no swaps are made in a pass, it terminates early, as the
wrong order. list is already sorted.
• It has a time complexity of O(n^2) • This modification reduces the time complexity in the
in the worst and average cases, best-case scenario to O(n) when the list is already sorted.
making it inefficient for large • However, it still has a worst-case time complexity of
datasets. O(n^2) in the same way as the basic bubble sort.
• It is stable and easy to implement
but not suitable for large or already
partially sorted lists.
Merge sort
Implementations:
Merge sort #include <stdio.h>
void merge(int arr[], int l, int m, int r) {
while (j < n2) {
arr[k] = R[j];
Implementations in c : int i, j, k; j++;
k++;
int n1 = m - l + 1; }
int n2 = r - m; }
int L[n1], R[n2]; void mergeSort(int arr[], int l, int r) {
for (i = 0; i < n1; i++) if (l < r) {
L[i] = arr[l + i]; int m = l + (r - l) / 2;
mergeSort(arr, l, m);
for (j = 0; j < n2; j++)
mergeSort(arr, m + 1, r);
R[j] = arr[m + 1 + j];
i = 0; merge(arr, l, m, r);
j = 0; }
k = l; }
while (i < n1 && j < n2) { int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
if (L[i] <= R[j]) {
int arr_size = sizeof(arr) / sizeof(arr[0]);
arr[k] = L[i];
i++; printf("Original Array: ");
} else { for (int i = 0; i < arr_size; i++) {
arr[k] = R[j]; printf("%d ", arr[i]);
j++; }
mergeSort(arr, 0, arr_size - 1);
}
k++; printf("\nSorted Array: ");
} for (int i = 0; i < arr_size; i++) {
while (i < n1) { printf("%d ", arr[i]);
arr[k] = L[i]; }
i++; return 0;
}
k++;
}
Merge Sort
Complexity Analysis :
Time Complexity: O(N log(N)), Merge Sort is a recursive algorithm and time complexity can be expressed
as following recurrence relation. T(n) = 2T(n/2) + θ(n)
The above recurrence can be solved either using the Recurrence Tree method or the
Master method. It falls in case II of the Master Method and the solution of the
recurrence is θ(Nlog(N)). The time complexity of Merge Sort isθ(Nlog(N)) in all 3 cases
(worst, average, and best) as merge sort always divides the array into two halves and
takes linear time to merge two halves.
Auxiliary Space:
O(N), In merge sort all elements are copied into an auxiliary array. So N
auxiliary space is required for merge sort.