Professional Documents
Culture Documents
DS File
DS File
DS File
PROGRAM-1
AIM:
Program to sort an array using Bubble Sort.
THEORY:
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.
ALGO:
Array A[n] 🡪 to sort, i=0, j=0
(1) Compare A[i] and A[i+1], if A[i] > A[i+1] then swap them
using temp=A[i], A[i]=A[i+1] and A[i+1]=temp.
(2) Now increment i by 1, repeat from step (1) until i<(n-1-j).
(3) Now increment j by 1 and repeat again from step (1) until
j<(n-1).
(4) Finally we will be left with list sorted in ascending order.
FLOW CHART:
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main()
int n,c,i;
scanf("%d",&n);
float *p=(float *)calloc(n,sizeof(float));
for(i=0;i<n;i++)
scanf("%f",(p+i));
Bubble_sort(p,n);
for(int i=0;i<n;i++)
printf("%f ",*(p+i));
for(int i=n-1;i>=0;i--)
printf("%f ",*(p+i));
getch();
int i,j;
float temp;
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(*(p+j)>*(p+j+1))
temp=*(p+j+1);
*(p+1+j)=*(p+j);
*(p+j)=temp;
OUTPUT:
LEARNING:
Through this practical we have learnt how to sort a list of
numbers using Bubble sort.
We have seen the worst and average time complexity = O(n*n)
and best time complexity is O(n).
RESULT:
The practical was successfully completed as shown in the
output.
Jatin 2K18/CO/158
PROGRAM-2
AIM:
Program to sort an array using Selection Sort.
THEORY:
The selection sort algorithm sorts an array by repeatedly finding
the minimum element (considering ascending order) from
unsorted part and putting it at the beginning. The algorithm
maintains two subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element
(considering ascending order) from the unsorted subarray is
picked and moved to the sorted subarray.
ALGO:
(1) Set MIN to location 0.
(2) Search the minimum element in the list
(3) Swap with value at location MIN
(4) Increment MIN to point to next element
(5) Repeat until list is sorted
FLOWCHART:
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main()
int n,c,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%f",(p+i));
Selection_sort(p,n);
for(int i=0;i<n;i++)
printf("%f ",*(p+i));
for(int i=n-1;i>=0;i--)
{
printf("%f ",*(p+i));
getch();
int i,j;
float min;
for(i=0;i<n;i++)
min=*(p+i);
for(j=i+1;j<n;j++)
if(min>*(p+j))
min=*(p+j);
*(p+j)=*(p+i);
*(p+i)=min;
}
}
OUTPUT:
LEARNING:
We have learnt how to sort a list of numbers in ascending order
using Selection sort.
Here time complexity is O(n*n).
RESULT:
The practical was successfully completed as shown in the
output.
Jatin 2K18/CO/158
PROGRAM 3
AIM:
Program to sort an array using insertion sort.
THEORY:
This is an in-place comparison-based sorting algorithm. Here,
a sub-list is maintained which is always sorted. For example,
the lower part of an array is maintained to be sorted. An
element which is to be 'insert'ed in this sorted sub-list, has to
find its appropriate place and then it has to be inserted there.
Hence the name, insertion sort.
The array is searched sequentially and unsorted items are
moved and inserted into the sorted sub-list (in the same array).
This algorithm is not suitable for large data sets as its average
and worst case complexity are of Ο(n2), where n is the number
of items.
ALGO:
(1) If it is the first element, it is already sorted. return 1;
(2) Pick next element
(3) Compare with all elements in the sorted sub-list
(4) Shift all the elements in the sorted sub-list that is greater
than the value to be sorted
(5) Insert the value
(6) Repeat until list is sorted
FLOWCHART:
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main()
int n,c,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%f",(p+i));
Insertion_sort(p,n);
for(int i=0;i<n;i++)
printf("%f ",*(p+i));
for(int i=n-1;i>=0;i--)
{
printf("%f ",*(p+i));
getch();
int i,j;
float temp;
for(i=1;i<n;i++)
temp=*(p+i);
*(p+j+1)=*(p+j);
*(p+j+1)=temp;
OUTPUT:
LEARNING:
We have learnt how to sort a list of numbers using insertion
sort.
Here time complexity is O(n*n).
RESULT:
The practical was successfully completed as shown in the
output.
Jatin 2K18/CO/158
PROGRAM 4
AIM:
FLOWCHART:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
{
int n1=m-f+1,n2=l-m,i,j,k=f;
float b[n1],c[n2];
for(i=0;i<n1;i++)
b[i]=a[f+i];
for(j=0;j<n2;j++)
c[j]=a[m+1+j];
i=0;
j=0;
if(b[i]<c[j])
a[k]=b[i];
k++;
i++;
else
a[k]=c[j];
k++;
j++;
while(i<n1)
a[k]=b[i];
k++;
i++;
while(j<n2)
a[k]=c[j];
k++;
j++;
if(f<l)
int m=(f+l)/2;
mergesort(a,f,m);
mergesort(a,m+1,l);
merge(a,f,m,l);
else
return;
int main()
int n,i;
scanf("%d",&n);
float a[n];
for(i=0;i<n;i++)
scanf("%f",&a[i]);
mergesort(a,0,n-1);
for(i=0;i<n;i++)
{
printf("%f ",a[i]);
return 0;
OUTPUT:
LEARNING:
Jatin 2K18/CO/158
PROGRAM 5
AIM:
FLOWCHART:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i;
printf("Enter the size of list: ");
scanf("%d",&n);
float a[n];
printf("Enter numbers in the list\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
quicksort(a,0,n-1);
printf("Sorted list is: ");
for(i=0;i<n;i++)
{
printf("%f ",a[i]);
}
return 0;}
OUTPUT:
LEARNING: