Touay Stpy HeLPee
SuMIL POONA
wuaw.ignousite.com
Course Code : BCSL-033
Course Title: Data and File Structures Lab
Assignment Number : BCA(3)/L-033/Assignment/2022-23
Maximum Marks : 50 seer
Weightage : 25% www.ignousite.com hex i
Last Dates for Subr 2 31st October, 2022 (for July ie
'5"" April, 2023 (for January Session"
1, Write an algorithm and program that accepts the following list of integers and uses Quick Sort to sort and print them:
15, 18, 24, 12, 89, 34, 99, 3, 105
Ans.
Algorithm:
‘Step:1 Start at index zero, compare the element with the next one (a{0] & all] {ais the name of the array)), and swap if a[0} >
ali)
Step:2 Now compare aft] & a[2] and swap if a[1] > al2]
Step:3 Repeat this process until the end of the array. After doing this, the largest element is present at the end. This whole
thing is known asa pass. n the first pass, we process array elements from {0,n-1].
‘Step:4 Repeat step one but provess array elements [0, n-2] because the last one, ie, a[t-l], is present at its correct position.
Step:5 After this step, the largest two elemients are present at the end.
Step:6 Repeat this process n-1 times.
Program:
#includecstdio.h>
void quicksort{int number{25} ,int first, int last)
{
int i,j, pivot, temp;
itlfirstslast)
{
pivot=first;
iefirst;
srlast;
whiletic))
{
while(numberlilk-numberlpivot}&&iclast)
iets
Ignou Study Helper-Sunil Poonia Page 1E43 Tonou Stvoy HeLpee
wwnw.ignousite.com SUNIL PooMlA
itch)
{
temp-number(i);
number[i]=number{j);
rumberljJ-temp;
} fee
} od
tempsnumberfpivot];
numberfpivot}=numberlj}:
numberfj}=temp;
‘quicksort( number first);
quicksort(nurmber,j+1 last);
}
}
int main()
{
int count, number(25};
printf("How many enter the elements? :");
scanf{"éd" &count];
printf("Enter 9d elements: ", count);
for(i
scanf("%éd",&number(i};
\iecountji++)
quicksort(number,0,count-1);
printf(" Order of Sorted elements:
forti
‘count;i++)
printf(" %d" numberti);
return 0;
}
Ignou Study Helper-Sunil Poonia Page 2ffs Touou Srvny Heveee
wvaw.ignousite.com Nar? SumL POONA
Output:
rca
Ere eee eZee:
ee eae
iat, eee
Ans.
Algorithm:
‘Step 1: loop around all values of linked list and follow step 28 3.
Step 2: if the value of a node's exponent. is greater copy this node to result node and head tawards the next node.
Step 3: if the values of both node's exponent is same add the coefficients and then copy the added value with node to the
resutt.
Step 4: Print the resultant node.
Program:
Hinclude
using namespace std;
struct Nodet
int coef;
int pow;
struct Node *next;
}
void create_node(int , int y, struct Node **terp)
struct Node *r, *2;
inte
F-{struct Node*}malloc(sizeo!
escoeff
>pow=y;
‘temp
next = (struct Node*)malloc(sizeof{struct Node));
Ignou Study Helper-Sunil Poonia Page 3Touov Stvoy HELPER
SuMIL POONA
wuaw.ignousite.com
r= Ponext;
r-onext = NULL;
Jelse(
r>coeff =x;
pow =i
Fonext = (struct Nade*)malloc(sizeof(struct Node));
r= ronext;
r-onext = NULL;
eu0y,
gf
) iy
void polyada(struct Node *p1, struct Nade *p2, struct Node *result)(
while(plonext && p2->next}{
if(p1->pow > p2->powl{
result->pow = pl->pow;
result >coeff = p1>coeff;
pl=pl>next;
}
else f{p1->pow < p2->p0w){
result->pow = p2->pow;
result->coeff = p2->cveff;
p2=p2->next;
Jelse{
result->pow = pl->pow;
result->coeff = p1>coeff#p2->co
pl=pl->next;
p2=p2->next;
t
result->next = (struct Node *}malloc|sizeof(struct Node));,
Ignou Study Helper-Sunil Poonia Page 4+ Toney Stuy HeLPee
SUNIL POONIA
wvaw.ignousite.com
result = result->next;
result->next = NULL;
t
while(p1->next || p2->next){
(pl next){
result->pow = p1->pow;
result->coeff = pl->coeff;
pl=pl-onext;
t
if(p2->next){
result->pow = p2->pow;
result-scoef
p2=p2->next; rm
}
result->noxt = (struct Node *)malloc|sizeof(struct Node});,
result = result->next;
result >next = NULL;
1
void printpolyistruct Node *node){
while(node->next I= NULL}{
printf(*9%dx"séd", node->coeff, noce->pow),
node = node->next;
if(node->next != NULL)
printi(" +");
}
int main(}{
Ignou Study Helper-Sunil Poonia Page 5‘ee Touov Stuy HELPee
‘www.ignousite.com at SUMIL POONA
struct Node *p1 = NULL, *p2 = NULL, *result = NULL;
create_node(41,7,8p1);
create_node(12,5,8p1);
create_node(65,0,&p1);
create_node(21.5,8p2);
create_node(15,2,8—:
printf(" polynomial
printpoly(p1)
printf("\npolynomial 2: ");
printpoly(e2l;
result = (struct Node *)malloc(sizeof{struct Node));
polyadd(p1, p2, result);
printf("\npalynomial after adding p1 and p2 :*),
printpoly(resuit);
return 0;
cm
ome
[EW aeu cee nt ed ee eae Ee ea
Ignou Study Helper-Sunil Poonia Page 6