Classic (Java edition) analysis and implementation of sorting algorithm three simple selection sort

Source: Internet
Author: User


Select sort-Simple Select sort

The basic idea of sorting is: Each trip selects the smallest record of the keyword from the record to be sorted, placing it in the final order of the ordered sub-series until all records have been sorted. Simple selection sort is also called direct selection sort.

Basic algorithm:

The given sequence to be sorted A[0....N], the first time from a[0]~a[n-1] to select the minimum value, and a[0] Exchange, the second time from the a[1]~a[n-1] to select the minimum value, and a[1] exchange, ...., I time from a[i-1]~a[n-1] to select the minimum value, and a[ I-1] Exchange, ..., n-1 times from a[n-2]~a[n-1], with the a[n-2] exchange, a total of n-1 times, to get a sort code from small to large order sequence.

The direct selection sort and the direct insert sort, all divide the data into the ordered and unordered areas, the difference is that the direct insertion sort is to insert the first element of the unordered area directly into the ordered area to form a larger ordered area, and the direct selection of the order is to select a minimum element from the unordered area directly to the end of the ordered area.

Simple Selection Sorting Example:

Given the array to be sorted a[0......5]={2,7,6,3,1,5}

First trip: I=0, in the unordered area a[0......5], index=4, Element min a[0] and a[4] are exchanged. Get sequence: {1,7,6,3,2,5}

Second trip: I=1, in the unordered area a[1......5], index=4, Element min a[1] and a[4] are exchanged. Get sequence: {1,2,6,3,7,5}

Third trip: i=2, in the unordered area a[2......5], index=3, Element min a[2] and a[3] are exchanged. Get sequence: {1,2,3,6,7,5}

IV: I=3, in the unordered area a[3......5], index=5, Element min a[3] and a[5] are exchanged. Get sequence: {1,2,3,5,7,6}

V: i=4, in the unordered area a[4......5], index=5, Element min A[4] and a[5] are exchanged. Get sequence: {1,2,3,5,6,7}

Six trips: I=5, in the unordered area a[5......5], the last element, do not swap. Get the queued sequence: {1,2,3,5,6,7}

Follow the selection sorting algorithm:

public static void Selectsort (int [] arr) {int len = arr.length;for (int i=0;i<len-1;i++) {int min = i;for (int j = I+1;J&L t;len;j++) {if (Arr[j]<arr[min]) {min =j;}} if (min! = i) {int temp = Arr[i];arr[i] = arr[min];arr[min] = temp;}}}

Complexity of the algorithm

1 time complexity O (n^2)

  Select sort time-consuming operations: Compare + swap locations :

  Total number of comparisons n= (n-1) + (n-2) +...+1=n* (n-1)/2, best and worst case is O (n^2)

2 space complexity

  because A temporary variable is required to Exchange element bits ,

3 stability

        .

This article is from the "Hollows" blog, make sure to keep this source http://honeybee.blog.51cto.com/9309920/1745565

Classic (Java edition) analysis and implementation of sorting algorithm three simple selection sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.