sort int Array Descending - Java java.lang

Java examples for java.lang:int Array

Description

sort int Array Descending

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int[] num = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        sortArrayDescending(num);//w ww  .  j  a  va  2  s. c om
    }

    public static void sortArrayDescending(int[] num) {
        int j;
        boolean flag = true; // set flag to true to begin first pass
        int temp; // holding variable
        while (flag) {
            flag = false; // set flag to false awaiting a possible swap
            for (j = 0; j < num.length - 1; j++) {
                if (num[j] < num[j + 1]) // change to > for ascending sort
                {
                    temp = num[j]; // swap elements
                    num[j] = num[j + 1];
                    num[j + 1] = temp;
                    flag = true; // shows a swap occurred
                }
            }
        }
    }
}

Related Tutorials