In Java Convince yourself that the following code fragment rearranges the intege
ID: 3748862 • Letter: I
Question
In Java
Convince yourself that the following code fragment
rearranges the integers stored in the variables A, B, C, and D
so that A <= B <= C <= D.
Optimal oblivious sorting networks.
Create a program that
sorts 4 integers using only 5 if statements,
and one that sorts 5 integers using only 9 if statements of the type above?
and one that sorts 6 integers using only 12 if statements of the type above
if (A > B) { t = A; A = B; B = t; } if (B > C) { t = B; B = C; C = t; } if (A > B) { t = A; A = B; B = t; } if (C > D) { t = C; C = D; D = t; } if (B > C) { t = B; B = C; C = t; } if (A > B) { t = A; A = B; B = t; } if (D > E) { t = D; D = E; E = t; } if (C > D) { t = C; C = D; D = t; } if (B > C) { t = B; B = C; C = t; } if (A > B) { t = A; A = B; B = t; }
Explanation / Answer
Screenshot
Sorts 4 integers using only 5 if statements,
public class IfSorting {
public static void main(String[] args) {
//Variables set for test
int a=5,b=2,c=1,d=6;
//variables for sort
int max1, max2, min1, min2;
//First loop set first and second numbers minimum & maximum
if (a > b)
{
max1 = a;
min1 = b;
}
else
{
max1 = b;
min1 = a;
}
//Second loop set first and second numbers minimum & maximum
if (c > d)
{
max2 = c;
min2 = d;
}
else
{
max2 = d;
min2 = c;
}
//Third loop set maximum numbers
if (max1 > max2)
{
d=max1;
c=max2;
}
else {
d=max2;
c=max1;
}
//Fourth loop set minimum numbers
if (min1 < min2) // 4
{
a=min1;
b=min2;
}
else {
a=min2;
b=min1;
}
//Print sorted result
System.out.println(a+" "+b+" "+c+" "+d);
}
}
Output
1 2 5 6
---------------------------------------
Sorts 5 integers using only 8 if statements
public class IfSorting {
public static void main(String[] args) {
//Variables set for test
int a=5,b=2,c=1,d=6,e=7,t=0;
//variables for sort
int max1, max2, min1, min2;
//First loop set first and second numbers minimum & maximum
if (a > b)
{
max1 = a;
min1 = b;
}
else
{
max1 = b;
min1 = a;
}
//Second loop set first and second numbers minimum & maximum
if (c > d)
{
max2 = c;
min2 = d;
}
else
{
max2 = d;
min2 = c;
}
//Third loop set maximum numbers
if (max1 > max2)
{
d=max1;
c=max2;
}
else {
d=max2;
c=max1;
}
//Fourth loop set minimum numbers
if (min1 < min2) // 4
{
a=min1;
b=min2;
}
else {
a=min2;
b=min1;
}
//fifth
if(a>e) {
t=a;
a=e;
e=t;
}
//Sixth
if(b>e) {
t=b;
b=e;
e=t;
}
//Seventh
if(c>e) {
t=c;
c=e;
e=t;
}
//Eighth
if(d>e) {
t=d;
d=e;
e=t;
}
//Print sorted result
System.out.println(a+" "+b+" "+c+" "+d+" "+e);
}
}
Output
1 2 5 6 7
--------------------------------------------------------
Sort 6 integers using 12 loops
public class IfSorting {
public static void main(String[] args) {
//Variables set for test
int a=5,b=2,c=1,d=6,e=7,f=8,t=0;
//variables for sort
int max1, max2, min1, min2;
//First loop set first and second numbers minimum & maximum
if (a > b)
{
max1 = a;
min1 = b;
}
else
{
max1 = b;
min1 = a;
}
//Second loop set first and second numbers minimum & maximum
if (c > d)
{
max2 = c;
min2 = d;
}
else
{
max2 = d;
min2 = c;
}
//Third loop set maximum numbers
if (max1 > max2)
{
d=max1;
c=max2;
}
else {
d=max2;
c=max1;
}
//Fourth loop set minimum numbers
if (min1 < min2) // 4
{
a=min1;
b=min2;
}
else {
a=min2;
b=min1;
}
//fifth
if(a>e) {
t=a;
a=e;
e=t;
}
//Sixth
if(b>e) {
t=b;
b=e;
e=t;
}
//Seventh
if(c>e) {
t=c;
c=e;
e=t;
}
//Eighth
if(d>e) {
t=d;
d=e;
e=t;
}
//nineth
if(a>f) {
t=a;
a=f;
f=t;
}
//Tenth
if(b>f) {
t=b;
b=f;
f=t;
}
//eleventh
if(c>f) {
t=c;
c=f;
f=t;
}
//Twelth
if(d>f) {
t=d;
d=f;
f=t;
}
//Print sorted result
System.out.println(a+" "+b+" "+c+" "+d+" "+e+" "+f);
}
}
Output
1 2 5 6 7 8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.