Uploaded by Aditya Singh

SES Lab

advertisement
Software for Embedded Systems
Sorting Algorithms in C
Name – Aditya Singh
Reg. No. – 2022H1400148
Selection Sort, Bubble Sort, Insertion Sort and Merge Sort
1. Selection Sort
Code
#include <stdio.h>
void selectionsort(int arr[],int n)
{
int i,j,temp; //i and j are looping variables
//temp is used for swapping
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[j]<arr[i])
{
//Code for swapping
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
void printarray(int arr[], int n)
{
int i;
for(i=0;i<n;i++)
{
//Printing each element of the array
printf("%d ",arr[i]);
}
}
int main()
{
int arr[] = {7, 15, 2, 12, 11, 17};
//array_length = (total size of the array) / (size of the first element in the array)
int n = sizeof(arr)/sizeof(arr[0]); //Length of the array
printf("Before selection sorting: \n");
printarray(arr,n); //Calling the print array function which has been defined above
selectionsort(arr,n); //Calling the sorting function to perform the sorting
printf("\nAfter selection sorting: \n");
printarray(arr,n); //Printing the array after sorting
return 0; //returning zero since main function is defined int
}
Output
2. Bubble Sorting
Code
#include <stdio.h>
void bubblesort(int arr[],int n)
{
int i,j,temp; //i and j are looping variables
//temp is used for swapping
//Compare consecutive items
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
//Code for swapping
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
void printarray(int arr[], int n)
{
int i;
for(i=0;i<n;i++)
{
//Printing each element of the array
printf("%d ",arr[i]);
}
}
int main()
{
int arr[] = {7, 15, 2, 12, 11, 17};
//array_length = (total size of the array) / (size of the first element in the array)
int n = sizeof(arr)/sizeof(arr[0]); //Length of the array
printf("Before bubble sorting: \n");
printarray(arr,n); //Calling the print array function which has been defined above
bubblesort(arr,n); //Calling the sorting function to perform the sorting
printf("\nAfter bubble sorting: \n");
printarray(arr,n); //Printing the array after sorting
return 0; //returning zero since main function is defined int
}
Output
3. Insertion Sort
Code
#include <stdio.h>
void insertionsort(int arr[],int n)
{
int i,j,temp; //i and j are looping variables
//temp is for storing the current element
for(i=1;i<n;i++) //Zeorth element doesnot have any other element in the left
{
temp = arr[i];
j = i-1;
while (j>=0 && arr[j]>temp)
{
arr[j+1] = arr[j];
j = j - 1;
}
arr[j+1] = temp;
}
}
void printarray(int arr[], int n)
{
int i;
for(i=0;i<n;i++)
{
//Printing each element of the array
printf("%d ",arr[i]);
}
}
int main()
{
int arr[] = {7, 15, 2, 12, 11, 17};
//array_length = (total size of the array) / (size of the first element in the array)
int n = sizeof(arr)/sizeof(arr[0]); //Length of the array
printf("Before insertion sorting: \n");
printarray(arr,n); //Calling the print array function which has been defined above
insertionsort(arr,n); //Calling the sorting function to perform the sorting
printf("\nAfter insertion sorting: \n");
printarray(arr,n); //Printing the array after sorting
return 0; //returning zero since main function is defined int
}
Output
4. Merge Sort
Code
#include <stdio.h>
#include <stdlib.h>
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2]; //temperory arrays of right and left subportions
//Copying data to temperory arrays
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
//Merging temp arrays
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
//Copy the remaining elements of L[]
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
//Copy the remaining elements of R[]
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
//l - left index, r - right index
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
// Calculate midpoint
int m = l + (r - l) / 2;
// Sorting the halves
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
int main()
{
int arr[] = { 1, 11, 3, 15, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
printf("Before merge sorting: \n");
printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
printf("\nAfter merge sorting: \n");
printArray(arr, arr_size);
return 0;
}
Output
Complexity Analysis
Selection, Bubble and Insertion Sort
Time Complexity – O(n^2)
Space Complexity - O(1)
Merge Sort
Time Complexity – O(n * log(n))
Space Complexity - O(n)
Download