Sorting algorithm (iii): Simple selection sorting algorithm-java implementation

Source: Internet
Author: User

Sorting algorithm (iii): Simple selection sorting algorithm-java implementation

The simple selection algorithm is another sort of sorting algorithm in addition to the heap ordering, it is also an unstable sorting algorithm, the average time complexity is O (N2), the space time complexity is O (1)

Simple selection algorithm is relatively simple, and easy to understand, the specific sorting algorithm ideas are as follows:

    1. Select the smallest element in the array to swap with array[i], always dividing the array into aray[0]...array[i] ordered and array[i+1]...array[array.length-1] unordered two parts
    2. Place the smallest value in the unordered order at Array[i+1]

The implementation code is as follows:

1  Public classSelectsort {2 3      Public Static voidMain (string[] args) {4         int[] Array =New int[]{5, 3, 6, 2, 1, 9, 4, 8, 7};5Selectsort Selectsort =NewSelectsort ();6 Selectsort.selectsort (array);7          for(inti = 0; i < Array.Length; i++) {8System.out.print (Array[i] + "");9         }Ten     } One  A      Public voidSelectsort (int[] Array) { -         if(Array = =NULL|| Array.Length = = 0)return; -  the          for(inti = 0; i < Array.Length; i++) { -             //default minimum is I -             intMin =i; -             //This loop is primarily a selection of the smallest element +              for(intj = i + 1; J < Array.Length; J + +) { -                 if(Array[min] >Array[j]) { +Min =J; A                 } at             } -  -             //swap two elements -             if(min! =i) { - Swap (array, I, min); -             } in  -         } to     } +  -     Private voidSwapint[] Array,intIintj) { the         //there may be overflow *Array[i] = Array[i] +Array[j]; $ARRAY[J] = array[i]-Array[j];Panax NotoginsengArray[i] = array[i]-Array[j]; -     } the}

Sorting algorithm (iii): Simple selection sorting algorithm-java implementation

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.