Test the selection of sorting algorithms and sorting time

Source: Internet
Author: User

// March 13, 2013 19:19:01 Wu xinqiang using System; using System. collections. generic; using System. linq; using System. diagnostics; using System. IO; namespace Chapter3_SelectionSort {class CArray {static void Main (string [] args) {CArray ca = new CArray (10); Timing tObj = new Timing (); random random = new Random (100); // Random number for (int I = 0; I <10; I ++) {ca. insert (random. next (0,100);} Console. writeLine ("SelectionSort Before Sorting:"); // Console Before Sorting. writeLine (); ca. displayElements (); // display the data Console. writeLine (); // line feed tObj. startTime (); // test the Console at the start time of the sorting time. writeLine (); Console. writeLine ("SelectionSort During Sorting:"); // Console in Sorting. writeLine (); ca. selectionSort (); // call the select sort method to sort the Console. writeLine (); tObj. stopTime (); // test the end time of the sorting time Console. writeLine ("SelectionSort After Sorting:"); // Console After Sorting. writeLine (); ca. displayElements (); // display the data Console. writeLine (); Console. writeLine ("SelectionSort time (. net): "+ tObj. result (). totalMilliseconds + "MS"); // display the total time used by the sorting method. writeLine (); ca. clear ();} public void SelectionSort () // select the sorting method {int temp, min; for (int outer = 0; outer <= upper; outer ++) {min = outer; for (int inner = outer + 1; inner <= upper; inner ++) {if (array [inner] <array [min]) {min = inner;} temp = array [outer]; array [outer] = array [min]; array [min] = temp;} this. displayElements () ;}} private int [] array; private int upper; private int numElements; public CArray (int Size) {array = new int [Size]; upper = Size-1; // maximum upper length numElements = 0; // array variable} public void Insert (int item) {array [numElements] = item; numElements ++ ;} public void DisplayElements () // display data {for (int I = 0; I <= upper; I ++) Console. write (array [I] + "");} public void Clear () // delete data {for (int I = 0; I <upper; I ++) {array [I] = 0; numElements = 0 ;}} public class Timing // time test class {TimeSpan duration; public Timing () {duration = new TimeSpan (0 );} public void StopTime () {duration = Process. getCurrentProcess (). totalProcessorTime;} public void StartTime () {GC. collect (); GC. waitForPendingFinalizers ();} public TimeSpan Result () {return duration ;}}/ * class CArray {private int [] array; private int upper; private int numElements; public CArray (int Size) {array = new int [Size]; upper = Size-1; numElements = 0;} public void Insert (int item) {array [numElements] = item; numElements ++;} public void DisplayElements () {for (int I = 0; I <= upper; I ++) Console. writeLine (array [I] + "");} public void Clear () {for (int I = 0; I <upper I ++) {array [I] = 0; numElements = 0 ;}}}*/experiment results:

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.