Вы находитесь на странице: 1из 9

EX 11

11. IMPLEMENTATION OF INSERTION SORT,BUBBLE SORT,QUICK SORT,MERGE


SORT

11(a)INSERTION SORT

AIM:

To write a C program to perform insertion sort.


ALGORITHM:
Step 1: Read the input list size.
Step 2: Read the list elements.
Step 3: pass=1
Step 4: Repeat the following steps until pass reach size-1 (for N-1 passes)
i) key=list[Pass]
ii) swap=pass-1
iii) Repeat the following steps if this condition is true list[swap]>key
and swap >= 0.(for comparison).
a) list [sawp+1]=list[swap ]
b) swap—
iv) list [swap+1]
Program:

#include<stdio.h>

int main()
{
int i,j,n,temp,a[30];
printf("\nEnter the number of elements:");
scanf("%d",&n);
printf("\nEnter the elements\n");

for(i=0;i<n;i++)
{
printf("Enter a[%d] = ",i+1);
scanf("%d",&a[i]);
}

for(i=1;i<=n-1;i++)
{
temp=a[i];
j=i-1;

while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j]; //moves element forward
j=j-1;
}

a[j+1]=temp; //insert element in proper place


}

printf("\nSorted list is as follows\n");


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

printf("\na[%d] = %d ",i+1,a[i]);
}

return 0;
}

SAMPLE OUTPUT:
Enter the number of elements: 5
Enter the elements 12
5
2
8
3
Sorted list is as follows
2 3 5 8 12

RESULT:
Thus the C program for Insertion sort was created, executed and output was
verified successfully.

11(b) Bubble Sort

AIM:
To write a C program to perform bubble sort.
ALGORITHM:
Step 1: Read the input list size.
Step 2: Read the list elements.
Step 3: Assign count =0
Step 4: Repeat the loop until count less than list size.(for n-1 passes)
a) Assign inner_count=count+1
b) Repeat the loop inner_count less than list size (for arrange the
list)
1) If list [count]> list [inner_count] (check element value )
2) temp = list [count]
3) list [count]=list[inner_count]
4) a[inner_count]=temp

Program:
#include<stdio.h>

int main()
{
int a[50],n,i,j,temp;
printf("Enter the size of array: ");
scanf("%d",&n);
printf("Enter the array elements: ");

for(i=0;i<n;++i)
{ printf("a[%d]=",i+1);
scanf("%d",&a[i]);
}
for(i=1;i<n;++i)
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("\nArray after sorting: ");
for(i=0;i<n;++i)
printf("\na[%d]=%d \n ",i,a[i]);

return 0;
}

SAMPLE OUTPUT:
Enter the size of array: 5
Enter the array elements: 12
5
2
8
3
array after sorting: 2 3 5 8 12

RESULT:
Thus the C program for Bubble sort was created, executed and output was
verified successfully.

11(c) QUICK SORT

AIM:

To write a C program to perform quick sort.


ALGORITHM:
Step1: Get the value of how many no. to be sorted.
Step2: Get the elements from the user.
Step3: Two function quicksort() and swap().
a) Quick sort to perform sorting.
b)Swap() is just to rearrange the values.
Step4: Quick sort algorithm works by partitioning the array to be sorted, then
recursively sorting each partition.
Step5:Display the sorted value.

Program:
#include <stdio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);
int main()
{
int a[50],n,i;
printf("How many elements?");
scanf("%d",&n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

quick_sort(a,0,n-1);
printf("\nArray after sorting:");

for(i=0;i<n;i++)
printf("%d ",a[i]);

return 0;
}

void quick_sort(int a[],int l,int u)


{
int j;
if(l<u)
{
j=partition(a,l,u);
quick_sort(a,l,j-1);
quick_sort(a,j+1,u);
}
}

int partition(int a[],int l,int u)


{
int v,i,j,temp;
v=a[l];
i=l;
j=u+1;

do
{
do
i++;

while(a[i]<v&&i<=u);

do
j--;
while(v<a[j]);

if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);

a[l]=a[j];
a[j]=v;

return(j);
}

SAMPLE OUTPUT:
How many elements? 5
Enter array elements: 12
5
2
8
3
Array after sorting: 2 3 5 8 12
RESULT:
Thus the C program for quicksort was created, executed and output was verified
successfully.

11(d) MERGE SORT

AIM:

To write a C program to perform Merge sort.


ALGORITHM:
Step1:If the list is of length 0 or 1, then it is already sorted. Otherwise:
Step2: Divide the unsorted list into two sublist of about half the size.
Step3: Sort each sublist recursively by re-applying merge sort.
Step4: Merge the two sublist back into one sorted list.

Program:
#include<stdio.h>
#include<conio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
void main()
{
int a[30],n,i;
clrscr();
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nSorted array is:");
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1&&j<=j2)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

SAMPLE OUTPUT:
Enter no of elements: 5
Enter array elements: 12
5
2
8
3
Sorted array is : 2 3 5 8 12

RESULT:
Thus the C program for Merge sort was created, executed and output was verified
successfully.

Вам также может понравиться