0% found this document useful (0 votes)
9 views6 pages

Ds 4

The document outlines the implementation of three sorting algorithms in C: Bubble Sort, Insertion Sort, and Selection Sort. Each algorithm is described with its methodology and includes sample code for sorting an array of integers in ascending order. The conclusion highlights the efficiency of each algorithm, noting that all have O(n²) worst-case complexity, making them less suitable for large datasets.

Uploaded by

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

Ds 4

The document outlines the implementation of three sorting algorithms in C: Bubble Sort, Insertion Sort, and Selection Sort. Each algorithm is described with its methodology and includes sample code for sorting an array of integers in ascending order. The conclusion highlights the efficiency of each algorithm, noting that all have O(n²) worst-case complexity, making them less suitable for large datasets.

Uploaded by

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

Aim: -Write a C program that implements the following sorting methods

to sort a given list of integers in ascending order


i) Bubble sort
ii) Insertion Sort
iii) Selection Sort
a) Bubble Sort Algorithm:

 Bubble Sort is a simple algorithm which is used to sort a given set of


n elements provided in form of an array with n number of elements.
Bubble Sort compares all the element one by one and sort them
based on their values.
 If the given array has to be sorted in ascending order, then bubble
sort will start by comparing the first element of the array with the
second element, if the first element is greater than the second
element, it will swap both the elements, and then move on to
compare the second and the third element, and so on.
 If we have total n elements, then we need to repeat this process for
n-1 times.

Problem Description:

You are given an array of N integers. Your task is to sort the array in
ascending order using the Bubble Sort algorithm. The algorithm should
repeatedly compare adjacent elements and swap them if they are in the
wrong order, until the entire array is sorted.

Implementing Bubble Sort Algorithm:

 Start at the beginning of the array.


 Compare the first two elements:
 If the first element is greater than the second, swap them.
 Move to the next pair and repeat the comparison/swapping process.
 Continue this for the entire array; the largest element will "bubble
up" to the last position.
 Repeat the process for the remaining elements (excluding the last
sorted ones).
 If no swaps occur in a full pass, the array is already sorted, and we
stop early.
Program:
#include<stdio.h>
int main()
{
int a[10],i,j,temp,n,flag;
printf("enter no of elements");
scanf("%d",&n);
printf("enter the elements into the array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("before sorting");
for(i=0;i<n;i++)
{
printf("\n%d\t",a[i]);
}
for(i=0;i<n-1;i++)
{
flag=0;
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag=1;
}
}
if(flag==0)
break;
}
printf("\nAfter sorting");
for(i=0;i<n;i++)
{
printf("\n%d\t",a[i]);
}

return 0;
}

Input and Output Processing:

Test case1:
b) Insertion Sort Algorithm:

Insertion Sort is a simple and efficient sorting algorithm that builds the
sorted array one element at a time by inserting each element into its
correct position.

Implementing Insertion Sort Algorithm:

 Assume the first element is already sorted.


 Take the next element and compare it with the sorted part of the
array.
 Shift elements in the sorted part to make space if needed.
 Insert the current element into its correct position.
 Repeat for all elements until the entire array is sorted.

Program:
#include<stdio.h>
int main()
{
int a[10],i,j,temp,n;
printf("enter no of elements");
scanf("%d",&n);
printf("enter the elements into the array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("before sorting\t");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(j>=0 && a[j]>temp)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
printf("\nafter sorting\t");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return 0;
}

Input and Output Processing:

c) Selection Sort Algorithm:

Selection Sort is a simple sorting algorithm that divides the array into
sorted and unsorted parts and repeatedly selects the smallest
element from the unsorted part to place it in its correct position.

Implementing Selection Sort Algorithm:

 Start with the first element as the minimum.


 Search for the smallest element in the unsorted part.
 Swap it with the first element of the unsorted part.
 Move to the next element and repeat the process for the
remaining array.
 Continue until the entire array is sorted.

Program:
#include<stdio.h>
int main()
{
int a[10],i,j,temp,n;
printf("enter no of elements");
scanf("%d",&n);
printf("enter the elements into the array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("before sorting\t");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(j>=0 && a[j]>temp)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
printf("\nafter sorting\t");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return 0;
}
Input and Output Processing:

Conclusion:
 Insertion Sort is generally the most efficient among the three for
small or nearly sorted data.
 Selection Sort performs fewer swaps than Bubble Sort but is still
inefficient for large datasets.
 Bubble Sort is the least efficient as it makes the most swaps but
is useful for learning purposes.
 All three algorithms have O(n²) worst-case complexity,
making them impractical for large datasets.

You might also like