// Note: Java
program is built on NetBeans IDE 8.2. OS Windows 8 or Windows 10.
//
This is a sample of the simple programming in Java for
beginners.
//------------------------------------------------------------------------------------------------------
package sortarrayple;
import java.util.Scanner;
import java.util.Arrays;
class data
{
Scanner input = new Scanner(System.in);
int i;
int j = 5;
int k;
final int num[] = new int[j]; //original values
int BUB[] = new int [j]; //for BUBLhightolow
int BUB2[] = new int [j]; //for BUBLlowtohigh
int n[] = new int[j]; // for sortLOWTOHIGH
int n2[] = new int[j]; //for sortHIGHTOLOW
int ex[] = new int[j]; // for exeHIGHTOLOW
int ex2[] = new int[j]; // for exeLOWTOHIGH
void enterdata() // method to retrieve user data
{
for (i = 0; i < j; i++)
{
System.out.println("ENTER VALUE");
num[i] = input.nextInt();
}
}
void BUBLhightolow()
{
int temp;
for (i = 0; i < j; i++)
{
BUB[i] = num[i];
}
for (i = 1; i < BUB.length; i++)
{
for (k = (BUB.length - 1); k >= i; k--)
{
if (BUB[k-1] < BUB[k]) //compares the the value of the 2 most adjacent loction and finds the highest.
{
temp = BUB[k-1];
BUB[k-1] = BUB[k];
BUB[k] = temp;
}
}
}
}
void BUBLlowtohigh()
{
int temp2;
for (i = 0; i < j; i++)
{
BUB2[i] = num[i];
}
for (i = 1; i < BUB2.length; i++)
{
for (k = (BUB2.length - 1); k >= i; k--)
{
if (BUB2[k-1] > BUB2[k]) // compares the 2 most adjacent values to find the lowest
{
temp2 = BUB2[k-1];
BUB2[k-1] = BUB2[k];
BUB2[k] = temp2;
}
}
}
}
void sortLOWTOHIGH()
{
for (i = 0; i < j; i++)
{
n[i] = num[i];
}
Arrays.sort(n); //java util allows finds low to high
}
void sortHIGHTOLOW()
{
for (i = 0; i < j; i++)
{
n2[i] = num[i]*(-1); //if all values are multiplied by -1 then the values are reversed... the highest becomes lowest
Arrays.sort(n2);
}
for (i = 0; i < j; i++)
{
n2[i] = n2[i]*(-1); // after sorting -1 is multiplied again so the highest will be the highest again
}
}
void exeHIGHTOLOW()
{
int temp3;
for (i = 0; i < j; i++)
{
ex[i] = num[i];
}
for (i = 0; i < ex.length-1; i++)
{
for (k = (i +1); k < ex.length; k++) // compares the the first value in the for loop to the rest of the values
{
if (ex[i] < ex[k]) //returns the highest then the lowest
{
temp3 = ex[i];
ex[i] = ex[k];
ex[k] = temp3;
}
}
}
}
void exeLOWTOHIGH()
{
int temp4;
for (i = 0; i < j; i++)
{
ex2[i] = num[i];
}
for (i = 0; i < ex.length-1; i++)
{
for (k = (i +1); k < ex.length; k++)// compares the the first value in the for loop to the rest of the values
{
if (ex2[i] > ex2[k])//returns the lowest then the highest
{
temp4 = ex2[i];
ex2[i] = ex2[k];
ex2[k] = temp4;
}
}
}
}
void display(int tst, data b)
{
for (i = 0; i < j; i++)
{
switch (tst)
{
case 0:
System.out.print(b.num[i] + " ");
break;
case 1:
System.out.print(b.BUB[i] + " ");
break;
case 2:
System.out.print(b.BUB2[i] + " ");
break;
case 3:
System.out.print(b.n2[i] + " ");
break;
case 4:
System.out.print(b.n[i] + " ");
break;
case 5:
System.out.print(b.ex[i] + " ");
break;
case 6:
System.out.print(b.ex2[i] + " ");
break;
}
}
System.out.println();
}
}
public class SortArrayple
{
public static void main(String[] args)
{
int i;
data a = new data();
a.enterdata();
a.BUBLhightolow();
a.BUBLlowtohigh();
a.sortLOWTOHIGH();
a.sortHIGHTOLOW();
a.exeHIGHTOLOW();
a.exeLOWTOHIGH();
System.out.println();
System.out.print("ORIGINAL ORDER: ");
// for (i = 0; i < 5; i++)
// {
// System.out.print(a.num[i] + " ");
// }
// System.out.println();
a.display(0, a);
System.out.print("BUBBLE SORT (HItoLO): ");
// for (i = 0; i < 5; i++)
// {
// System.out.print(a.BUB[i] + " ");
// }
// System.out.println();
a.display(1, a);
System.out.print("BUBBLE SORT (LOtoHI): ");
// for (i = 0; i < 5; i++)
// {
// System.out.print(a.BUB2[i] + " ");
// }
// System.out.println();
a.display(2, a);
System.out.print("UTIL ASSORT (HItoLO): ");
// for (i = 0; i < 5; i++)
// {
// System.out.print(a.n2[i] + " ");
// }
// System.out.println();
a.display(3, a);
System.out.print("UTIL ASSORT (LOtoHI): ");
// for (i = 0; i < 5; i++)
// {
// System.out.print(a.n[i] + " ");
// }
// System.out.println();
a.display(4, a);
System.out.print("EXCHANGE SORT (HItoLO): ");
// for (i = 0; i < 5; i++)
// {
// System.out.print(a.ex[i] + " ");
// }
// System.out.println();
a.display(5, a);
System.out.print("EXCHANGE SORT (LOtoHI): ");
// for (i = 0; i < 5; i++)
// {
// System.out.print(a.ex2[i] + " ");
// }
a.display(6, a);
}
}
No comments:
Post a Comment