Java Array Sort sort(int values[])

Here you can find the source of sort(int values[])

Description

Sort an array of integers to place them in increasing order

License

Open Source License

Parameter

Parameter Description
values the array of integers to sort

Declaration

public static void sort(int values[]) 

Method Source Code

//package com.java2s;
/*/*from w ww.j ava 2  s.co m*/
 * File:  arrayUtil.java
 *
 * Copyright (C) 1999, Dennis Mikkelson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 * Contact : Dennis Mikkelson <mikkelsond@uwstout.edu>
 *           Department of Mathematics, Statistics and Computer Science
 *           University of Wisconsin-Stout
 *           Menomonie, WI 54751, USA
 *
 * This work was supported by the Intense Pulsed Neutron Source Division
 * of Argonne National Laboratory, Argonne, IL 60439-4845, USA.
 *
 * For further information, see <http://www.pns.anl.gov/ISAW/>
 *
 * Modified:
 *
 *  $Log$
 *  Revision 1.21  2005/05/27 03:57:29  dennis
 *  Fixed javadoc error.
 *
 *  Revision 1.20  2004/04/02 15:10:26  dennis
 *  Uncommented use of "Comparator" and "SortOnX()" method to sort
 *  floatPoint2D objects on their X components.  This had been commented
 *  out to maintain compatibility with java 1.1*.
 *
 *  Revision 1.19  2004/03/11 23:00:11  rmikk
 *  Added the correct package name to all java files
 *
 *  Revision 1.18  2003/10/16 00:34:42  dennis
 *  Fixed javadocs to build cleanly with jdk 1.4.2
 *
 *  Revision 1.17  2002/11/27 23:23:49  pfpeterson
 *  standardized header
 *
 *  Revision 1.16  2002/04/11 21:17:06  dennis
 *  Fixed documentation for get_index_of() method.
 *  interpolate() method now includes the endpoints in the
 *  special cases that are filtered out.
 *
 *  Revision 1.15  2002/03/18 21:15:49  dennis
 *  The reverse() method now just returns if the array is null.
 *
 */

public class Main {
    /**
     *  Sort an array of integers to place them in increasing order
     *
     *  @param  values  the array of integers to sort
     */
    public static void sort(int values[]) {
        q_sort(values, 0, values.length - 1);
    }

    /**
     *  Do a "quick" sort of a portion of an array of integers.  This 
     *  implementation of the quick sort algorithm uses the middle element 
     *  of a sub-list as the key to split the sub-list.  While the worst 
     *  case behavior is still O(n**2), it should only occur in rare cases.  
     *  The standard quick sort is O(n**2) for ordered lists, which is a very
     *  common occurence.
     *
     *  @param  list    the array holding the list to sort.
     *  @param  start   the index of the start of the list to sort in the array.
     *  @param  end     the index of the end of the list to sort in the array.
     */
    public static void q_sort(int list[], int start, int end) {
        int i = start;
        int j = end;
        int key;

        if (i >= j) // at most one element, so we're
            return; // done with this sublist

        swap(list, start, (i + j) / 2);

        key = list[start];
        while (i < j) {
            while (list[i] <= key && i < end)
                i++;
            while (list[j] > key)
                j--;
            if (i < j)
                swap(list, i, j);
        }
        swap(list, start, j);

        q_sort(list, start, j - 1);
        q_sort(list, j + 1, end);
    }

    /**
     *  Interchange two integers in an array of integers, used by q_sort_ints.
     * 
     *  @param  list  the array holding the elements to interchange.
     *  @param  i     the index of the first element to interchange.
     *  @param  j     the index of the second element to interchange.
     */
    public static void swap(int list[], int i, int j) {
        int temp = list[i];
        list[i] = list[j];
        list[j] = temp;
    }
}

Related

  1. sort(final double[] data)
  2. sort(final int[] data)
  3. sort(final long[] primary, final double[] secondary)
  4. Sort(float cais[])
  5. sort(int v[], int left, int right)
  6. sort(int[] array)
  7. sort(int[] array)
  8. sort(int[] array)
  9. sort(int[] array)