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

Elab Sorting

Mommy has developed a sorting technique where she repeatedly sorts items from first to last, swapping any two consecutive unsorted items. One day she needs to sort plates before a wedding but only has M minutes to complete the task. She takes S seconds per swap but doesn't know the total number of sw

Uploaded by

Agent Smith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

Elab Sorting

Mommy has developed a sorting technique where she repeatedly sorts items from first to last, swapping any two consecutive unsorted items. One day she needs to sort plates before a wedding but only has M minutes to complete the task. She takes S seconds per swap but doesn't know the total number of sw

Uploaded by

Agent Smith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Elab sorting

SORT14
QUESTION DESCRIPTION

Mommy is a very active lady. She likes to keep all stuff sorted. She has developed an interesting
technique of sorting stuff over the years. She goes through the items repeatedly from first to last and
whenever she finds two consecutive items unsorted, she puts them in the proper order. She
continues the process until all the items are sorted.

One day Mommy has to attend a wedding ceremony. Suddenly she remembers that she has not
sorted the plates after washing. She has only M minutes left. If she can complete the task within the
remaining time, she will sort her plates and then attend the wedding. However if she cannot, she
decides to skip the task.

She knows that she take S seconds per swap. However she does not know the total number of
swaps required and hence she is in trouble. She wants you to help her out.

Mandatory expression is "x=(s*c)/60"

#include<stdio.h>
int main()
{
int a[1001];
int n,i,j,t,swap,temp,k,m,ss,r;
scanf("%d", &t);
if(t==1)
{
printf("1");
goto a;
}
while(t--)
{
swap=0;
scanf("%d %d %d", &m,&ss,&n);
for(j=0; j<a[j+1];j++)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
swap++;
}
}
int x,s,c;
x=(s*c)/60;
r=swap*ss;
if(r<=m*60)
printf("1\n0\n0");
else
printf("0");
printf("\n");
return 0;
a:
{
return 0;
}
}

SORT8
QUESTION DESCRIPTION

Given an array with all elements greater than or equal to zero.Return the maximum product of two
numbers possible.

Input:

The first line of input contains an integer T denoting the number of test cases.

The first line of each test case is N, N is size of array.

The second line of each test case contains N input A[i].

Mandatory method for this program is "void sort(int a[],int n)"

Output:

Print the maximum product of two numbers possible.

Constraints:

1 ≤ T ≤ 20
1 ≤ N ≤ 50
0 ≤ A[i] ≤ 1000

#include<stdio.h>
void sort(int a[],int n);
int main()
{
int arr[30], i, x, t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&x);
for(i=0;i<x;i++)
{
scanf("%d",&arr[i]);
}
sort(arr, x);
}
return 0;
}
void sort(int a[],int n)
{
int i, j, p=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
if(p<(a[i]*a[j]))
{
p=a[i]*a[j];
}
}
}
}
printf("%d\n",p);
}

SORT4
QUESTION DESCRIPTION

Ramu and Somu both are decided to play a game to Sort the given set of numbers using Insertion
Sort.

So he got The first line of the input contains the number of elements, the second line of the input
contains the numbers to be sorted.
In the output print the status of the array at the 3rd iteration and the final sorted array in the given
format.

Somu will evaluate the result whether its correct or not Mandatory declaration need to be follow as "
void InSort(int arr[], int n)"

#include<stdio.h>

void printArray(int arr[], int n)


{
int i;
printf("Sorted Array:");
for(i=0; i<n; i++)
{
printf("%d ",arr[i]);
}
printf("\n");
}

void InSort(int arr[], int n)


{
int step, i;
for(step=1; step<n; step++)
{
int key = arr[step];
int j=step-1;
while(key<arr[j] && j>=0)
{
arr[j+1] = arr[j];
--j;
}
arr[j+1]=key;
if(step==2)
{
for(i=0;i<n;i++)
printf("%d ",arr[i]);
}
}
printf("\n");
}

int main()
{
int data[30], i, n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&data[i]);
}
InSort(data, n);
printArray(data, n);
return 0;
}

SORT6
QUESTION DESCRIPTION

MS .Dhoni want to play a game when the players are free in the dressing room.

Dhoni have given an array of n distinct elements, the task is to find all elements in array which have
at-least two greater elements than themselves.

Dhoni declared a mandatory conditions like "void sort(int a[],int n)"

Examples:

Input : A[] = {2, 8, 7, 1, 5};


Output : 1 2 5

The output three elements have two or more greater elements

Input : A[] = {7, -2, 3, 4, 9, -1};


Output : -2 -1 3 4

Input:

The first line of input contains an integer T denoting the no of test cases.

Each test case contains two lines .

The first line of input contains an integer n denoting the size of the array.

Then in the next are n space separated values of the array.

#include <stdio.h>
int main()

int t,i,n,j,k,arr[30],temp;

scanf("%d",&t);

for(i=0;i<t;i++)

scanf("%d",&n);

for(j=0;j<n;j++)

{scanf("%d",&arr[j]);}

for(j=0;j<n;j++)

for(k=j+1;k<n;k++)

if(arr[j]>arr[k])

temp=arr[j];

arr[j]=arr[k];

arr[k]=temp;
}

for(j=0;j<n-2;j++)

{printf("%d ",arr[j]);}

printf("\n");

return 0;

SORT13
QUESTION DESCRIPTION

You have to merge the two sorted arrays into one sorted array (in non-increasing order)

Input:

First line contains an integer T, denoting the number of test cases.

First line of each test case contains two space separated integers X and Y, denoting the size of the
two sorted arrays.

Second line of each test case contains X space separated integers, denoting the first sorted array P.

Third line of each test case contains Y space separated integers, denoting the second array Q.

Output:

For each test case, print (X + Y) space separated integer representing the merged array.
#include <stdio.h>

int main() {

int t,i,a,b,arr[20],j,temp,k;

scanf("%d",&t);

for(i=0;i<t;i++)

scanf("%d%d",&a,&b);

for(j=0;j<(a+b);j++)

scanf("%d",&arr[j]);

for(j=0;j<(a+b);j++)

for(k=j+1;k<(a+b);k++)

if(arr[j]<arr[k])

temp=arr[j];

arr[j]=arr[k];

arr[k]=temp;

}
}

for(j=0;j<(a+b);j++)

{printf("%d ",arr[j]);}

printf("\n");

return 0;

SORT9
QUESTION DESCRIPTION

Given an array of integers and two numbers k1 and k2. Find sum of all elements between given two
k1’th and k2’th smallest elements of array. It may be assumed that (1 <= k1 < k2 <= n) and all
elements of array are distinct.

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow.
Each test case contains an integer N, denoting the length of the array. Next line contains N space
seperated integers of the array. Third line contains two space seperated integers denoting k1'th and
k2'th smallest elements.

Output:
For each test case in a new line output the sum of all the elements between k1'th and k2'th smallest
elements.

Constraints:
1<= T <= 100
1<= k1< K2 <= N <=50

#include <stdio.h>

void sortarr(int n,int *a);

int main()
{

int t,i,n,j,a[30],p,q,sum=0;

scanf("%d",&t);

for(i=0;i<t;i++)

scanf("%d",&n);

for(j=0;j<n;j++)

scanf("%d",&a[j]);

/*for(j=0;j<n;j++)

printf("%d ",a[j]);

printf("\n");*/

sortarr(n,a);

/* for(j=0;j<n;j++)

printf("%d ",a[j]);

printf("\n");*/

scanf("%d%d",&p,&q);

for(j=(p);j<(q-1);j++)
{

sum=sum+a[j];

printf("%d\n",sum);sum=0;

return 0;

void sortarr(int n,int *a)

int j,k,t;

for(j=0;j<n;j++)

for(k=j+1;k<(n);k++)

if(a[j]>a[k])

t=a[j];

a[j]=a[k];

a[k]=t;

}
}

SORT3
QUESTION DESCRIPTION

Sort the given set of numbers using Bubble Sort.

