zoukankan      html  css  js  c++  java
  • 排序03-简单排序法

    简单排序法:用i位置的数据,与n-i+1中所有的数据进行比较,获取最小的记录,并进行交换;

    时间复杂度:O(n^2)

    特点:数据交换,移动次数最少;

    测试代码:

    public class SimpleSort {
    
        public static void main(String[] args) {
    //        int[] arr = {2,8,5,7,3,1,10,11,6};
            int[] arr = {2,3,5,7,8,1,10,11,6};
            System.out.println(Arrays.toString(arr));
            simpleSort(arr);
    
        }
        //通用,用于数据交换
        public static void swap(int[] arr , int i ,int j){
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    
        public static void simpleSort(int[] arr){
    
            int i,j,min;
            for(i = 0 ; i< arr.length ; i++){
              min = i ;
              for(j = i+1 ; j<arr.length ; j++){
                if( arr[min] > arr[j] ){
                    min = j;
                }
              }
              if(min != i){
                swap(arr,i,min);
              }
                System.out.println(Arrays.toString(arr));
            }
        }

     实现过程:

  • 相关阅读:
    Design Tutorial: Inverse the Problem
    The Number Off of FFF
    "Money, Money, Money"
    No Pain No Game
    Group
    Vases and Flowers
    Codeforces Round #466 (Div. 2)
    ST表
    Wildcard Matching
    HDOJ 3549 Dinitz
  • 原文地址:https://www.cnblogs.com/perferect/p/12303708.html
Copyright © 2011-2022 走看看