zoukankan      html  css  js  c++  java
  • 选择排序

    选择排序:

    基本原理:比如在一个长度为N的无序数组中,在第一趟遍历N个数据,找出其中最小(大)的数值与第一个元素交换,
                      第二趟遍历剩下的N-1个数据,找出其中最小(大)的数值与第二个元素交换......
                      第N-1趟遍历剩下的2个数据,找出其中最小(大)的数值与第N-1个元素交换,至此选择排序完成
    稳定性:不稳定
    时间复杂度:O(n^2)
    代码演示

    public class SimpleSelectSort {
    	public static void main(String[] args) {
    		int[] array = { -2,112,2,4,10,12,-1,65,11,94,1};
    		int temp = 0;
    		int minIndex = 0;
    		for (int i = 0; i < array.length; i++) {
    			minIndex = i;
    
    			for (int j = i + 1; j < array.length; j++) {
    				if (array[j] < array[minIndex]) {
    					minIndex = j;
    				}
    			}
    			if (minIndex != i) {
    				temp = array[minIndex];
    				array[minIndex] = array[i];
    				array[i] = temp;
    			}
    		}
    		for (int i : array) {
    			System.out.print(i + " ");
    		}
    	}
    
    }
    

      

  • 相关阅读:
    java入门了解14
    java入门了解13
    java入门了解12
    java入门了解11
    Hive-安装
    Hive-基本概念
    Java笔记20
    Java-笔记19
    Java-笔记18-复习
    Java-笔记18
  • 原文地址:https://www.cnblogs.com/javabigdata/p/7300413.html
Copyright © 2011-2022 走看看