Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Sorting

Sorting is the process of arranging elements in the


list according to their values, in ascending or descending
order.
A sorted list is called an ordered list.
Selection Sort:-

Selection sort algorithm (for ascending order)


i. Find the minimum element in the array and swap it
with the element in the 1st position.
ii. Find the minimum element again in the remaining
array[2, n] and swap it with the element at 2nd
position, now we have two elements at their correct
positions.
iii. We have to do this n-1 times to sort the array.
Selection sort program in C
#include <stdio.h>
#include<conio.h>
void main()
{
int a[5]={6,5,3,11,9};
int n=5;
int i, j, position, swap;
clrscr();
for (i = 0; i < (n - 1); i++)
{
position = i;
for (j = i + 1; j < n; j++)
{
if (a[position] > a[j])
position = j;
}
if (position != i)
{
swap = a[i];
a[i] = a[position];
a[position] = swap;
}
}
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
getch();
}
O/P:-
3 5 6 9 11
Insertion Sort:-
Insertion sort is a simple sorting algorithm that
works similar to the way you sort playing cards in your
hands.
The insertion sort algorithm sorts a list by repeatedly
inserting an unsorted element into the correct position
in a sorted sublist.
Insertion sort is a simple sorting algorithm that builds
the final sorted array (or list) one item at a time by
comparisons.
Insertion sort iterates, consuming one input element
each repetition, and grows a sorted output list.
Algorithm for Insertion Sort
• Step 1 − If the element is the first one, it is already
sorted.
• Step 2 – Move to next element
 Step 3 − Compare the current element with all
elements in the sorted array
 Step 4 – If the element in the sorted array is
smaller than the current element, iterate to the next
element. Otherwise, shift all the greater element in
the array by one position towards the right
 Step 5 − Insert the value at the correct position
 Step 6 − Repeat until the complete list is sorted
C Program for Insertion Sort
#include <stdio.h>
#include<conio.h>
void main()
{
int i;
int a[] = {41,21,50,3,51};
int n = 5;
clrscr();
insertionSort(a, n);
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
getch();
}
int insertionSort(int a[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = a[i];
j = i - 1;
while (j >= 0 && a[j] > key)
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = key;
}
return 0;
}
O/P:
3 21 41 50 51
Factorial Program in C
Factorial Program in C: Factorial of n is the product of
all positive descending integers. Factorial of n is denoted
by n!. For example:
5! = 5*4*3*2*1 = 120
3! = 3*2*1 = 6
Factorial Program in C
#include<stdio.h>
#include<conio.h>
void main()
{
int i,fact=1,number;
clrscr();
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++)
{
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
getch();
}
O/P:
Enter a number:4
Factorial of 4 is:24

Sum of Natural Numbers Using for Loop


#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, sum = 0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 1; i <= n;i++)
{
sum =sum+i;
}
printf("Sum = %d", sum);
getch();
}
O/P:-
Enter a positive integer:10
Sum=55
C program to check Armstrong number in C.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0,temp;
clrscr();
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
getch();
}
O/P:-
enter the number=370
armstrong number
C program to calculate the sum, and average of given
numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,Sum=0,numbers;
float Average;
clrscr();
printf("\nPlease Enter How many Number you want?\n");
scanf("%d",&n);
printf("\nPlease Enter the elements one by one\n");
for(i=0;i<n;++i)
{
scanf("%d",&numbers);
Sum = Sum +numbers;
}
Average = Sum/n;
printf("\nSum of the %d Numbers = %d",n, Sum);
printf("\nAverage of the %d Numbers = %f",n, Average);
getch();
}
O/P:-
Please Enter How many Number you want?
2
Please Enter the elements one by one
10
20
Sum of the 2 Numbers = 30
Average of the 2 Numbers = 15.00
C program for palindrome of string without using
string built in function.
#include <stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string[100];
int start, middle, end, length = 0;
clrscr();
printf("Enter a string\n");
gets(string);
while ( string[length] != '\0' )
length++;
end = length - 1;
middle = length/2;
for( start = 0 ; start < middle ; start++ )
{
if ( string[start] != string[end] )
{
printf("Not a palindrome.\n");
break;
}
end--;
}
if( start == middle )
printf(" It is Palindrome.\n");
getch();
}
O/P:-
Enter a string
EEE
It is Palindrome.

You might also like