Professional Documents
Culture Documents
EXPERIMENT – 2
Name: Ajeetha
Reg no: 20BEC1192
TASK 1:
In a given array A, elements are sorted in index positions from 1 to j-1. To insert
element A[j] in the correct position, elements in locations A[j-1], A[j-2],… are
moved one position right until the correct position of A[j] is found.
Now consider “Modified Insertion Sort”, to find the correct position of A[j], search
is performed as ‘linear search’ i.e., element A[j] is compared with elements in
index positions from A[1] to A[j-1], if the condition A[k] < A[j] < A[k+1] where k ϵ
1,2,…., j-1 satisfies then A[j] is inserted in index position k+1 and element in k+1
position is moved to k+2 an so on. Write an algorithm for “Modified Insertion
Sort” and implement the same.
Algorithm:
BEGIN
The length of the array and its elements are taken from the user as input.
Using position variable, the position at which the digit must be added in the given array is found by
comparing the digit value with the array value.
At the index value equal to position the digit is added. The remaining elements are shifted by 1
position to the right.
EXIT.
Code:
#include<stdio.h>
int main()
{
int length,val,d,ch=0,pos,i;
printf("Enter the length of the array: ");
scanf("%d",&length);
int array[length];
printf("Enter the elements in ascending the order: ");
for(i=0;i<length;++i)
{
scanf("%d",&val);
array[i]=val;
}
printf("Enter the digit to be added in the array: ");
scanf("%d",&d);
int array2[length+1];
for(i=0;i<=length;++i)
{
if(array[i]<d)
{
array2[i]=array[i];
}
else
{
pos=i;
break;
}
}
for(i=pos;i<=length;++i)
{
if(i==pos)
{
array2[i]=d;
}
else
{
array2[i]=array[i-1];
}
}
printf("The updated array is: ");
for(i=0;i<=length;++i)
{
printf("%d ",array2[i]);
}
}
Output:
TASK 2:
Given an array of n integers. Sort the elements in even index locations elements
in ascending order and the odd positioned elements in the descending order.
Apply Selection sort to sort the elements.
ALGORITHM:
BEGIN
The length of the array and its elements are taken as input from the user
Variables named n,val,hv,max1,min1,temp,i,j,k are declared and defined as int.
The elements at even positions are stored in even array and elements at odd positions are stored in
odd array.
The elements in odd array are sorted in descending order and elements in even array are sorted in
ascending order.
EXIT
CODE:
#include<stdio.h>
int main()
{
int n,val,hv,max1,min1,temp,i,j=0,k=0;
printf("Enter the length of the array: ");
scanf("%d",&n);
int array[n];
printf("Enter the elements: \n");
for(i=0;i<n;++i)
{
scanf("%d",&val);
array[i]=val;
}
if(n%2==0)
{
hv=n/2;
}
else
{
hv=(n+1)/2;
}
int even[hv],odd[hv];
for(i=0;i<n;i++)
{
if(i%2==0)
{
even[j]=array[i];
j++;
}
else
{
odd[k]=array[i];
k++;
}
}
printf("The elements at the even index are: ");
for(i=0;i<hv;++i)
{
printf("%d ",even[i]);
}
printf("\n The elements at the odd index are: ");
for(i=0;i<hv;++i)
{
printf("%d ",odd[i]);
}
for(i=0;i<hv-1;++i)
{
max1=i;
min1=i;
for(j=i+1;j<hv;++j)
{
if(odd[j]>odd[max1])
{
max1=j;
}
if(even[j]<even[min1])
{
min1=j;
}
}
if(max1!=i)
{
temp=odd[i];
odd[i]=odd[max1];
odd[max1]=temp;
}
if(min1!=i)
{
temp=even[i];
even[i]=even[min1];
even[min1]=temp;
}
}
int new[n];
for(i=0;i<hv;++i)
{
new[2*i]=even[i];
new[(2*i)+1]=odd[i];
}
printf("\n The required array is: ");
for(i=0;i<n;++i)
{
printf("%d ",new[i]);
}
}
OUTPUT:
TASK 3:
The length of the array and its elements are taken from the user as input.
The negative elements are stored in negative variable using a FOR loop.
The positive and negative values of the given array is stored using a FOR loop.
The elements of positive array are sorted in ascending order and elements of the negative array is
sorted in descending order.
The two arrays arr1 and arr2 are concatenated using a FOR loop.
EXIT.
CODE:
#include <stdio.h>
int main() {
int n,i,j,k,nos;
int cnt=0;
int cnt2=0;
printf("Enter the length of the array \n");
scanf("%d",&n);
int arr[n];
printf("Enter the array elements \n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
if(arr[i]<0)
{
cnt++;
}
else
{
cnt2++;
}
}
int cnt3=0;
int cnt4=0;
int arr1[cnt];
int arr2[cnt2];
for(i=0;i<n;i++)
{
if(arr[i]<0)
{
arr1[cnt3]=arr[i];
cnt3++;
}
else
{
arr2[cnt4]=arr[i];
cnt4++;
}
}
int temp;
for(i = 0; i < cnt2 - 1; i++){
for(j = 0; j < cnt2 - i - 1; j++){
if(arr2[j] > arr2[j + 1]){
temp = arr2[j];
arr2[j] = arr2[j + 1];
arr2[j + 1] = temp;
}
}
}
for (i = 0 ; i < ( cnt - 1 ); i++){
for (j= 0 ; j < cnt - i - 1; j++){
if(arr1[j] < arr1[j+1]){
temp=arr1[j];
arr1[j] = arr1[j+1];
arr1[j+1] = temp;
}
}
}
for(i=0;i<cnt;i++)
{
printf("%d ",arr1[i]);
}
for(i=0;i<cnt2;i++)
{
printf("%d ",arr2[i]);
}
}
OUTPUT: