bubble sort java achieve

/*

  • The basic idea: compare the size of two numbers, a large number of sink, a small number of take up.
  • Algorithm Description:
  • Compare adjacent elements. If the first is larger than a second, to exchange their two;
  • We do the same work for each pair of adjacent elements, from the beginning to the end of the first to the last pair, so that should be the last element is the largest number;
  • Repeating the above steps for all elements, except the last one;
  • Repeat steps 1 to 3 until the sorting is completed.
  • */

public int[] BubbleSort(int[] args)
    {
        for(int i=0;i<args.length;i++)
        {
            int max;
            for (int j=0;j<args.length-i-1;j++)
            {
                if(args[j]>args[j+1]){
                    max = args[j];
                    args[j] = args[j+1];
                    args[j+1] = max;
                }
            }

        }
        return args;
    }

Guess you like

Origin blog.51cto.com/12013190/2426925