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

Practical: - 4: Aim: Write A Program To Implement Selection Sort in C Language

The document contains code to implement selection sort and quicksort algorithms in C language. The selection sort code takes in an array of 5 integers, finds the minimum element in the unsorted portion of the array and swaps it with the first element, repeating this process to fully sort the array. The quicksort code takes in an array of 10 integers, uses partition exchange sort to recursively divide the array into subproblems and solve them to fully sort the array. Both programs print the input and sorted output arrays.

Uploaded by

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

Practical: - 4: Aim: Write A Program To Implement Selection Sort in C Language

The document contains code to implement selection sort and quicksort algorithms in C language. The selection sort code takes in an array of 5 integers, finds the minimum element in the unsorted portion of the array and swaps it with the first element, repeating this process to fully sort the array. The quicksort code takes in an array of 10 integers, uses partition exchange sort to recursively divide the array into subproblems and solve them to fully sort the array. Both programs print the input and sorted output arrays.

Uploaded by

Pankaj Saini
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

PRACTICAL: - 4

Aim: Write a program to implement Selection Sort in C language:#include<stdio.h>


#include<conio.h>
void main()
{
clrscr();
int A[5];
int i,j,temp,min_index,n=5;
printf("\n Enter the array:\n");
for(i=0;i<n;i++)
{
printf(" \n ");
scanf("%d",&A[i]);
}
for(i=0;i<n-1;i++)
{
min_index=i;
for(j=i+1;j<n;j++)
{
if(A[j]<A[min_index])
{
min_index=j;
}
}
temp=A[min_index];
A[min_index]=A[i];
A[i]=temp;
}
printf("\n");
printf(" Sorted array:");
for(i=0;i<n;i++)
{
printf("\n");
printf("\n A[%d]=%d",i,A[i]);
}

getch();
}

OUTPUT:-

PRACTICAL: - 5
Aim: Write a program to implement sorting by Quick sort in C language:#include<stdio.h>
#include<conio.h>
int a[10];
int partition(int p,int r)
{
int x,j,i,temp;
x=a[r]; i=p-1;
for(j=p;j<r;j++)
{
if(a[j]<=x)
{
i=i+1;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[r];
a[r]=a[i+1];
a[i+1]=temp;
return (i+1);
}
void qsort(int p,int r)
{
int q;
if(p<r)
{
q=partition(p,r);
qsort(p,q-1);
qsort(q+1,r);
}
}
void main()
{

int p=0,r=4,i;
clrscr();
printf("\n Enter the array:");
for(i=0;i<5;i++)
{
printf(" \n");
Scanf("%d",&a[i]);
}
qsort(p,r);
printf("\n Sorted array:");
for(i=0;i<5;i++)
{
printf(" \n");
printf("%d ",a[i]);
}
getch();
}

OUTPUT:-

You might also like