-3

Can someone explain how this code work especially at the if statement. I dont have any idea on how it make the sorting, by comparing random variable and declaring int t and assign to variable num1, then num1 = num2 and after that num2 = t and so on. What does it mean actually, and how it sort the value according to the order?

package Lab4;

import javax.swing.JOptionPane;

public class test2 {
    public static void main(String[] args) {

        String stringNum1 = JOptionPane.showInputDialog(null,
                "Please enter 1st Integer: ");
        String stringNum2 = JOptionPane.showInputDialog(null,
                "Please enter 2nd Integer: ");
        String stringNum3 = JOptionPane.showInputDialog(null,
                "Please enter 3rd Integer: ");
        String stringNum4 = JOptionPane.showInputDialog(null,
                "Please enter 4th Integer: ");
        String stringNum5 = JOptionPane.showInputDialog(null,
                "Please enter 5th Integer: ");

        int num1 = Integer.parseInt(stringNum1);
        int num2 = Integer.parseInt(stringNum2);
        int num3 = Integer.parseInt(stringNum3);
        int num4 = Integer.parseInt(stringNum4);
        int num5 = Integer.parseInt(stringNum5);


        if (num1 > num2) {
            int t = num1;
            num1 = num2;
            num2 = t;
        }
        if (num4 > num5) {
            int t = num4;
            num4 = num5;
            num5 = t;
        }
        if (num1 > num3) {
            int t = num1;
            num1 = num3;
            num3 = t;
        }
        if (num2 > num3) {
            int t = num2;
            num2 = num3;
            num3 = t;
        }
        if (num1 > num4) {
            int t = num1;
            num1 = num4;
            num4 = t;
        }
        if (num3 > num4) {
            int t = num3;
            num3 = num4;
            num4 = t;
        }
        if (num2 > num5) {
            int t = num2;
            num2 = num5;
            num5 = t;
        }
        if (num2 > num3) {
            int t = num2;
            num2 = num3;
            num3 = t;
        }
        if (num4 > num5) {
            int t = num4;
            num4 = num5;
            num5 = t;
        }

        System.out.println("Ascending order  : " + num1 + " " + num2 + " " + num3 + " " + num4 + " " + num5);
        System.out.println("Discending order : " + num5 + " " + num4 + " " + num3 + " " + num2 + " " + num1);
    }
}
  • 5
    Appears to be a [Sorting network](http://en.wikipedia.org/wiki/Sorting_network). – Elliott Frisch Oct 01 '14 at 16:42
  • Also, see http://stackoverflow.com/questions/1534748/design-an-efficient-algorithm-to-sort-5-distinct-keys-in-fewer-than-8-comparison – NPE Oct 01 '14 at 16:43
  • The code above is the worst example of a bubble sort ever. This is a hard coded bubble sort. – DwB Oct 01 '14 at 16:43
  • 1
    It looks like an extremely confusing and difficult-to-maintain custom sorting implementation. Honestly, if sorting an array of values is all that's needed then there are *plenty* of well known algorithms to accomplish that. You're right to be confused by this one, it's not very clear. Each *individual operation* is clear, but the overall algorithm is downright silly. – David Oct 01 '14 at 16:43

1 Answers1

0

It is not comparing random variables, all the variables compared are user input. Let's take it step by step:

"declaring int t and assign to variable num1, then num1 = num2 and after that num2 = t" - that part is swapping the values of num1 and num2. In order to swap the values of two variables, you will need a third one to temporary hold the value. Think of it like this: if you have a red mug with tea and a blue mug with coffee, you would need a third empty mug to swap the liquids in the mugs, right? Put the coffee in the third mug, then your blue mug can store the tea. Once the tea has been displaced to the blue mug, your red mug can be filled with the coffee which is now in the third mug.

"how it make the sorting, by comparing random variable" - the variables themselves are not random, but the way they are picked and compared is a bit random. I would have done it in order. The way to grasp this is to visualize it: write on a piece of paper the variables in alphabetical order and see how swapping their values would sort them from lowest to greatest:

num1..........num2............num3..........num4..........num5

Now look at the first if statement: if num1 is greater than num2, then their values are swapped so the first two elements are in order. The second if statement orders the last two elements. So on until all five elements are sorted.

RockOnRockOut
  • 751
  • 7
  • 18