DS File

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

Jatin 2K18/CO/158

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 Bubble_sort(float *,int);

void main()

int n,c,i;

printf("Enter the number of entries in list\n");

scanf("%d",&n);
float *p=(float *)calloc(n,sizeof(float));

printf("Enter the numbers in the list\n");

for(i=0;i<n;i++)

scanf("%f",(p+i));

Bubble_sort(p,n);

printf("Sorted list in ascending order is\n");

for(int i=0;i<n;i++)

printf("%f ",*(p+i));

printf("\nSorted list in descending order is\n");

for(int i=n-1;i>=0;i--)

printf("%f ",*(p+i));

getch();

void Bubble_sort(float *p,int n)

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 Selection_sort(float *,int);

void main()

int n,c,i;

printf("Enter the number of entries in list\n");

scanf("%d",&n);

float *p=(float *)calloc(n,sizeof(float));

printf("Enter the numbers in the list\n");

for(i=0;i<n;i++)

scanf("%f",(p+i));

Selection_sort(p,n);

printf("Sorted list in ascending order is\n");

for(int i=0;i<n;i++)

printf("%f ",*(p+i));

printf("\nSorted list in descending order is\n");

for(int i=n-1;i>=0;i--)
{

printf("%f ",*(p+i));

getch();

void Selection_sort(float *p,int n)

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 Insertion_sort(float *,int);

void main()

int n,c,i;

printf("Enter the number of entries in list\n");

scanf("%d",&n);

float *p=(float *)calloc(n,sizeof(float));

printf("Enter the numbers in the list\n");

for(i=0;i<n;i++)

scanf("%f",(p+i));

Insertion_sort(p,n);

printf("Sorted list in ascending order is\n");

for(int i=0;i<n;i++)

printf("%f ",*(p+i));

printf("\nSorted list in descending order is\n");

for(int i=n-1;i>=0;i--)
{

printf("%f ",*(p+i));

getch();

void Insertion_sort(float *p,int n)

int i,j;

float temp;

for(i=1;i<n;i++)

temp=*(p+i);

for(j=i-1;j>=0 && *(p+j)>temp;j--)

*(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:

Program to sort an array using merge sort.


THEORY:
Like QuickSort, Merge Sort is a Divide and Conquer algorithm.
It divides input array in two halves, calls itself for the two halves
and then merges the two sorted halves. The merge()
function is used for merging two halves. The merge(arr, l, m, r)
is key process that assumes that arr[l..m] and arr[m+1..r] are
sorted and merges the two sorted sub-arrays into one.
ALGO:

(1)Divide the unsorted list into N sublists, each


containing 1 element.
(2)Take adjacent pairs of two singleton lists and merge them
to form a list of 2 elements. N will now convert into N/2 lists
of size 2.
(3)Repeat the process till a single sorted list of obtained.

FLOWCHART:
SOURCE CODE:
#include <stdio.h>

#include <stdlib.h>

void merge(float a[],int f,int m,int l)

{
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;

while(i<n1 && j<n2)

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++;

void mergesort(float a[],int f,int l)

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;

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]);

mergesort(a,0,n-1);

printf("Sorted list is: ");

for(i=0;i<n;i++)

{
printf("%f ",a[i]);

return 0;

OUTPUT:

LEARNING:

We learnt how to sort a list of numbers using merge sort.


Here merge uses recurrence so its recursive eqn is
T(n)=2T(n/2)+Theta(n).
And time complexity is Theta(n Log(n)).
RESULT:
The practical was successfully completed as shown in the
output.

Jatin 2K18/CO/158
PROGRAM 5
AIM:

Program to sort an array using quick sort.


THEORY:
Like Merge Sort, QuickSort is a Divide and Conquer algorithm.
It picks an element as pivot and partitions the given array
around the picked pivot. There are many different versions of
quickSort that pick pivot in different ways.
1. Always pick first element as pivot.
2. Always pick last element as pivot
3. Pick a random element as pivot.
4. Pick median as pivot.
The key process in quickSort is partition(). Target of partitions
is, given an array and an element x of array as pivot, put x at its
correct position in sorted array and put all smaller elements
(smaller than x) before x, and put all greater elements (greater
than x) after x. All this should be done in linear time.
ALGO:
Quick sort pivot algorithm
(1) Choose the highest index value has pivot
(2) Take two variables to point left and right of the list
excluding pivot
(3) left points to the low index
(4) right points to the high
(5) while value at left is less than pivot move right
(6) while value at right is greater than pivot move left
(7) if both step 5 and step 6 does not match swap left and
right
(8) if left ≥ right, the point where they met is new pivot

Quick sort algorithm


(1) Make the right-most index value pivot
(2) partition the array using pivot value
(3) quicksort left partition recursively
(4) quicksort right partition recursively

FLOWCHART:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>

int partition(float a[],int f,int l)


{
int i=f+1,j=l;
float temp;
label:
while(a[f]>a[i])
i++;
while(a[f]<a[j])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
goto label;
}
else
{
temp=a[j];
a[j]=a[f];
a[f]=temp;
}
return j;
}

void quicksort(float a[],int f,int l)


{
if(f<l)
{
int p=partition(a,f,l);
quicksort(a,f,p-1);
quicksort(a,p+1,l);
}
else
return;
}

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:

We have learnt how to sort a list of numbers using quick sort.


Here let us analyse quick sort.
In general time taken by quick sort is T(n)=T(k)+T(n-k-
1)+Theta(n).
In worst case T(n)=T(n-1)+Theta(n).
In best case T(n)=2T(n/2)+Theta(n).
In average case T(n)=T(n/9)+T(9n/10)+Theta(n).
RESULT:
The practical was successfully completed as shown in the
output.

You might also like