Select and sort by Sorting Algorithm

Source: Internet
Author: User

Select and sort by Sorting Algorithm

Select sorting definition: select the smallest (or largest) element from the data elements to be sorted, and place the elements in sequence at the end of the sorted series, until all data elements to be sorted are arranged. Selecting sorting is an unstable sorting method.

Class Program {static void Main (string [] args) {int [] array = new [] {234,632, 23,643, 2, 6,-2,423,234, 43}; Console. writeLine ("Before sorting:"); Console. writeLine (string. join (",", array); SelectSort (array); Console. writeLine ("sorted:"); Console. writeLine (string. join (",", array); Console. readKey ();} /// <summary> /// select sort /// </summary> /// <param name = "sources"> target array </param> private static void SelectSort (int [] sources) {for (int I = 0, len = sources. length-1; I <= len; I ++) {// assume that the minimum index int minIndex = I; // cyclically traverse the index for (int j = I + 1; j <= len; j ++) that finds the minimum value {// if the minimum value is greater than other elements, reset the minimum value index if (sources [minIndex]> sources [j]) {minIndex = j ;}// the position where the temporary variable exchanges the minimum value; int temp = sources [I]; sources [I] = sources [minIndex]; sources [minIndex] = temp ;}}}

 


C language selection Sorting Algorithm

Void bubble (int a [], int n) {int I, k, temp; for (k = 0; k <n-1; k ++) {for (I = k + 1; I <n; I ++) {if (a [k]> a [I]) {temp = a [I]; a [I] = a [k]; a [k] = temp ;}}for (I = 0; I <n; I ++) printf ("% d", a [I]); printf ("\ n ");}

What is the idea of selecting a sorting algorithm?

Select the maximum value of the current sequence for each time:
For example, 1, 3, 6, 9, and 5 are sorted in ascending order!
Step 1
Step 2, 1
Step 3, 3, 5
Step 4, 1, 5, 6
Step 5, 1, 5, 6, 9

// Progressively select the implementation of sorting!

Void SelectSort (double * head, int amount)
{
Double temp;
Int m, n;
For (m = 0; m <amount-1; m ++)
{
For (n = m + 1; n <amount; n ++)
{
If (head [n] {
Temp = head [n];
Head [n] = head [m];
Head [m] = temp;
}
}
}
}

Related Article

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.