Test the insertion 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 ("InsertionS Ort 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 ("InsertionSort During Sorting:"); // Console in Sorting. writeLine (); ca. insertionSort (); // call the select sort method to sort the Console. writeLine (); tObj. stopTime (); // test the end time of the sorting time Console. writeLine ("InsertionSort After Sorting:"); // Console After Sorting. writeLine (); c A. displayElements (); // display the data Console. writeLine (); Console. writeLine ("InsertionSort time (. net): "+ tObj. result (). totalMilliseconds + "MS"); // display the total time used by the sorting method. writeLine (); ca. clear (); for (int I = 0; I <10; I ++) {ca. insert (random. next (0,100);} // Console. writeLine ("Enter the search value:"); Console. writeLine (); ca. displayElements (); // display the data Console. writeLine (); // line feed tObj. startTime (); // Co at the start time of the test sorting time Nsole. writeLine (); Console. writeLine ("Enter the search value:"); ca. rbinSearch (5, 1, 10); // call the half lookup method to find int v = Convert. toInt32 (Console. readLine (); // if (value = mid) // Console. writeLine ("the value you searched for exists! "); // Else // Console. WriteLine (" the value you searched for does not exist! "); Console. writeLine (); tObj. stopTime (); // test the end time of the query time Console. writeLine (); ca. displayElements (); // display the data Console. writeLine (); Console. writeLine ("RbinSearch time (. net): "+ tObj. result (). totalMilliseconds + "MS"); // display the total time used for selecting the sort method} public void InsertionSort () // Insert the sort method {int temp, inner; for (int outer = 1; outer <= upper; outer ++) {inner = outer; temp = array [outer]; while (inner> 0 & array [inne R-1]> = temp) {array [inner] = array [inner-1]; inner-= 1;} array [inner] = temp; this. displayElements () ;}} public int RbinSearch (int value, int lower, int upper) {int mid; if (lower> upper) return-1; else {// int mid; mid = (int) (lower + upper)/2; if (value <array [mid]) return RbinSearch (value, lower, mid-1 ); else if (value = mid) return mid; else return RbinSearch (value, mid + 1, upp Er);} if (value = mid) Console. WriteLine ("the value you are looking for exists! "); Else Console. WriteLine (" the value you searched for does not exist! ");} 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 ;}} lab Result:

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.