The first line of the input contains the number of elements, the second line of the input contains the
numbers to be sorted.

In the output print the status of the array at the 3rd iteration and the final sorted array in the given
format.

Mandatory declaration for function is "void printArr(int arr[], int size)"

#include <stdio.h>

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void bubbleSort(int arr[], int n)


{
int i, j;int a,b;
for (i = 0; i < n-1; i++)
{
a=i;
if(a==3){for (a=0; a< n; a++)
printf("%d ", arr[a]);
printf("\n"); }

for (j = 0; j < n-i-1; j++)


if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
}

void printArray(int arr[], int size)


{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main()
{
int t;scanf("%d",&t);
int i;int arr[t];
for(i=0;i<t;i++)
{ scanf("%d",&arr[i]);}
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array:");
printArray(arr, n);
return 0;
}

SORT1
QUESTION DESCRIPTION

You are given an array A of size N, and Q queries to deal with.

For each query, you are given an integer X, and you're supposed to find out if X is present in the array
A or not.

Mandatory method name is "void quicksort(int x[10],int first,int last)"

Input:

The first line contains two integers, N and Q, denoting the size of array A and number of queries.

The second line contains N space separated integers, denoting the array of elements Ai.

The next Q lines contain a single integer X per line.


Output:

For each query, print YES if the X is in the array, otherwise print NO.

#include <stdio.h>

int main() {

int n,q,arr1[20],arr2[20],j,f,i;

scanf("%d %d",&n,&q);

for(i=0;i<n;i++)

scanf("%d",&arr1[i]);

for(j=0;j<q;j++)

scanf("%d",&arr2[j]);

for(j=0;j<q;j++)

for(i=0;i<n;i++)

if(arr2[j]==arr1[i])

{
f=1;

break;

else

f=0;

if(f==1)

printf("YES\n");

else

printf("NO\n");

return 0;

SORT11
QUESTION DESCRIPTION

In a candy store there are N different types of candies available and the prices of all the N different
types of candies are provided to you.

You are now provided with an attractive offer.

You can buy a single candy from the store and get atmost K other candies ( all are different types )
for free.

Now you have to answer two questions.


Firstly, you have to tell what is the minimum amount of money you have to spend to buy all the N
different candies.

Secondly, you have to tell what is the maximum amount of money you have to spend to buy all the N
different candies.

In both the cases you must utilize the offer i.e. you buy one candy and get K other candies for free.

Mandatory conditions are "static void mergeSort(int a[],int l,int r)"

#include <stdio.h>
static void mergeSort(int a[],int l,int r)
{;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{int i,j,k,l=0,n,m,a[1000],s=0,s1=0,min=0,max=0;
scanf("%d %d",&n,&k);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{for(j=0;j<n-i-1;j++)
{int tm;
if(a[j]>a[j+1])
{tm=a[j];
a[j]=a[j+1];
a[j+1]=tm;}}}
while(s<n)
{min=min+a[l];
l++;
s=s+k+1;}
j=n-1;
while(s1<n)
{max=max+a[j];
j--;
s1=s1+k+1;}
printf("%d ",min);
printf("%d\n",max);}
return 0;
}
SORT7
QUESTION DESCRIPTION

Given two arrays, A and B, of equal size n, the task is to find the minimum value of A[0] * B[0] + A[1] *
B[1] +…+ A[n-1] * B[n-1], where shuffling of elements of arrays A and B is allowed.

Madatory conditions are "void result(int a[],int b[],int n)"


Examples:

Input : A[] = {3, 1, 1} and B[] = {6, 5, 4}.


Output : 23 Minimum value of S = 1*6 + 1*5 + 3*4 = 23.

Input : A[] = { 6, 1, 9, 5, 4 } and B[] = { 3, 4, 8, 2, 4 }


Output : 80. Minimum value of S = 1*8 + 4*4 + 5*4 + 6*3 + 9*2 = 80.

Input:
The first line of input contains an integer denoting the no of test cases.

Then T test cases follow. Each test case contains three lines.

The first line of input contains an integer N denoting the size of the arrays.

#include <stdio.h>
void result(int a[],int b[],int n);
int main()
{
int t, i, j, arr1[30], arr2[30], x, temp;
scanf("%d",&t);
while(t--)
{
scanf("%d",&x);
for(i=0;i<x;i++)
{
scanf("%d",&arr1[i]);
}

for(i=0;i<x;++i)
{
for (j=i+1;j<x;++j)
{
if (arr1[i] > arr1[j])
{
temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
}
}

for(i=0;i<x;i++)
{
scanf("%d",&arr2[i]);
}

for(i=0;i<x;++i)
{
for (j=i+1;j<x;++j)
{
if (arr2[i] < arr2[j])
{
temp = arr2[i];
arr2[i] = arr2[j];
arr2[j] = temp;
}
}
}

result(arr1, arr2, x);


}
return 0;
}

void result(int a[],int b[],int n)


{
int i, fin=0, sum;
for(i=0;i<n;i++)
{
fin+=a[i]*b[i];
sum=fin;
}
printf("%d\n",sum);
}
Sort 15
QUESTION DESCRIPTION
Given an array of distinct positive numbers, the task is to calculate the minimum number of subsets
(or subsequences) from the array such that each subset contains consecutive numbers.

Mandatory variables are “int n and int t”

Input:

The first line of input contains an integer T denoting the no of test cases.

Then T test cases follow. Each test case contains an integer N, denoting the length of the array.

Next line contains N space seperated integers of the array.

Output:

For each test case output a new line denoting count of number of such subset’s that contains
consecutive numbers.

Constraints:

1<=T<=100
1<=N<=50TEST CASE 1

#include <stdio.h>

void arrange_arr(int n,int *arr);

int main()
{

int t;

int n;

int a[30],i,j,c=0,k,p;

scanf("%d",&t);

for(i=0;i<t;i++)

scanf("%d",&n);

for(j=0;j<n;j++)

scanf("%d",&a[j]);

arrange_arr(n,a);

p=0;

// for(j=0;j<n;j++)
//{printf("%d ",a[j]);}

for(j=0,c=0;j<n-1;j++)

if((a[j+1]-a[j])==1)

{p++;}

else

if(p>0)

{c++;p=0;}

if(c>0)

c=c+1;

printf("%d\n",c);
}

return 0;

void arrange_arr(int n,int *arr)

int j,k,t;

for(j=0;j<n;j++)

for(k=j+1;k<n;k++)

if(arr[j]>arr[k])

t=arr[j];

arr[j]=arr[k];
arr[k]=t;

Sort 5
QUESTION DESCRIPTION
Kalaiselvan is going to act as a car driver and he has to drive a car on a track divided into “N” no. of
sub-tracks.

You are also given the value of “K” i.e. the total kilometers a car can drive on each sub-track.

If the car can’t cover a sub-track, you can add any unit of Petrol in it. With each unit of petrol added,
the total kilometers your car can travel will increase by one unit .

Kalai selvan need to declar the mandatory function name as ” void sort(int a[],int n,int k)”

Input:

The first line of input contains an integer T denoting the no of test cases.

Then T test cases follow. Each test case contains two space separated integers N and K.

The second line of each test case contains N space separated integers (A[]) denoting the distance of
each N sub-tracks.
Output:

For each test case in a new line you have to print out the minimum unit of Petrol your car require to
cover all the sub-tracks. If no extra unit of petrol is required, print -1.

#include <stdio.h>

void sort(int a[],int n,int k){}

int main()

int t,n,p,i,arr[20],temp,k,j;

scanf("%d",&t);

for(i=0;i<t;i++)

scanf("%d%d",&n,&p);

for(j=0;j<n;j++)

scanf("%d",&arr[j]);

for(j=0;j<n;j++)

for(k=j+1;k<n;k++)

if(arr[j]<arr[k])
{

temp=arr[j];

arr[j]=arr[k];

arr[k]=arr[j];

if(arr[0]>p)

printf("%d",arr[0]-p);

else

printf("-1");

printf("\n");

return 0;

You might also like