0% found this document useful (0 votes)
45 views

Lab Exercise-1: Prog1: Write A Program To Implement Selection Sort

The document contains algorithms and C programs for selection sort, insertion sort, heap sort, and quick sort. For each sorting algorithm, pseudocode is provided to describe the algorithm, followed by a C program implementation of the algorithm to sort an integer array. The programs take user input for the array size and elements, perform the sorting, and output the sorted array.

Uploaded by

kuku288
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Lab Exercise-1: Prog1: Write A Program To Implement Selection Sort

The document contains algorithms and C programs for selection sort, insertion sort, heap sort, and quick sort. For each sorting algorithm, pseudocode is provided to describe the algorithm, followed by a C program implementation of the algorithm to sort an integer array. The programs take user input for the array size and elements, perform the sorting, and output the sorted array.

Uploaded by

kuku288
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

LAB EXERCISE-1

Prog1: Write a program to implement Selection Sort.


Algorithm:
SelectionSort(A,n)
{
for i 0 to (n-1)
{
imin i
for j (i+1) to (n-1)
{
if(A[j]<A[imin])
{
imin j
}
}
temp A[i]
A[i] A[imin]
A[imin] temp
}
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,min,temp,n;
clrscr();
printf("Enter the number of elemEnts in the array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]); }
for(i=0;i<n;i++)
{ min=i;
for(j=(i+1);j<n;j++)
{

if(a[j]<a[min])
{
min=j; }
temp=a[i];
a[i]=a[min];
a[min]=temp;
}}
printf("\nThe array after SELECTION SORT is: \n");
for(i=0;i<n;i++)
{
printf("%d \t",a[i]);
}
getch();
}

Prog 2:- Write a program to implement Insertion Sort.


Algorithm:
Insertion Sort( A, n)
{
for i 1 to (n-1)
{
value A[i]
hole i
while( hole>0 && A[hole-1]>value )
{
A[hole] A[hole-1]
hole hole-1
}
A[hole] value
}
}

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,value,hole,n;
clrscr();
printf("Enter the number of elements in the array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
value=a[i];
hole=i;
while(hole>0 && a[hole-1]>value)
{
a[hole]=a[hole-1];
hole=hole-1;
}
a[hole]=value;
}
printf("\nThe array after INSERTION SORT is: \n");
for(i=0;i<n;i++)
{
printf("%d \t",a[i]);
}
getch();
}

LAB EXERCISE-2
Prog 1: Write a program to implement Heap Sort.
Algorithm:

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define left(i) (2*i)
#define right(i) (2*i+1)
int max_heapify(int a[],int i,int size)

{ int l,r,largest,temp;
l=left(i);
r=right(i);
if((l<=size)&&a[l]>a[i])
largest=l;
else
largest=i;
if((r<=size)&&a[r]>a[largest])
largest=r;
if(largest!=i)
{ temp=a[i];
a[i]=a[largest];
a[largest]=temp;
max_heapify(a,largest,size);
}
return 0;}
void max_heap(int a[],int size)
{int i;
for(i=floor(size/2);i>=1;i--)
max_heapify(a,i,size);
}
void heapsort(int a[],int size)
{ int temp,i,s;
max_heap(a,size);
s=size;
for(i=size;i>=2;i--)
{ temp=a[1];
a[1]=a[i];
a[i]=temp;
s--;
max_heapify(a,1,s); }
printf("\n After Sorting: ");
for(i=1;i<=size;i++)
{ printf("%d \t",a[i]);
}}
void main()
{int a[20],size,i;
clrscr();
printf("Enter the size of the heap: ");
scanf("%d",&size);

printf("\nEnter the elements of the heap: ");


for(i=1;i<=size;i++)
scanf("%d",&a[i]);
heapsort(a,size);
getch();}

Prog 2:- Write a program to implement Quick Sort.


Algorithm:
QuickSort(A low,high)
{
if(low<high)
m partition(A,low,high)
QuickSort(A,low,m-1)
QuickSort(A,m+1,high)
}
Partition(A, low, high)
{
pivot A[low]
i low
j high
while(i<=j) do
{
while(A[i]<=pivot) do
i i+1
while(A[j]>= pivot) do
j j-1
if(i<=j) then
swap(A[i],A[j])
}
swap(A[low],A[j])
return j
}

#include<stdio.h>
#include<conio.h>
void quicksort(int [10],int,int);
void main(){
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("After Quick sort: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
getch();}
void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;
if(first<last){ pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);}}

You might also like