---
title: "java选择法排序"
author: "祖祖cool"
date: "2011-12-16T10:20:36Z"
tags: []
categories: ["开源软件"]
slug: "java"
draft: false
lastmod: "2011-12-16T10:20:06Z"
id: 1313579
url: "https://joecn.iteye.com/blog/1313579"
---

public class SelectSortApp
{
    public static void main(String args)
       {
       int maxSize = 100;            // array size
       ArraySel arr;                 // reference to array
       arr = new ArraySel(maxSize);  // create the array

       arr.insert(77);               // insert 10 items
       arr.insert(99);
       arr.insert(44);
       arr.insert(55);
       arr.insert(22);
       arr.insert(88);
       arr.insert(11);
       arr.insert(00);
       arr.insert(66);
       arr.insert(33);

       arr.display();                // display items

       arr.selectionSort();          // selection-sort them

       arr.display();                // display them again
       }  // end main()
    }  // end class SelectSortApp
////////////////////////////////////////////////////////////// //

public class ArraySel
{
    private long a;                 // ref to array a
    private int nElems;               // number of data items
// --------------------------------------------------------------
    public ArraySel(int max)          // constructor
       {
       a = new long;                 // create the array
       nElems = 0;                        // no items yet
       }
// --------------------------------------------------------------
    public void insert(long value)    // put element into array
       {
       a = value;             // insert it
       nElems++;                      // increment size
       }
// --------------------------------------------------------------
    public void display()             // displays array contents
       {
       for(int j=0; j<nElems; j++)       // for each element,
          System.out.print(a + " ");  // display it
       System.out.println("");
       }
// --------------------------------------------------------------
    public void selectionSort()
       {
       int out, in, min;

       for(out=0; out<nElems-1; out++)   // outer loop
          {
          min = out;                     // minimum
          for(in=out+1; in<nElems; in++) // inner loop
             if(a < a )         // if min greater,
                 min = in;               // we have a new min
          swap(out, min);                // swap them
          }  // end for(out)
       }  // end selectionSort()
// --------------------------------------------------------------
    private void swap(int one, int two)
       {
       long temp = a;
       a = a;
       a = temp;
       }
// --------------------------------------------------------------
    }  // end class ArraySel