Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. namespace Lab17
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             Console.WriteLine("Vvedite chislo");
  8.             int length = Convert.ToInt32(Console.ReadLine());
  9.             int[] arr = Generic(length, -100, 100);
  10.             Stopwatch st = new Stopwatch();
  11.  
  12.             st.Reset();
  13.             Console.Clear();
  14.             Console.WriteLine("Ur array:");
  15.            
  16.             PrintArray(arr);
  17.  
  18.             st.Start();
  19.             arr = Bubble_Sort(arr);
  20.             st.Stop();
  21.             Console.WriteLine("\n\n\n\nBubble sort: ");
  22.             PrintArray(arr);
  23.             Console.WriteLine("\n\n Time: " + st.ElapsedMilliseconds);
  24.            
  25.             arr = quickSort(arr, 0, arr.Length);
  26.             Console.WriteLine("\n\n\n\nQuick sort: ");
  27.             PrintArray(arr);
  28.             Console.WriteLine("\n\n Time: " + st.ElapsedMilliseconds);
  29.  
  30.             Console.ReadKey();
  31.         }
  32.  
  33.         static int[] Generic(int length, int min, int max)
  34.         {
  35.             Random rand = new Random(DateTime.Now.Millisecond);
  36.             int[] array = new int[length];
  37.  
  38.             for (int i = 0; i < length; i++)
  39.                 array[i] = rand.Next(min, max);
  40.  
  41.             return array;
  42.         }
  43.  
  44.         static void PrintArray(int[] arr)
  45.         {
  46.             foreach (int el in arr)
  47.                 Console.Write(el + " ");
  48.         }
  49.  
  50.         static int [] Bubble_Sort(int[] arr)
  51.         {
  52.             for (int i = 0; i < arr.Length; i++)
  53.             {
  54.                 for (int j = 0; j < arr.Length - 1 - i; j++)
  55.                 {
  56.                     if (arr[j] > arr[j + 1])
  57.                     {
  58.                         int tmpParam = arr[j];
  59.                         arr[j] = arr[j + 1];
  60.                         arr[j + 1] = tmpParam;
  61.                     }
  62.                 }
  63.             }
  64.            
  65.             return arr;
  66.         }
  67.  
  68.         static int[] quickSort(int[] a, int l, int r)
  69.         {
  70.             int temp;
  71.             int x = a[l + (r - l) / 2];
  72.            
  73.             int i = l;
  74.             int j = r;
  75.            
  76.             try
  77.             {
  78.                 while (i <= j)
  79.                 {
  80.                     while (a[i] < x) i++;
  81.                     while (a[j] > x) j--;
  82.                     if (i <= j)
  83.                     {
  84.                         temp = a[i];
  85.                         a[i] = a[j];
  86.                         a[j] = temp;
  87.                         i++;
  88.                         j--;
  89.                     }
  90.                 }
  91.                 if (i < r)
  92.                     quickSort(a, i, r);
  93.  
  94.                 if (l < j)
  95.                     quickSort(a, l, j);
  96.             }
  97.             catch (Exception) { }
  98.  
  99.             return a;
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement