Internal Sorting: Straight selection sort: Sorting by Selection
来源:互联网 发布:网络阅卷注意事项 编辑:程序博客网 时间:2024/06/03 17:37
Straight selection sort:直接选择排序
Animation
Selection sort animation
Selection sort animation. Red is current min. Yellow is sorted list. Blue is current item.
Complexity
Algorithm S
Algorithm S (Straight selection sort). Records
place; after sorting is complete, their keys will be in order,
Sorting is based on the method indicated above, except that it proves to be
more convenient to select the largest element first, then the second largest, etc.
S1. [Loop on
S2. [Find
maximal one; let it be
S3. [Exchange with
are in their final position.) |
Flow diagram
Data table
Jave program
In this program, R1,…,RN were simplified to K1,…,KN.
/** * Created with IntelliJ IDEA. * User: 1O1O * Date: 12/1/13 * Time: 10:01 PM * :)~ * Straight selection sort:Sorting by Selection:Internal Sorting */public class Main { public static void main(String[] args) { int N = 16; int[] K = new int[17]; int temp; int maxIndex; /*Prepare the data*/ K[1] = 503; K[2] = 87; K[3] = 512; K[4] = 61; K[5] = 908; K[6] = 170; K[7] = 897; K[8] = 275; K[9] = 653; K[10] = 426; K[11] = 154; K[12] = 509; K[13] = 612; K[14] = 677; K[15] = 765; K[16] = 703; /*Output unsorted Ks*/ System.out.println("Unsorted Ks:"); for(int i=1; i<=N; i++){ System.out.println(i+":"+K[i]); } System.out.println(); /*Kernel of the Algorithm!*/ for(int j=N; j>=2; j--){ maxIndex = j; for(int i=1; i<=j; i++){ if(K[i] > K[maxIndex]){ maxIndex = i; } } temp = K[j]; K[j] = K[maxIndex]; K[maxIndex] = temp; } /*Output sorted Ks*/ System.out.println("Sorted Ks:"); for(int i=1; i<=N; i++){ System.out.println(i+":"+K[i]); } }}
Outputs
Unsorted Ks:1:5032:873:5124:615:9086:1707:8978:2759:65310:42611:15412:50913:61214:67715:76516:703Sorted Ks:1:612:873:1544:1705:2756:4267:5038:5099:51210:61211:65312:67713:70314:76515:89716:908
Reference
<< The art of computer programming: Sorting and Searching >> VOLUME 3, DONALD E. KNUTH
https://en.wikipedia.org/wiki/Selection_sort
- Internal Sorting: Straight selection sort: Sorting by Selection
- Internal Sorting: Heapsort-2:Sorting by Selection
- Internal Sorting: Heapsort-1: Sorting by Selection
- Internal Sorting: Straight Two-way Merge Sort: Sorting by Merging
- Internal Sorting: Straight insertion: Sorting by Insertion
- Sorting Algorithm-Simple Selection Sort
- Internal Sorting: Radix sort: Sorting by Exchanging
- Internal Sorting: Bubble sort: Sorting by Exchanging
- Internal Sorting: Radix List Sort: Sorting by Distribution
- Internal Sorting: List Merge Sort: Sorting by Merging
- Internal Sorting: Radix exchange sort: Sorting by Exchanging
- Internal Sorting: Natural Two-way Merge Sort: Sorting by Merging
- Internal Sorting: Merge exchange sort: Sorting by Exchanging
- Internal Sorting: Cocktail-shaker sort: Sorting by Exchanging
- Sorting Algorithm-Straight Insertion Sort
- 【Data Structures】 7. Simple Sorting—Bubble Sort, Selection Sort, and Insertion Sort
- 选择排序法(Selection sorting)
- Internal Sorting: Quicksort-2: Sorting by Exchanging
- solr DataImportHandler (数据相关操作)
- jstack,用于检测死锁
- ListView的优化
- Unity中用触摸控制物体旋转和放大
- Java的引用类型(附图说明)
- Internal Sorting: Straight selection sort: Sorting by Selection
- TalkingData美国旧金山之行 —— Twitter
- Source Insight上添加svn命令,打开当前文件夹命令
- Python之学习笔记(模块的使用)
- Log4net 初识及简单使用
- POJ2001 -- 字典树和字符串的前缀
- SCM知识拓展
- Java回忆录(八)--------异常处理
- Objective-C面向对象(中)