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

Sorting in C Programming

Sorting in C programming

Uploaded by

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

Sorting in C Programming

Sorting in C programming

Uploaded by

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

• It is a process of arranging the given elements in ascending or

descending order
• Different sorting techniques are
• Bubble sort
• Selection sort
• Merge sort
• Insertion sort
• Quick sort
• ……
Consider these as the given elements, which are unsorted
20 40 10 30 50
The elements after sorting in ascending order are
10 20 30 40 50
• Simplest and easiest technique
• Two successive elements A[i] and A[i+1] are compared
if A[i] > A[i+1]
then
exchange A[i] and A[i+1]
• Let us consider the given array elements
A[5] = { 50, 40, 30, 20, 10 }
Sort these array elements in ascending order
The pictorial representation of the array is
Let us consider the tracing part
tracing part
tracing part
tracing part
• From previous example
• There are 5 elements in the array
• There are 4 passes
• In 1st pass no. of comparisons 4, 2nd pass no. of comparisons 3, 3rd pass no.
of comparisons 2, 4th pass no. of comparisons 1

• In general,
• For n elements in the array
• There will be n-1 passes
• No. of comparisons n-1-i
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,n;
clrscr();
printf(“enter the number of elements\n”);
scanf(“%d”,&n);
printf(“enter the array elements\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf(“The sorted array elements are:\n”);
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
getch();
}
enter the number of elements
5
enter the array elements
26418
The sorted array elements are:
1
2
4
6
8
• As the name indicates we first select the smallest element in the list
• Then exchange that with the first element
• Next find second smallest element and swap it with second
element
• So on
• Finally we get all the elements sorted in ascending order
• Let us consider the given array elements
A[5] = { 25, 15, 20, 5, 10 }
Sort these array elements in ascending order
The pictorial representation of the array is
Tracing:
First find smallest element in the list
Complete tracing
• From previous example
• There are 5 elements in the array
• There are 4 passes
• In each pass find smallest element and swap it to appropriate position
• Here POS is used to hold index of smallest element

• In general,
• For n elements in the array
• There will be n-1 passes
• No. of comparisons n-1-i
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,n,pos;
clrscr();
printf(“enter the number of elements\n”);
scanf(“%d”,&n);
printf(“enter the array elements\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n-1;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[pos])
{
pos=j;
}
}
temp=a[pos];
a[pos]=a[i];
a[i]=temp
}
printf(“The sorted array elements are:\n”);
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
getch();
}
enter the number of elements
5
enter the array elements
12 67 4 15 8
The sorted array elements are:
4
8
12
15
67

You might also like