• JAVA数据结构选择排序


    可以用JAVA进行选择排序:

     1 // selectSort.java
     2 // demonstrates selection sort
     3 // to run this program: C>java SelectSortApp
     4 ////////////////////////////////////////////////////////////////
     5 package SelectSortApp;
     6 
     7 class ArraySel
     8    {
     9    private long[] a;                 // ref to array a
    10    private int nElems;               // number of data items
    11 //--------------------------------------------------------------
    12    public ArraySel(int max)          // constructor
    13       {
    14       a = new long[max];                 // create the array
    15       nElems = 0;                        // no items yet
    16       }
    17 //--------------------------------------------------------------
    18    public void insert(long value)    // put element into array
    19       {
    20       a[nElems] = value;             // insert it
    21       nElems++;                      // increment size
    22       }
    23 //--------------------------------------------------------------
    24    public void display()             // displays array contents
    25       {
    26       for(int j=0; j<nElems; j++)       // for each element,
    27          System.out.print(a[j] + " ");  // display it
    28       System.out.println("");
    29       }
    30 //--------------------------------------------------------------
    31    public void selectionSort()
    32       {
    33       int out, in, min;
    34 
    35       for(out=0; out<nElems-1; out++)   // outer loop
    36          {
    37          min = out;                     // minimum
    38          for(in=out+1; in<nElems; in++) // inner loop
    39             if(a[in] < a[min] )         // if min greater,
    40                 min = in;               // we have a new min
    41          swap(out, min);                // swap them
    42          }  // end for(out)
    43       }  // end selectionSort()
    44 //--------------------------------------------------------------
    45    private void swap(int one, int two)
    46       {
    47       long temp = a[one];
    48       a[one] = a[two];
    49       a[two] = temp;
    50       }
    51 //--------------------------------------------------------------
    52    }  // end class ArraySel
    53 ////////////////////////////////////////////////////////////////
    54 public class SelectSort
    55    {
    56    public static void main(String[] args)
    57       {
    58       int maxSize = 100;            // array size
    59       ArraySel arr;                 // reference to array
    60       arr = new ArraySel(maxSize);  // create the array
    61 
    62       arr.insert(77);               // insert 10 items
    63       arr.insert(99);
    64       arr.insert(44);
    65       arr.insert(55);
    66       arr.insert(22);
    67       arr.insert(88);
    68       arr.insert(11);
    69       arr.insert(00);
    70       arr.insert(66);
    71       arr.insert(33);
    72 
    73       arr.display();                // display items
    74 
    75       arr.selectionSort();          // selection-sort them
    76 
    77       arr.display();                // display them again
    78       }  // end main()
    79    }  // end class SelectSortApp
    80 ////////////////////////////////////////////////////////////////
  • 相关阅读:
    使用插件和不使用插件实现select的框
    利用sshtunnel实现跳板机的效果[嵌套ssh实现]
    laravel中get()与 first()区别、collection与stdClass的区别
    Laravel 安全:避免 SQL 注入
    MAC 终端走代理服务器
    Python--Virtualenv简明教程(转载https://www.jianshu.com/p/08c657bd34f1)
    Charles 如何抓取https数据包
    爬虫出现Forbidden by robots.txt(转载 https://blog.csdn.net/zzk1995/article/details/51628205)
    Scrapy框架的学习(6.item介绍以及items的使用(提前定义好字段名))转载https://blog.csdn.net/wei18791957243/article/details/86259688
    python xpath
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2485068.html
Copyright © 2020-2023  润新知