Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Lab17
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Vvedite chislo");
- int length = Convert.ToInt32(Console.ReadLine());
- int[] arr = Generic(length, -100, 100);
- Stopwatch st = new Stopwatch();
- st.Reset();
- Console.Clear();
- Console.WriteLine("Ur array:");
- PrintArray(arr);
- st.Start();
- arr = Bubble_Sort(arr);
- st.Stop();
- Console.WriteLine("\n\n\n\nBubble sort: ");
- PrintArray(arr);
- Console.WriteLine("\n\n Time: " + st.ElapsedMilliseconds);
- arr = quickSort(arr, 0, arr.Length);
- Console.WriteLine("\n\n\n\nQuick sort: ");
- PrintArray(arr);
- Console.WriteLine("\n\n Time: " + st.ElapsedMilliseconds);
- Console.ReadKey();
- }
- static int[] Generic(int length, int min, int max)
- {
- Random rand = new Random(DateTime.Now.Millisecond);
- int[] array = new int[length];
- for (int i = 0; i < length; i++)
- array[i] = rand.Next(min, max);
- return array;
- }
- static void PrintArray(int[] arr)
- {
- foreach (int el in arr)
- Console.Write(el + " ");
- }
- static int [] Bubble_Sort(int[] arr)
- {
- for (int i = 0; i < arr.Length; i++)
- {
- for (int j = 0; j < arr.Length - 1 - i; j++)
- {
- if (arr[j] > arr[j + 1])
- {
- int tmpParam = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = tmpParam;
- }
- }
- }
- return arr;
- }
- static int[] quickSort(int[] a, int l, int r)
- {
- int temp;
- int x = a[l + (r - l) / 2];
- int i = l;
- int j = r;
- try
- {
- while (i <= j)
- {
- while (a[i] < x) i++;
- while (a[j] > x) j--;
- if (i <= j)
- {
- temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- i++;
- j--;
- }
- }
- if (i < r)
- quickSort(a, i, r);
- if (l < j)
- quickSort(a, l, j);
- }
- catch (Exception) { }
- return a;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement