0% found this document useful (0 votes)
8 views11 pages

Y25 DST Programs

The document contains C code implementations for various sorting algorithms including Insertion Sort, Shell Sort, Merge Sort, and Quick Sort, along with their time complexities. Each algorithm is presented with a main function that allows user input for sorting an array of integers. Additionally, a table summarizes the best, average, and worst-case time complexities for each sorting method.

Uploaded by

indrahasasai26
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)
8 views11 pages

Y25 DST Programs

The document contains C code implementations for various sorting algorithms including Insertion Sort, Shell Sort, Merge Sort, and Quick Sort, along with their time complexities. Each algorithm is presented with a main function that allows user input for sorting an array of integers. Additionally, a table summarizes the best, average, and worst-case time complexities for each sorting method.

Uploaded by

indrahasasai26
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/ 11

//INSERTION SORT

#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>

// Function to perform insertion sort


void insertionSort(int arr[], int n)
{
int i, j,KEY;
for (i = 1; i < n; i++)
{
KEY = arr[i];
j = i - 1;

// 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;
}
}

// Function to print an array


void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}

int main()
{
int n;

// Get the size of the array from the user


printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
// Get the elements of the array from the user
printf("Enter the elements:\n");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Original array: ");
printArray(arr, n);
insertionSort(arr, n);
printf("Sorted array: ");
printArray(arr, 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

SORT-TYPE BEST-CASE AVERAGE-CASE WORST-CASE

INSERTION O(N) O(N^2) O(N^2)


SHELL O(N LOG N) O(N LOG N) O(N^2)
QUICK O(N LOG N) O(N LOG N) O(N^2)
MERGE O(N LOG N) O(N LOG N) O(N LOG N)
BUCKET O(N+K) O(N+K) O(N^2)

You might also like