Today is the choice of sorting, including "Direct selection sort" and "heap sort."
The last time "bubble sort" was a quick row of abuse, and "Fast Platoon" won the reuse of Necoux, the natural envy of the brothers, the need to find a faster than the row.
This is not today, the two brothers to find a quick row of accounts.
1. Direct Selection Order:
First figure:
To be honest, direct selection is the most akin to human instinct, such as putting a three-year-old toy in a sequence of sizes,
The child will first find the smallest one in so many toys and then find the second one, and so on.
, how clever children are, so young to know the direct selection of the sort. The envy of ...
Yes, the kids taught us a lesson.
The first step: we take 80 as reference (base), after 80 to find a minimum of 20, and then Exchange 80 with 20.
Step two: The first digit is already the smallest number, then we push further to find a minimum number on the back of 30, find ourselves the smallest, do not exchange.
The third step: ...
Finally, we sorted the finished. Done.
Since it is to challenge, then 5 innings 3 wins system.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading;
Using System.Diagnostics;
Namespace Selectionsort {public class program {static void Main (string[] args) {///5 comparison for (int i = 1; I <= 5; i++) {list<
int> list = new list<int> (); Inserts a 2w random number into the array for (int j = 0; J < 20000; J + +) {Thread .
Sleep (1); List. ADD (new Random (int) DateTime.Now.Ticks).
Next (1000, 1000000));
} Console.WriteLine ("\ n" + i + "second comparison:");
Stopwatch watch = new stopwatch (); Watch.
Start (); var result = list. By-=> (single).
ToList (); Watch. Stop(); Console.WriteLine ("\ n Quick sort time consuming:" + watch.)
Elapsedmilliseconds); Console.WriteLine ("Output first 10 digits:" + string.) Join (",", result. Take (10).
ToList ())); Watch.
Start ();
result = Selectionsort (list); Watch.
Stop (); Console.WriteLine ("\ n) direct selection of sorting time consuming:" + watch.
Elapsedmilliseconds); Console.WriteLine ("Output first 10 digits:" + string.) Join (",", list.) Take (10).
ToList ()));
}///select sort static list<int> selectionsort (list<int> List) {///the number of times to traverse for (int i = 0; i < list.) Count-1;
i++) {//Assume the tempindex of the lower value of the minimum int tempindex = i; for (int j = i + 1; j < list.) Count;
J + +) {//If the value of the target under Tempindex is greater than the value of the object under J, record lesser value subscript j if (List[tempindex] > list[j]) tempindex = j;
//finally swap the imaginary minimum with the true minimum value var tempdata = List[tempindex];
List[tempindex] = List[i];
List[i] = TempData;
} return list; }
}
}