Selection Sort in Java

Code :
import java.io.*;

class select

{
public static void main(String args[])throws IOException
{
int[] array = new int[100];
int max = 9;
int low , temp;
int a = 0;

try
{
BufferedReader keyb = new BufferedReader (new
InputStreamReader(System.in));
System.out.print("Enter max number to be sort:");
max = Integer.parseInt(keyb.readLine());
for (int x = 0 ; x < max ; x++)
{
System.out.print("Enter number:");
array[x] = Integer.parseInt(keyb.readLine());
}
for (int m = 0 ; m < max ; m++)
{
System.out.print(array[m] + " ");
}
System.out.print(" ");
//for (int a = max - 1; a > 0; a--)

{

for( int b = 0 ; b < max ; b++)
{
low = array[b];
for (int c = b ; c < max ; c++)
{
if (array[c] < low)
{
low = array[c]; //
System.out.print("<" + b + "><" + c + "><" + low + ">

");
}
else
{
// System.out.print("<" + b + "><" + c + "><" + low + ">");
}


for (int d = b ; d <>
{
if (array[d] == low)
{
temp = array[b];
array[b] = array[d];
array[d] = temp;
//System.out.print("*" + c + "*");
}
}
}

System.out.print("
");
for (int m = 0 ; m <>
{

System.out.print(array[m] + " ");
}

System.out.print("
");


}




}



}
catch (IOException e)
{
System.out.println("System Error");
}
catch (NumberFormatException er)
{
System.out.println("Kindly enter a number");
}

}
}



How to sort an array in java

import java.util.Arrays;


public class Main
{


public void sortIntArray()
{

int[] arrayToSort = new int[] {48, 5, 89, 80, 81, 23, 45, 16, 2};

Arrays.sort(arrayToSort);

for (int i = 0; i <>
System.out.println(arrayToSort[i]);
}


public void sortStringArray() {

String[] arrayToSort = new String[] {"Oscar", "Charlie", "Ryan", "Adam", "David"};

Arrays.sort(arrayToSort);

for (int i = 0; i <>
System.out.println(arrayToSort[i]);
}


public static void main(String[] args)
{

Main main = new Main();
main.sortIntArray();
main.sortStringArray();
}
}

Using the Comparable interface to compare and sort objects

import java.util.Arrays;


public class Main {


public void comparableExample() {

//Creating the objects that implements the Comparable interface
Car car1 = new Car("Toyota", 2006, 5000);
Car car2 = new Car("BMW", 2007, 5000);
Car car3 = new Car("Chrysler", 2007, 4000);

//Comparing the objects by calling the compareTo method on one of them
//passing another object as argument.
System.out.println("Car 1 equals Car 2: " + car1.compareTo(car2));
System.out.println("Car 1 equals Car 3: " + car1.compareTo(car3));
System.out.println("Car 2 equals Car 3: " + car2.compareTo(car3));
System.out.println();

//To sort them we create an array which is passed to the Arrays.sort()
//method.
Car[] carArray = new Car[] {car1, car2, car3};
Arrays.sort(carArray);

//Print out the sorted array
for (Car car : carArray)
System.out.println(car.toString());
}

//The Car class used to compare and sort objects.
class Car implements Comparable
{

private String make;
private int year;
private int mileage;

public Car(String make, int year, int mileage)
{

this.make = make;
this.year = year;
this.mileage = mileage;
}

//Mandatory method when implementing the
//Comparable interface. In this method we
//compare the mileage of the two car objects.
public int compareTo(Object obj)
{

if (obj instanceof Car)
{

Car car = (Car) obj;
if (this.mileage > car.getMileage())
return 1;
else if (this.mileage < car.getMileage())
return -1;
}
return 0;
}

public void setMake(String make)
{
this.make = make;
}

public void setYear(int year)
{
this.year = year;
}

public void setMileage(int mileage)
{
this.mileage = mileage;
}

public String getMake()
{
return make;
}

public int getYear()
{
return year;
}

public int getMileage()
{
return mileage;
}

public String toString()
{

StringBuffer buffer = new StringBuffer();
buffer.append("Make: " + make + "\n");
buffer.append("Year: " + year + "\n");
buffer.append("Mileage: " + mileage + "\n");

return buffer.toString();
}
}


public static void main(String[] args)
{
new Main().comparableExample();
}
}

Program to copy an array

public class Main
{


public void copyArrayExample()
{


int[] intArray = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

int[] arrayCopy = new int[intArray.length];

System.arraycopy(intArray, 0, arrayCopy, 0, intArray.length);

for (int i = 0; i <>
System.out.println(arrayCopy[i]);

}


public static void main(String[] args)
{

new Main().copyArrayExample();
}
}

Program to Extend the size of an array

public class Main
{

/**
* Extends the size of an array.
*/
public void extendArraySize()
{


String[] names = new String[] {"Joe", "Bill", "Mary"};

//Create the extended array
String[] extended = new String[5];

//Add more names to the extended array
extended[3] = "Carl";
extended[4] = "Jane";

//Copy contents from the first names array to the extended array
System.arraycopy(names, 0, extended, 0, names.length);

//Ouput contents of the extended array
for (String str : extended)
System.out.println(str);

}
public static void main(String[] args)
{
new Main().extendArraySize();
}
}

Extend the size of an array

public class Main
{

public void extendArraySize()
{


String[] names = new String[] {"Joe", "Bill", "Mary"};

//Create the extended array
String[] extended = new String[5];

//Add more names to the extended array
extended[3] = "Carl";
extended[4] = "Jane";

//Copy contents from the first names array to the extended array
System.arraycopy(names, 0, extended, 0, names.length);

//Ouput contents of the extended array
for (String str : extended)
System.out.println(str);

}
public static void main(String[] args)
{
new Main().extendArraySize();
}
}

We Are Founder..