0

How Do I make it So My highest number simply moves to the first position. This is what I have So far

            public static void main(String[] args) {
            //array of 10 numbers
            int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};

            //assign first element of an array to largest and smallest
            int smallest = numbers[0];
            int largetst = numbers[0];

            for(int i=1; i< numbers.length; i++)
            {
                    if(numbers[i] > largetst)
                            largetst = numbers[i];}

             System.out.println("Numbers : " + numbers);
             }
             }
2
  • Do you only want to have the highest number at the beginning or sort the entire array? (Good sorting algorithms are e.g. quicksort, video - another one which might be easier to understand is bubble.. ) Commented Mar 23, 2015 at 4:47
  • @skofgar at the beginning Commented Mar 23, 2015 at 4:53

3 Answers 3

1
public static void main(String[] args) {
    //array of 10 numbers
    int numbers[] = new int[] { 32, 43, 53, 54, 32, 65, 63, 98, 43, 23 };

    //assign first element of an array to largest and smallest
    int tmp = 0;
    int largetst = numbers[0];


    for (int i = 1; i < numbers.length; i++) {
        if (numbers[i] > largetst){
            largetst = numbers[i];
        }
    }

    for(int i = 1; i < numbers.length; i++){
        if(largetst == numbers[i]){
            tmp = numbers[0];
            numbers[0] =  largetst;
            numbers[i] = tmp;
        }
    }

    System.out.println("Numbers : " + numbers[0]);
}

Though its not optimal it works please use this approach and further optimize it thanks

Sign up to request clarification or add additional context in comments.

2 Comments

I would avoiding looping twice through the array. It's still quick, but you risk to have a worst-case run-time complexity of 2n instead of just n to find the largest variable. (It's not bad, but we can avoid this) - alternatively you could save in your first loop, the position of the largest number and then swap without another loop
Ya this is the result of quick answer as I mentioned in post It should be lot optimized, posted to give OP some idea.
0

What you could do to get the biggest number to the beginning is: Every time you find a bigger number, swap them with the first one.

public static void main(String[] args) {
        //array of 10 numbers
        int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};

        // temporary variable for swapping numbers
        int tmp;

        for(int i=1; i < numbers.length; i++)
        {
                // check if i-th number bigger than first one
                if(numbers[i] > numbers[0]) { 
                        // if yes - swap!
                        tmp = numbers[0];
                        numbers[0] = numbers[i];
                        numbers[i] = tmp;
                }
         }

         System.out.println("Largest number : " + numbers[0]);
 }

Alternatively - instead of swapping them - you could just store the position of the largest number and then just swap them after the loop. (Saves some memory and performance)

Comments

0

See Demo

If I understand your question correctly then this might be the answer :

**

You only wanted the highest number in the front position of the array. Got no more information

**

public static void main(String[] args) {
        //array of 10 numbers
        int numbers[] = new int[]{32, 43, 53, 54, 32, 65, 663, 98, 43, 23};

        //assign first element of an array to largest and smallest
        int smallest = numbers[0];
        int largest = numbers[0];
        //to keep track of the largest number's index
        int indexOfLargestNumber = 0;

        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > largest) {
                indexOfLargestNumber = i;
                largest = numbers[i];
            }
        }

        // swap the position of the front number and the highest number
        int temp = numbers[0];
        numbers[0] = largest;
        numbers[indexOfLargestNumber] = temp;

        //printing the whole array
        for(int i = 0;i<numbers.length;i++){
            System.out.print(numbers[i]+"  ");
        }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.