Advertisement
ionut258

bubble sort benchmark

Aug 3rd, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7.  
  8. namespace array_benchmark
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int ok = 1,temp,c=0;
  15.             int[] x = new int[1001];
  16.             int[] p = new int[1001];
  17.             Random rnd = new Random();
  18.             for (int i = 0; i < 1000; i++)
  19.                 p[i]=x[i] = rnd.Next(-100,100);
  20.            
  21.             var sw = Stopwatch.StartNew();
  22.  
  23.             while (ok == 1)
  24.             {
  25.                 ok = 0;
  26.                 c++;
  27.  
  28.                 for (int i = 0; i < x.Length - c; i++)
  29.                 {
  30.                     if (x[i] > x[i + 1])
  31.                     {
  32.                         temp = x[i];
  33.                         x[i] = x[i + 1];
  34.                         x[i + 1] = temp;
  35.                         ok = 1;
  36.                     }
  37.                 }
  38.  
  39.  
  40.             }
  41.             sw.Stop();
  42.             Console.WriteLine("bubble sort speed for 1000 random elements array ={0}", sw.Elapsed);
  43.  
  44.             var sw2 = Stopwatch.StartNew();
  45.             Array.Sort(p);
  46.             sw2.Stop();
  47.             Console.WriteLine("quicksort speed for 1000 random elements ={0}", sw2.Elapsed);
  48.             Console.ReadLine();
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement