0% found this document useful (0 votes)
5 views15 pages

M 3 Sorting Updated

Uploaded by

Abhinav v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

M 3 Sorting Updated

Uploaded by

Abhinav v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

INSERTION SORT:

INSERTION SORT:
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t, flag = 0;
scanf("%d", &n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 1 ; c <= n - 1; c++)
{
t = array[c];
for (d = c - 1 ; d >= 0; d- -)
{
if (array[d] > t)
{
array[d+1] = array[d];
flag = 1;
}
else
break;
}
if (flag)
array[d+1] = t;
}
printf("INSERTION SORT\n");
for (c = 0; c <= n - 1; c++) {
printf("%d\n", array[c]);
}

return 0;
}

Best case complexity of insertion sort is O(n), average and the worst
case complexity is O(n2).

INSERTION SORT:
#include <stdio.h>
void insertionsort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main()
{
int n, arr[1000], c;
scanf("%d", &n);
for (c = 0; c < n; c++)
scanf("%d", &arr[c]);
insertionsort(arr, n);
for (c = 0; c < n; c++)
printf("%d\n", arr[c]);
return 0;
}

The best case time complexity of insertion sort is O(N) under the
condition that the array is already sorted while the worst case reaches
up to O(N2) and on deducing the average case, it goes on to be O(N2) as
well.
SELECTION SORT:
#include <stdio.h>
void selectionsort(int array[], int size){
int i, j, imin;
for(i = 0; i<size-1; i++) {
imin = i; //get index of minimum data
for(j = i+1; j<size; j++)
if(array[j] < array[imin])
imin = j;
//placing in correct position
int temp;
temp = array[i];
array[i] = array[imin];
array[imin] = temp;
}
}
int main()
{
int n, arr[1000], c;
scanf("%d", &n);
for (c = 0; c < n; c++)
scanf("%d", &arr[c]);
selectionsort(arr, n);
printf("SELECTION SORT \n");
for(int c = 0; c<n; c++)
printf("%d ", arr[c]);
return 0;
}

BUBBLE SORT:

void bubblesort(int numbers[], intarray_size){


inti, j, temp;
for (i = (array_size - 1); i>= 0; i--)
for (j = 1; j <= i; j++)
if (numbers[j-1] > numbers[j])
{
temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
}
}
The best-case time complexity of Bubble Sort is: O(n)
The avg-case time complexity of Bubble Sort is: O(n²)
The worst-case time complexity of Bubble Sort is: O(n²)

COUNTING SORT:
#include<stdio.h>
int countingsort(int a[], int n){
int i, j;
int output[15], c[100];
for (i = 0; i < 100; i++)
c[i] = 0;
for (j = 0; j < n; j++)
++c[a[j]];
for (i = 1; i <= 99; i++)
c[i] += c[i-1];
for (j = n-1; j >= 0; j--) {
output[c[a[j]] - 1] = a[j];
--c[a[j]];
}
printf("COUNTING SORTING");
for (i = 0; i<n; i++)
printf("%d ", output[i]);
}
void main(){
int n, arr[1000], c;
scanf("%d", &n);
for (c = 0; c < n; c++)
scanf("%d", &arr[c]);
countingsort(arr, n);
}

QUICK SORT: (RECURSION)


#include<stdio.h>
void quickSort(int[], int, int);
int partition(int[], int, int);
void swap(int*, int*);
int main()
{
int n,i;
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
quickSort(arr,0,n-1);
for(i=0;i<n;i++)
printf("%d ",arr[i]);
return 0;
}

void quickSort(int arr[], int start, int end)


{
if(start < end)
{
int pIndex = partition(arr, start, end);
quickSort(arr, start, pIndex-1);
quickSort(arr, pIndex+1, end);
}
}
int partition(int arr[], int start, int end)
{
int pIndex = start;
int pivot = arr[end];
int i;
for(i = start; i < end; i++)
{
if(arr[i] < pivot)
{
swap(&arr[i], &arr[pIndex]);
pIndex++;
}
}
swap(&arr[end], &arr[pIndex]);
return pIndex;
}

void swap(int *x, int *y)


{
int t = *x;
*x = *y;
*y = t;
}
MERGE SORT: (RECURSION)

MERGE SORT - RECURSION


#include <stdio.h>
// Merge two subarrays L and M into arr
void merge(int arr[], int p, int q, int r) {
// Create L ← A[p..q] and M ← A[q+1..r]
int n1 = q - p + 1;
int n2 = r - q;
int L[n1], M[n2];
for (int i = 0; i < n1; i++)
L[i] = arr[p + i];
for (int j = 0; j < n2; j++)
M[j] = arr[q + 1 + j];
// Maintain current index of sub-arrays and main array
int i, j, k;
i = 0;
j = 0;
k = p;
// Until we reach either end of either L or M, pick larger among
// elements L and M and place them in the correct position at A[p..r]
while (i < n1 && j < n2) {
if (L[i] <= M[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = M[j];
j++;
}
k++;
}
// When we run out of elements in either L or M,
// pick up the remaining elements and put in A[p..r]
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = M[j];
j++;
k++;
}
}
// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r) {
if (l < r) {
// m is the point where the array is divided into two subarrays
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
// Merge the sorted subarrays
merge(arr, l, m, r);
}
}
int main() {
int n, arr[1000], c;
scanf("%d", &n);
for (c = 0; c < n; c++)
scanf("%d", &arr[c]);
mergeSort(arr, 0, n - 1);
for (c = 0; c < n; c++)
printf("%d ", arr[c]);
}

BEST CASE AVERAGE CASE WORST CASE


Insertion Sort O(n) O(n2) O(n2)
Selection Sort O(n2) O(n2) O(n2)
Bubble Sort O(n) O(n2) O(n2)
Counting Sort O(n) O(n+k) O(n+k)
Quick Sort O(n log n) O(n log n)
Merge Sort O(n log n) O(n2)

You might also like