Testing of Bubble Sorting Algorithm and sorting time

Source: Internet
Author: User

This is a summary of my experiments. // Wu xinqiang's: 20 using System; using System on March 03, 2013. collections. generic; using System. linq; using System. text; using System. diagnostics; namespace Chapter3_Sort {class CArray {static void Main (string [] args) {CArray ca = new CArray (200); Timing tObj = new Timing (); random random = new Random (10000); // Random number for (int I = 0; I <200; I ++) {ca. insert (random. next (0, 10000);} Console. writeLine ("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 ("During Sorting:"); // Console in Sorting. writeLine (); ca. bubbleSort (); // call the bubble sort method to sort the logs. writeLine (); tObj. stopTime (); // test the end time of the sorting time Console. writeLine ("After Sorting:"); // Console After Sorting. writeLine (); ca. displayElements (); // display the data Console. writeLine (); Console. writeLine ("time (. net): "+ tObj. result (). totalSeconds + "S"); // display the total time used by the bubble sort method Console. writeLine ();} public void BubbleSort () // bubble sort {int temp; for (int outer = upper; outer> = 1; outer --) {for (int inner = 0; inner <= outer-1; inner ++) {if (int) array [inner]> array [inner + 1]) {temp = array [inner]; array [inner] = array [inner + 1]; array [inner + 1] = 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 ;}} lab Result:

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.