• 简单选择排序SelectSort


    package com.cc.dataconsttuct.com;

    /***
    * 简单选择排序:令A[0,n-1]有n个数据元素的数组,将数组A排列为一个非降序的有序数组;
    * 算法:首先在n个元素中找到最小元素,将其放在A[0]中,然后将剩下的n-1个元素中找到最小的放在A[1]中,这个过程不断进行下去。。。。
    * @author caocx
    *
    */
    public class SelectSort {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] a = {12,23,9,24,15,3,18};
    //排序前输出
    for (int i = 0; i < a.length; i++) {
    if(i == a.length-1){
    System.out.println(a[i]);
    }
    else{
    System.out.print(a[i]+" ");
    }

    }
    //调用方法
    SelectSortMain(a);
    //排序后输出
    System.out.println("选择排序后的数据");
    for (int i = 0; i < a.length; i++) {
    if(i == a.length-1){
    System.out.println(a[i]);
    }
    else{
    System.out.print(a[i]+" ");
    }
    }
    }

    //函数方法
    private static void SelectSortMain(int[] arr) {
    int temp;
    //eg:就比如n个数,前n-1个数都可以当做min 依次与后面的数进行比较
    for (int j = 0; j < arr.length-1; j++) {
    int min = j;
    //代表第j+1个数的各个数,拿第j个数与j+1 到arr的最后一个数进行对比
    //j是最小的数,依次与后面的进行比较,如果后面的数比最小数还要小 就对换位置
    for (int k = j+1; k < arr.length; k++)
    if(arr[k]<arr[min])
    min = k;
    //j不是最小的数,把最小的数值对换到j的位置
    if(j!=min){
    temp = arr[j];
    arr[j] = arr[min];
    arr[min] = temp;
    }
    }
    }
    }

  • 相关阅读:
    hdu4122
    poj3410单调队列(单调栈)
    hdu3415 单调队列模板题
    网站指纹识别工具Whatweb的使用
    Python中shodan模块的使用
    Shodan的使用
    Google Hacking的用法
    PyCharm调试程序
    Python脚本与Metasploit交互进行自动永恒之蓝攻击
    关于PHP动态的接收传递的GET,POST和COOKIE变量
  • 原文地址:https://www.cnblogs.com/caocx/p/7609783.html
Copyright © 2020-2023  润新知