- Which of the following code is better in terms of time and space complexities Code #1 Code #2 int ary[] (5,8,3,6,2,9,8,5,8,3}; int ary[] (5,8,3,6,2,9,8,5,8,3); int x; int x; int y-0; for (int i-0; i<= ary.length-2; i++) ( for (int j=ary.length-1; j>=1; j--){ if (ary(j-1)>ary[j] ){ for (int i-0; i<= ary.length-2; i++) y=i; for (int j-i+1; j<=ary. length-1: j++ if (ary[y)>ary[al)( x-ary[j); ary(j) = ary(-1]; ary(j-1] = x; xary(1]: ary[i] ary[yl: aryly) - x:
- Which of the following code is better in terms of time and space complexities Code #1 Code #2 int ary[] (5,8,3,6,2,9,8,5,8,3}; int ary[] (5,8,3,6,2,9,8,5,8,3); int x; int x; int y-0; for (int i-0; i<= ary.length-2; i++) ( for (int j=ary.length-1; j>=1; j--){ if (ary(j-1)>ary[j] ){ for (int i-0; i<= ary.length-2; i++) y=i; for (int j-i+1; j<=ary. length-1: j++ if (ary[y)>ary[al)( x-ary[j); ary(j) = ary(-1]; ary(j-1] = x; xary(1]: ary[i] ary[yl: aryly) - x:
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 8SA
Related questions
Question
100%
Give brief explanation on why code 2 is better than code 1
![- Which of the following code is better in terms of time and space
complexities
Code #1
Code #2
int ary[] -(5,8,3,6,2,9,8,5,8,3};
int ary[] = (5,8,3,6,2,9,8,5,8,3);
int x;
int x;
int y-0;
for (int i-0; i<= ary. length-2; i++) (
for (int j=ary.length-1; j>=1; j--){
if (ary[j-1]>ary(j))(
for (int i-0; i<= ary. length-2: i++) (
y=i;
for (int j-i+1; j<=ary.length-1: j++)(
if (ary[y)>ary[jl) (
x=ary[j];
ary(]
ary[j-1];
ary[j-1] = x;
x-ary[i]:
ary[i] - ary[y):
ary(y) - X:](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fb677c51a-beba-4976-9618-5fa4e8dc630a%2F75e62353-9520-465d-9a7d-4c224ca55ca2%2Fwbd8xpl_processed.jpeg&w=3840&q=75)
Transcribed Image Text:- Which of the following code is better in terms of time and space
complexities
Code #1
Code #2
int ary[] -(5,8,3,6,2,9,8,5,8,3};
int ary[] = (5,8,3,6,2,9,8,5,8,3);
int x;
int x;
int y-0;
for (int i-0; i<= ary. length-2; i++) (
for (int j=ary.length-1; j>=1; j--){
if (ary[j-1]>ary(j))(
for (int i-0; i<= ary. length-2: i++) (
y=i;
for (int j-i+1; j<=ary.length-1: j++)(
if (ary[y)>ary[jl) (
x=ary[j];
ary(]
ary[j-1];
ary[j-1] = x;
x-ary[i]:
ary[i] - ary[y):
ary(y) - X:
Expert Solution
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning