`
sunwinner
  • 浏览: 197984 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

基础数据结构和算法二:Selection sort

阅读更多

 

One of the simplest sorting algorithms works as follows: First, find the smallest item in the array and exchange it with the first entry (itself if the first entry is already the smallest). Then, find the next smallest item and exchange it with the sec- ond entry. Continue in this way until the entire array is sorted. This method is called selection sort because it works by repeatedly selecting the smallest remaining item.

 

Time complexity: Selection sort uses ~N^2 / 2 compares and N exchanges to sort an array of length N.

public class Selection {

    // selection sort
    public static void sort(Comparable[] a) {
        int N = a.length;
        for (int i = 0; i < N; i++) {
            int min = i;
            for (int j = i+1; j < N; j++) {
                if (less(a[j], a[min])) min = j;
            }
            exch(a, i, min);
            assert isSorted(a, 0, i);
        }
        assert isSorted(a);
    }

    // use a custom order and Comparator interface - see Section 3.5
    public static void sort(Object[] a, Comparator c) {
        int N = a.length;
        for (int i = 0; i < N; i++) {
            int min = i;
            for (int j = i+1; j < N; j++) {
                if (less(c, a[j], a[min])) min = j;
            }
            exch(a, i, min);
            assert isSorted(a, c, 0, i);
        }
        assert isSorted(a, c);
    }


   /***********************************************************************
    *  Helper sorting functions
    ***********************************************************************/
    
    // is v < w ?
    private static boolean less(Comparable v, Comparable w) {
        return (v.compareTo(w) < 0);
    }

    // is v < w ?
    private static boolean less(Comparator c, Object v, Object w) {
        return (c.compare(v, w) < 0);
    }
        
        
    // exchange a[i] and a[j]
    private static void exch(Object[] a, int i, int j) {
        Object swap = a[i];
        a[i] = a[j];
        a[j] = swap;
    }


   /***********************************************************************
    *  Check if array is sorted - useful for debugging
    ***********************************************************************/

    // is the array a[] sorted?
    private static boolean isSorted(Comparable[] a) {
        return isSorted(a, 0, a.length - 1);
    }
        
    // is the array sorted from a[lo] to a[hi]
    private static boolean isSorted(Comparable[] a, int lo, int hi) {
        for (int i = lo + 1; i <= hi; i++)
            if (less(a[i], a[i-1])) return false;
        return true;
    }

    // is the array a[] sorted?
    private static boolean isSorted(Object[] a, Comparator c) {
        return isSorted(a, c, 0, a.length - 1);
    }

    // is the array sorted from a[lo] to a[hi]
    private static boolean isSorted(Object[] a, Comparator c, int lo, int hi) {
        for (int i = lo + 1; i <= hi; i++)
            if (less(c, a[i], a[i-1])) return false;
        return true;
    }



    // print array to standard output
    private static void show(Comparable[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }
}

 

分享到:
评论
1 楼 20131007 2013-11-19  
用java描述算法?

相关推荐

    Java数据结构及算法实例:选择排序 Selection Sort

    主要介绍了Java数据结构及算法实例:选择排序 Selection Sort,本文直接给出实现代码,代码中包含详细注释,需要的朋友可以参考下

    数据结构常用算法c++实现

    数据结构常用算法c++实现,程序目录如下: Array shuffle Prime test(trial division) Prime test(Miller-Rabin's method) 2D Array Arbitary Integer Linear congruential generator Maximum subarray problem Bit...

    c++数据结构与算法实现

    Sort.h: A collection of sorting and selection routines TestSort.cpp: Test program for sorting and selection routines RadixSort.cpp: Radix sorts DisjSets.h: Header file for disjoint sets algorithms ...

    数据结构算法实现(严蔚敏版配套实现程序)

    1.4.6 双亲、孩子和兄弟节点的查询(顺序结构) 158 范例1-60 双亲、孩子和兄弟节点的查询 158 ∷相关函数:Parent函数 LeftChild函数 RightChild函数 LeftSibling函数 RightSibling函数 1.4.7 双亲、孩子和兄弟节点...

    playAlgorithm:实现大部分基础算法的仓库

    学习的大部分基础数据结构和相关的算法 实现语言主要是C/C++ BST: 二分搜索树 BubbleSort: 冒泡排序 CircleLinkList: 循环链表 Expression: 表达式求值 Fibonacci: 斐波那契数列 Floyd: 弗洛伊德算法 Graphic: 图...

    c语言数据结构算法演示(Windows版)

    本课件是一个动态演示数据结构算法执行过程的辅助教学软件, 它可适应读者对算法的输入数据和过程执行的控制方式的不同需求, 在计算机的屏幕上显示算法执行过程中数据的逻辑结构或存储结构的变化状况或递归算法执行...

    数据结构算法演示(Windows版)

    本课件是一个动态演示数据结构算法执行过程的辅助教学软件, 它可适应读者对算法的输入数据和过程执行的控制方式的不同需求, 在计算机的屏幕上显示算法执行过程中数据的逻辑结构或存储结构的变化状况或递归算法执行...

    数据结构算法实现(严蔚敏版配套实现程序)

    数据结构算法实现(严蔚敏版配套实现程序) 1.1 数组和字符串 2 1.1.1 一维数组的倒置 2 范例1-1 一维数组的倒置 2 ∷相关函数:fun函数 1.1.2 一维数组应用 3 范例1-2 一维数组应用 3 1.1.3 一维数组的高级应用 5 ...

    java_algorithm:Java算法集合:排序、高级排序、堆和堆排序、二分搜索树、并查表、图的基础、最小生成树、最短路径

    ·【v0.02】SelectionSort排序算法更新,com.myself.algorithm.sort.SelectionSort.class ·【v0.03】template泛型模板函数传参,com.myself.algorithm.sort.template 相关笔记 1、为什么学习O(n^2)的排序算法? ·...

    Ruby中的算法和数据结构:算法,数据结构和编程挑战的Ruby实现

    Ruby中的算法和数据结构精选在超和该存储库包含各种算法和数据结构的Ruby实现,以及和的许多挑战的解决方案内容: 基于二分搜索的问题阵列旋转算法阵列旋转的块交换算法子数组问题(Kadane算法)改组数组在数组中...

    算法和数据结构:Java实现常用数据结构和算法

    算法和数据结构 目录 └─Outline ├─algorithms │ ├─search │ │ ├─binarysearch │ │ └─linearsearch │ └─sort │ ├─bubblesort │ ├─countingsort │ ├─insertionsort │ ├─...

    学习数据结构算法必备

    数据结构算法演示 1. 顺序表 (1)在顺序表中插入一个数据元素(ins_sqlist) (2)删除顺序表中一个数据元素(del_sqlist) (3)合并两个有序顺序表(merge_sqlist) 2. 链表 (1)创建一个单链表(Crt_LinkList) ...

    数据结构和算法:此存储库包含数据结构和算法程序(详细解释)

    数据结构数据结构是一种组织数据的方式,以便可以有效地使用它。内容

    数据结构演示软件

    本课件是一个动态演示数据结构算法执行过程的辅助教学软件, 它可适应读者对算法的输入数据和过程执行的控制方式的不同需求, 在计算机的屏幕上显示算法执行过程中数据的逻辑结构或存储结构的变化状况或递归算法执行...

    严蔚敏 数据结构算法演示(Windows版)软件

    本课件是一个动态演示数据结构算法执行过程的辅助教学软件, 它可适应读者对算法的输入数据和过程执行的控制方式的不同需求, 在计算机的屏幕上显示算法执行过程中数据的逻辑结构或存储结构的变化状况或递归算法执行...

    php数据结构 算法(PHP描述) 简单选择排序 simple selection sort

    复制代码 代码如下: &lt;?php /** * 简单选择排序 simple selection sort * * ... */ function sort_simple_selection($list) { $len = count($list); if(empty($len)) return $list; for($i = 0;$i &lt; $len; $i++) {

    用c描述的数据结构演示软件

    本课件是一个动态演示数据结构算法执行过程的辅助教学软件, 它可适应读者对算法的输入数据和过程执行的控制方式的不同需求, 在计算机的屏幕上显示算法执行过程中数据的逻辑结构或存储结构的变化状况或递归算法执行...

    leetcode中国-np-algorithm:数据结构和算法

    学习算法和数据结构到底有没有用? [无代码] 1-3 更多课程学习注意事项 [无代码] 1-4 课程编程环境的搭建 [无代码] 1-5 【文字】JDK 的国内下载链接,和更多学习方法 [无代码] 第二章 线性查找法 (02-Linear-Search/...

    普林斯顿算法课程 Robert Sedgewick

    这是一门经典的数据结构与算法课,免费,分上下两部分,上部分内容包括Union-Find, basic data structures(Array, LinkedList, Queue, Stack, prioprity queue, symbol table...), sorting algorithms (selection ...

Global site tag (gtag.js) - Google Analytics