Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package class170706;
- import java.util.Arrays;
- import java.util.Random;
- public class PersonTester {
- public static void main(String[] args) {
- // create an object for random numbers
- Random rnd = new Random();
- // create a persons array
- Person[] personsArr = new Person[10];
- // create each person in the array with random height and weight
- System.out.println("source data in the array");
- for (int i = 0; i < personsArr.length; i += 1) {
- // create a person with default values
- personsArr[i] = new Person();
- // update each person properties
- personsArr[i].setName("Person-" + i);
- personsArr[i].setHeight(1 + rnd.nextInt(250));
- personsArr[i].setWeight(1 + 150 * rnd.nextDouble());
- System.out.printf("[%d] : %s\n", i, personsArr[i]);
- }
- // sorting array by weight, using insert sort algorithm
- sortByWeight(personsArr);
- System.out.println("\narray sorted by weight:");
- for (int i = 0; i < personsArr.length; i += 1) {
- System.out.printf("[%d] : %s\n", i, personsArr[i]);
- }
- // sorting array by height, using bubble sort algorith
- sortByHeight(personsArr);
- System.out.println("\narray sorted by height:");
- for (int i = 0; i < personsArr.length; i += 1) {
- System.out.printf("[%d] : %s\n", i, personsArr[i]);
- }
- // sorting arra by name, using Arrays.sort method
- sortByName(personsArr);
- System.out.println("\narray sorted by name:");
- for (int i = 0; i < personsArr.length; i += 1) {
- System.out.printf("[%d] : %s\n", i, personsArr[i]);
- }
- }
- // sorting persons array by weight, using insert sort
- public static void sortByWeight(Person[] personsArray) {
- int startIndex = 0; // first element to checked. elements before that
- // are already sorted
- while (startIndex < personsArray.length - 1) {
- // initialize minimum weight and and its index
- int minIndex = startIndex;
- double minWeight = personsArray[startIndex].getWeight();
- // start scanning in the array
- for (int i = startIndex + 1; i < personsArray.length; i += 1) {
- // find the minimum weight and its index
- if (personsArray[i].getWeight() < minWeight) {
- minIndex = i;
- minWeight = personsArray[i].getWeight();
- }
- }
- // put the person with minimum weight at startIndex
- if (minIndex != startIndex) {
- Person tempPerson = personsArray[startIndex];
- personsArray[startIndex] = personsArray[minIndex];
- personsArray[minIndex] = tempPerson;
- }
- // now start searching from the next cell
- startIndex += 1;
- }
- }
- // sorting persons array by height, using bubble sort
- public static void sortByHeight(Person[] personsArray) {
- boolean isOrderd; // true when the array is sorted
- int lastIndex = personsArray.length - 1; // last active index in the
- // array
- // start switching unordered elements
- do {
- isOrderd = true; // initialization
- // scan all active cells in the array (0 to lastIndex)
- for (int i = 0; i < lastIndex; i += 1) {
- // if persons are not orderd
- if (personsArray[i].getHeight() > personsArray[i + 1]
- .getHeight()) {
- // switch them
- Person tempPerson = personsArray[i];
- personsArray[i] = personsArray[i + 1];
- personsArray[i + 1] = tempPerson;
- // mark array as unordered to make another round
- isOrderd = false;
- }
- }
- // last index now containing the right element
- // stop before it next time (mark it as unActive)
- lastIndex -= 1;
- } while (!isOrderd); // loop until the array is ordered
- }
- // sorting persons array by name, using Array.sort method
- public static void sortByName(Person[] personsArray) {
- Arrays.sort(personsArray, (person1, person2) -> person1.getName()
- .compareTo(person2.getName())); // compare two persons by
- // comparing their names
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement