Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Sort Numbers with If Statements (Java)

I know that you can easily sort numbers with an array, but my assignment for class is that I need to sort four numbers in descending order using if statements and not arrays.

Here is my code so far:

package integersort;

import java.util.Scanner;

public class IntegerSort {

public static void main(String[] args) {
    Scanner userInput = new Scanner(System.in);

    int firstNum, secondNum, thirdNum, fourthNum; //inputted numbers

    System.out.println("Enter first number:");
    firstNum = userInput.nextInt();
    System.out.println("Enter second number:");
    secondNum = userInput.nextInt();
    System.out.println("Enter third number:");
    thirdNum = userInput.nextInt();
    System.out.println("Enter fourth number:");
    fourthNum = userInput.nextInt();


    int firstPlace = 0, secondPlace = 0, thirdPlace = 0, fourthPlace = 0;

    //firstPlace start
    if (firstNum > secondNum && firstNum > thirdNum && firstNum > fourthNum) {
        firstPlace = firstNum;
    } else if (secondNum > firstNum && secondNum > thirdNum && secondNum > fourthNum) {
        firstPlace = secondNum;
    } else if (thirdNum > firstNum && thirdNum > secondNum && thirdNum > fourthNum) {
        firstPlace = thirdNum;
    } else if (fourthNum > firstNum && fourthNum > secondNum && fourthNum > thirdNum) {
        firstPlace = fourthNum;
    }
    //firstPlace end

    //fourthPlace start
    if (firstNum < secondNum && firstNum < thirdNum && firstNum < fourthNum) {
        fourthPlace = firstNum;
    } else if (secondNum < firstNum && secondNum < thirdNum && secondNum < fourthNum) {
        fourthPlace = secondNum;
    } else if (thirdNum < firstNum && thirdNum < secondNum && thirdNum < fourthNum) {
        fourthPlace = thirdNum;
    } else if (fourthNum < firstNum && fourthNum < secondNum && fourthNum < thirdNum) {
        fourthPlace = fourthNum;
    }
    //forthPlace end

    //secondPlace start
    if (firstNum != firstPlace && firstNum != fourthPlace && firstNum < firstPlace && firstNum > fourthPlace && firstNum > fourthNum) {
        secondPlace = firstNum;
    } else if (secondNum != firstPlace && secondNum != fourthPlace && secondNum > firstNum && secondNum > thirdNum && secondNum > fourthNum) {
        secondPlace = secondNum;
    } else if (thirdNum != firstPlace && thirdNum != fourthPlace && thirdNum > firstNum && thirdNum > secondNum && thirdNum > fourthNum) {
        secondPlace = thirdNum;
    } else if (fourthNum != firstPlace && fourthNum != fourthPlace && fourthNum > firstNum && fourthNum > secondNum && fourthNum > thirdNum) {
        secondPlace = fourthNum;
    }
    //secondPlace end

    //thirdPlace start
    if (firstNum != firstPlace && firstNum != secondPlace && firstNum != fourthPlace) {
        thirdPlace = firstNum;
    } else if (secondNum != firstPlace && secondNum != secondPlace && secondNum != fourthPlace) {
        thirdPlace = secondNum;
    } else if (thirdNum != firstPlace && thirdNum != secondPlace && thirdNum != fourthPlace) {
        thirdPlace = thirdNum;
    } else if (fourthNum != firstPlace && fourthNum != secondPlace && fourthNum != fourthPlace){
        thirdPlace = fourthNum;
    }
    //thirdPlace end

    System.out.println("The sorted numbers are: "+ firstPlace + " " + secondPlace + " " + thirdPlace + " " + fourthPlace);
    }
}

From this code, I keep getting the numbers in descending order, but there is one digit remaining, as indicated with a zero just so I know something did not go well.

Example I/O: When 40, 52, 6, and 7000 are inputted, 7000, 0, 40, 6 are outputted, when the expected output is 7000, 52, 40, 6.

Not sure what I am doing wrong, but I would like to know so I can get a decent grade on this.

Thank you.

like image 726
Cody Mathews Avatar asked Sep 11 '15 20:09

Cody Mathews


3 Answers

Since a complete answer was already posted, here is another algorithm. By using a sorting network, this can be done with 5 if / swap statements. This is a c code example for descending sort of 4 numbers:

void sortnet4(int a[4])     /* four input sorting network */
{
int t;
    if (a[0] < a[2]) { t = a[0]; a[0] = a[2]; a[2] = t; }
    if (a[1] < a[3]) { t = a[1]; a[1] = a[3]; a[3] = t; }
    if (a[0] < a[1]) { t = a[0]; a[0] = a[1]; a[1] = t; }
    if (a[2] < a[3]) { t = a[2]; a[2] = a[3]; a[3] = t; }
    if (a[1] < a[2]) { t = a[1]; a[1] = a[2]; a[2] = t; }
}

This example shows an ascending order sort of 10 numbers:

void sortnet10(int a[10])   /* ten input sorting network, 29 if/swaps */
{
    int t;
    if (a[0] > a[5]) { t = a[0]; a[0] = a[5]; a[5] = t; }
    if (a[1] > a[6]) { t = a[1]; a[1] = a[6]; a[6] = t; }
    if (a[2] > a[7]) { t = a[2]; a[2] = a[7]; a[7] = t; }
    if (a[3] > a[8]) { t = a[3]; a[3] = a[8]; a[8] = t; }
    if (a[4] > a[9]) { t = a[4]; a[4] = a[9]; a[9] = t; }
    if (a[0] > a[3]) { t = a[0]; a[0] = a[3]; a[3] = t; }
    if (a[5] > a[8]) { t = a[5]; a[5] = a[8]; a[8] = t; }
    if (a[1] > a[4]) { t = a[1]; a[1] = a[4]; a[4] = t; }
    if (a[6] > a[9]) { t = a[6]; a[6] = a[9]; a[9] = t; }
    if (a[0] > a[2]) { t = a[0]; a[0] = a[2]; a[2] = t; }
    if (a[3] > a[6]) { t = a[3]; a[3] = a[6]; a[6] = t; }
    if (a[7] > a[9]) { t = a[7]; a[7] = a[9]; a[9] = t; }
    if (a[0] > a[1]) { t = a[0]; a[0] = a[1]; a[1] = t; }
    if (a[2] > a[4]) { t = a[2]; a[2] = a[4]; a[4] = t; }
    if (a[5] > a[7]) { t = a[5]; a[5] = a[7]; a[7] = t; }
    if (a[8] > a[9]) { t = a[8]; a[8] = a[9]; a[9] = t; }
    if (a[1] > a[2]) { t = a[1]; a[1] = a[2]; a[2] = t; }
    if (a[3] > a[5]) { t = a[3]; a[3] = a[5]; a[5] = t; }
    if (a[4] > a[6]) { t = a[4]; a[4] = a[6]; a[6] = t; }
    if (a[7] > a[8]) { t = a[7]; a[7] = a[8]; a[8] = t; }
    if (a[1] > a[3]) { t = a[1]; a[1] = a[3]; a[3] = t; }
    if (a[4] > a[7]) { t = a[4]; a[4] = a[7]; a[7] = t; }
    if (a[2] > a[5]) { t = a[2]; a[2] = a[5]; a[5] = t; }
    if (a[6] > a[8]) { t = a[6]; a[6] = a[8]; a[8] = t; }
    if (a[2] > a[3]) { t = a[2]; a[2] = a[3]; a[3] = t; }
    if (a[4] > a[5]) { t = a[4]; a[4] = a[5]; a[5] = t; }
    if (a[6] > a[7]) { t = a[6]; a[6] = a[7]; a[7] = t; }
    if (a[3] > a[4]) { t = a[3]; a[3] = a[4]; a[4] = t; }
    if (a[5] > a[6]) { t = a[5]; a[5] = a[6]; a[6] = t; }
}
like image 73
rcgldr Avatar answered Oct 24 '22 01:10

