Algorithm Crash (ii) the selection of the seven classic sorts

Source: Internet
Author: User
Tags comparison count join sort sorts

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; }   
     }   
 }

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.