Y25 DST Programs
Y25 DST Programs
#include<stdio.h>
int main() {
int a[5] = {8, 3, 12, 10, 4};
int i, j, key;
for (i = 1; i < 5; i++)
{
key = a[i];
j = i - 1;
// Store the current element
// Shift elements of the sorted portion of the array
while (j >= 0 && a[j] > key) {
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = key; // Insert the key into the correct position
}
printf("After sorting using insertion sort:\n");
for (i = 0; i < 5; i++) {
printf("%d ", a[i]);
}
return 0;
}
//INSERTION SORT USING FUNCTIONS
#include <stdio.h>
// 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;
}
}
int main()
{
int n;
return 0;
}
//SHELL SORT
#include<stdio.h>
int main()
{
int n,i,j,t,gap,a[100];
printf("enter n value:");
scanf("%d",&n);
printf("enter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(gap=n/2;gap>=1;gap=gap/2)
{
for(j=gap;j<n;j++)
{
for(i=j-gap;i>=0;i=i-gap)
{
if(a[i+gap]>a[i])
{
break;
}
else
{
t=a[i];
a[i]=a[i+gap];
a[i+gap]=t;
}
}
}
}
printf("The sorted array is:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
//MERGE SORT
#include <stdio.h>
int a[30]; // Define array globally for use in both `merge` and `mergesort`
void merge(int low, int mid, int high)
{
int b[30]; // Temporary array for merging
int i = low, j = mid + 1, k = low;
// Merge the two halves
while (i <= mid && j <= high) {
if (a[i] < a[j]) {
b[k] = a[i];
i++;
} else {
b[k] = a[j];
j++;
}
k++;
}
// Copy remaining elements from the left half
while (i <= mid) {
b[k] = a[i];
i++;
k++;
}
// Copy remaining elements from the right half
while (j <= high) {
b[k] = a[j];
j++;
k++;
}
// Copy the merged elements back to the original array
for (i = low; i <= high; i++) {
a[i] = b[i];
}
}
void mergesort(int low, int high) {
if (low < high) {
int mid = (low + high) / 2; // mid variable declaration
mergesort(low, mid);
mergesort(mid + 1, high);
merge(low, mid, high);
}
}
int main() {
int n, i;
// Input the size of the array
printf("Enter the number of elements: ");
scanf("%d", &n);
// Input the elements of the array
printf("Enter the elements: ");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
// Call mergesort
mergesort(0, n - 1);
// Output the sorted array
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
//QUICK SORT
#include<stdio.h>
void quicksort(int a[20],int start,int end)
{
int pivot,i,j,temp;
pivot=start;
i=start;
j=end;
while(i<j)
{
while(i<j)
{
while(a[i]<=a[pivot])
i++;
while(a[j]>a[pivot])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
quicksort(a,start,j-1);
quicksort(a,j+1,end);
}
}
int main()
{
int n,a[20];
printf("Enter the size of the Array\n");
scanf("%d",&n);
printf("Enter the elements into the Array\n");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
quicksort(a,0,n-1);
printf("After Sorting Array elements are:\n");
for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}
TIME COMPLEXITIES