rcgldr


This is one way of discovering second place:

int storeFirstNum = 0;
int storeSecondNum = 0;
int storeThirdNum = 0;
int storeFourthNum = 0;

// secondPlace start
if (firstNum != firstPlace && firstNum != fourthPlace) {
    storeFirstNum = firstNum;
}
if (secondNum != firstPlace && secondNum != fourthPlace) {
    storeSecondNum = secondNum;
}
if (thirdNum != firstPlace && thirdNum != fourthPlace) {
    storeThirdNum = thirdNum;
}
if (fourthNum != firstPlace && fourthNum != fourthPlace) {
    storeFourthNum = fourthNum;
}
if (storeFirstNum > storeSecondNum && storeFirstNum > storeThirdNum
        && storeFirstNum > storeFourthNum) {
    secondPlace = storeFirstNum;
} else if (storeSecondNum > storeFirstNum
        && storeSecondNum > storeThirdNum
        && storeSecondNum > storeFourthNum) {
    secondPlace = storeSecondNum;
} else if (storeThirdNum > storeFirstNum
        && storeThirdNum > storeSecondNum
        && storeThirdNum > storeFourthNum) {
    secondPlace = storeThirdNum;
} else if (storeFourthNum > storeFirstNum
        && storeFourthNum > storeSecondNum
        && storeFourthNum > storeThirdNum) {
    secondPlace = storeFourthNum;
}
// secondPlace end

Screenshot of outcome:

enter image description here

like image 1
smoggers Avatar answered Oct 24 '22 00:10

smoggers


Your program seems to find the first and last places properly. However, its logic breaks at the part where it needs to find the second place.

//secondPlace start
if (firstNum != firstPlace && firstNum != fourthPlace && firstNum < firstPlace && firstNum > fourthPlace && firstNum > fourthNum) {
    secondPlace = firstNum;
} else if (secondNum != firstPlace && secondNum != fourthPlace && secondNum > firstNum && secondNum > thirdNum && secondNum > fourthNum) {
    secondPlace = secondNum;
} else if (thirdNum != firstPlace && thirdNum != fourthPlace && thirdNum > firstNum && thirdNum > secondNum && thirdNum > fourthNum) {
    secondPlace = thirdNum;
} else if (fourthNum != firstPlace && fourthNum != fourthPlace && fourthNum > firstNum && fourthNum > secondNum && fourthNum > thirdNum) {
    secondPlace = fourthNum;
}
//secondPlace end

Now let's look at an example where the input is 52, 40, 6, 7000.

So we expect the number that goes to the second place to be 52, the firstNum.

  • Is firstNum != firstPlace? Yes, it is different than 7000, true.
  • Is firstNum != fourthPlace? Yes, it is different than 6, true.
  • Is firstNum < firstPlace? Yes, it is smaller than 7000, true.
  • Is firstNum > fourthPlace? Yes, it is greater than 6, true.
  • Is firstNum > fourthNum? No, fourthNum is 7000, and 52 is not greater than that. So we get false here.

Thus, firstNum is not selected as the second place, and your program breaks.

The condition for secondNum is also broken, in a slightly different way.

Note also that some of the conditions are redundant. If it is true that firstNum < firstPlace, it is also true that firstNum != firstPlace.

So you can try to fix your conditions' logic, or you can try a different algorithm. For example:

  • Prepare only four variables, and one extra temporary variable for swapping. (You swap x and y by doing temp = x; x = y; y = temp).
  • Compare the second, third, and fourth variables each to the first. If any of them is greater than the first, swap them with it. This will guarantee that the first is the greatest (do you understand why?)
  • Compare the third and fourth to the second, the same way. Now it's guaranteed that the second is the greatest of the remaining numbers.
  • Compare the third and fourth to each other.
like image 1
RealSkeptic Avatar answered Oct 24 '22 01:10

RealSkeptic