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

6/20/22, 10:34 PM Urgent!! Write A C++ Code Using DSA Concepts With ... | Chegg.

com

  Home Study tools


 My courses
 My books My folder Career Life 

Find solutions for your homework Search

home / study / engineering / computer science / computer science questions and answers / urgent!! write a c++ code using dsa concepts with comme…

Question: Urgent!!
Write a C++ code using DSA concepts with
comments:
… Post a question
Answers from our experts for your tough
homework questions

Urgent!! Enter question


Write a C++ code using DSA concepts with
comments:
a) A kid is playing with his blocks. The blocks contain
different numbers on them
i.e.2,8,3,10,1,7,9,4 . The kid is arranging the blocks in an
ascending order in such a
way that he is using divide and conquer algorithm. Which algorithm
the boy is
Continue to post
using to arrange the elements. Write C++ code for the
algorithm.
18 questions remaining

Expert Answer
My Textbook Solutions
Anonymous answered this
Was this answer helpful? 1 0
75 answers

Since the boy is using divide and conquer algorithm to sort the
blocks in ascending order, we can use
either merge sort or quick
sort.

Since merge sort takes


in the worst case I have used merge sort for the given
problem.
Astronomy Fundament... Fundament...
#include<iostream>
0th Edition
7th Edition 6th Edition
using namespace std;

View all solutions


//Merge function to merge after the recursive call finishes.

void merge(int *A, int p, int q, int r) {

int n1, n2, i, j, k;

n1 = r-p+1; //Size of the left sub array

n2 = q-r; //Size of the right sub array

int L[n1]; //Creating array for left subarray with size n1

int R[n2]; //Creating array for right subarray with size n2

//Coppying elements of left subarray from main array 'A' to the newly created array 'L'

for(int i=0; i<n1; i++) {

L[i] = A[p+i];

//Coppying elements of right subarray from main array 'A' to the newly created array 'R'

for(int i=0; i<n2; i++) {

R[i] = A[r+i+1];

i=0; //For indexing left subarray L

j=0; //For indexing right subarray R

k=p; //For indexing the main Array A

//Merging two subarrays L and R back into main array A

while(i < n1 && j < n2) {

//If the elements of Left subarray is smaller we put it into main Array and increment index of L

//else we put the element from R and increment it's index

if(L[i] <= R[j]) {

A[k] = L[i];

i++;

}else {

A[k] = R[j];

j++;

k++;

//If there are remaining elements in Left subarray L, we just copy it back into the main array

while(i < n1) {

A[k] = L[i];
i++;

k++;

//If there are remaining elements in Right subarray R, we just copy it back into the main array

while(j < n2) {

A[k] = R[j];

j++;

k++;

//Recursive merge sort function

//which takes the array pointer and it's starting index i.e 'p'

//and last index i.e 'q'

void mergesort(int *A, int p, int q) {

int r;

//We continue to call mergesort recursive until,

//the left index is greater than or equal to right index

if(p < q) {

r = (p+q)/2;

mergesort(A, p, r); //Calling mergesort recursively on left subarray

mergesort(A, r+1, q); //Calling mergesort recursively on right subarray

merge(A, p, q, r); //Merging the finally sorted subarrays

int main(void){

//Creating array of elements given

int A[] = {2, 8, 3, 10, 1, 7, 9, 4};

//Finding the size of the array

int size = sizeof A/sizeof(A[0]);

//Calling merge sort on the array A

mergesort(A, 0, size-1);

//Displaying the sorted elements

for(int i=0; i<size; i++) {

cout << A[i] << " ";

cout << endl;

return 0;

Screenshot of the output

Comment


Questions viewed by other students

Q: There is a garage where the access road can accommodate any


number of trucks at one time. The garage is build
such a
way that only the last truck entered can be moved out. Each
of the trucks is identified by a positive integer
(a truck_id).
Write a program to handle truck moves, allowing for
the following commands:
a) On_road (truck_id); b) Enter_garage
(truck_ id);
c) Exit_garage...

A: See answer 100% (2 ratings)

COMPANY LEGAL & POLICIES CHEGG PRODUCTS AND SERVICES CHEGG NETWORK CUSTOMER SERVICE
About Chegg Advertising Choices Cheap Textbooks Chegg Math Solver EasyBib Customer Service
Chegg For Good Cookie Notice Chegg Coupon Mobile Apps Internships.com Give Us Feedback
College Marketing General Policies Chegg Play Sell Textbooks Thinkful Manage Subscription
Corporate Development Intellectual Property Rights Chegg Study Help Solutions Manual
Investor Relations Terms of Use College Textbooks Study 101
Jobs Global Privacy Policy eTextbooks Textbook Rental
Join Our Affiliate Program DO NOT SELL MY INFO Flashcards Used Textbooks
Media Center Honor Code Learn Digital Access Codes
Site Map Honor Shield Uversity Chegg Life
Chegg Writing

© 2003-2022 Chegg Inc. All rights reserved.

https://www.chegg.com/homework-help/questions-and-answers/urgent-write-c-code-using-dsa-concepts-comments-kid-playing-blocks-blocks-contain-differen-q92206550?trackid=797abeeccf86&strackid=7ddab04c4e96 1/1

You might also like