My java program doesn't set up the array value in encreasing order


Hi guys! I need a hand.

This program is supposed to serve in output the inserted value into the array vet in a increasing order.
The integer variable n indicate the number of elements which the array is made of.

Unfortunately the program provides the value of the array in the same order they were inserted, I don't know why. Could you please help me?


* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.

package primo_programma;
import java.util.Scanner;
import static java.lang.System.out;

* @author Salvatore Serra
public class Primo_Programma {

 * @param args the command line arguments
public static void main(String[] args) {
    // TODO code application logic here
    int n, temp, i;
    boolean flag; 
    int vet[]; 
    System.out.println("Inserire quanti valori si vuole ordinare in ordine crescennte");
    Scanner keyboard = new Scanner(;
    n = keyboard.nextInt();  
    vet = new int [n];
    for (i=0; i<n; i++){
        System.out.println("Inserire i valori");
        vet[i] = keyboard.nextInt();
    flag = false;
    while (flag=!true) {
        for (i=0; i<n; i++){
        if (vet[i]>vet[i++]){ 
            temp = vet[i];
            vet[i] = vet[i++];
            flag = false;} 
    for (i=0; i<n; i++){


Here you can find the output if I chose the array has to be just of 3 elements (n=3), and insert the following elements: 3, 2, 1.


You should be aware of that bubble sort is an awful sorting algorithm that should be avoided, perhaps even for educational purposes. (preferably use built-in sort, or if you are looking to implement your own sorting algorithm and you're comfortable with recursion, quick sort/merge sort (very similar). If you're not comfortable with recursion, honestly I'd suggest just saving sorting for later, bubblesort is borderline harmful to learn.. well, at least keep this firmly in mind! Don't actually use this algorithm.)

You're not doing anything with the value you stow away in temp and you also aren't moving any values because postfix ++ returns the original value, not the incremented one.

BTW, you might find it advantageous to always keep your indentation levels matched to your curly braces. You're even in an IDE, there's probably some tool to re-format everything automatically. Use it, and/or try to get it right as you type.