Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace quicksort
- {
- using System;
- public class Program
- {
- static int count = 0;
- public static void Main()
- {
- Random rnd = new Random();
- int[] arr = new int[14];
- for (int i = 0; i < 14; i++)
- {
- int cislo = rnd.Next(0, 500);
- arr[i] = cislo;
- }
- int j, n, l, p;
- n = arr.Length;
- Console.Write("Původní pole : ");
- for (j = 0; j < n; j++)
- {
- Console.Write(arr[j] + " ");
- }
- Console.WriteLine("");
- int[] k = new int[1];
- l = 0;
- p = arr.Length - 1;
- if (arr.Length < 15 )
- {
- InsertionSort(arr);
- }
- else
- {
- QuickSort(arr, l, p);
- }
- Console.Write("Tolikrát jsem se musel juknout cotoe za číslo : ");
- Console.WriteLine(count);
- Console.Write("Takhle to dopadá : ");
- for (j = 0; j < n; j++)
- {
- Console.Write(arr[j] + " ");
- }
- }
- public static int Partition(int[] arr, int l, int p)
- {
- int pivot = arr[(l + p) / 2];
- while (l <= p)
- {
- while (arr[l] < pivot)
- {
- l++;
- count++;
- }
- while (arr[p] > pivot)
- {
- p--;
- count++;
- }
- if (l <= p)
- {
- int temp = arr[p];
- arr[p] = arr[l];
- arr[l] = temp;
- l++;
- p--;
- }
- }
- return l;
- }
- public static void QuickSort(int[] arr, int l, int p)
- {
- int index;
- if (arr.Length > 1)
- {
- index = Partition(arr, l, p);
- if (l < index - 1)
- {
- QuickSort(arr, l, index - 1);
- }
- if (p > index)
- {
- QuickSort(arr, index, p);
- }
- }
- }
- public static void InsertionSort(int [] arr)
- {
- int n = arr.Length;
- int t, iMin;
- for (int i = 0; i < n - 1; i++)
- {
- count++;
- iMin = i;
- for (int x = i + 1; x < n; x++)
- {
- if (arr[x] < arr[iMin])
- {
- iMin = x;
- }
- }
- t = arr[iMin];
- arr[iMin] = arr[i];
- arr[i] = t;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment