Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace KWEKSHORT002
- {
- class Program
- {
- //Where swapping happens
- static void Swap<T>(ref T lhs, ref T rhs)
- {
- T temp;
- temp = lhs;
- lhs = rhs;
- rhs = temp;
- }
- static void qsort(int[] tab, int left, int right)
- {
- //foreach (int number in tab)
- //{
- // Console.Write(number + " ");
- //}
- //Console.WriteLine();
- if (left < right)
- {
- int m = left;
- for (int i = left + 1; i <= right; i++)
- if (tab[i] < tab[left])
- Swap(ref tab[++m], ref tab[i]);
- Swap(ref tab[left], ref tab[m]);
- qsort(tab, left, m - 1);
- qsort(tab, m + 1, right);
- }
- }
- //Starts here
- static void Main(string[] args)
- {
- //int n = 0;
- //Console.WriteLine("input range :");
- //n = int.Parse(Console.ReadLine());
- Console.Title = "Quick Sort";
- const int nItems = 0;
- Random r = new Random(nItems);
- int[] a = { 0, 12, 34, 9, 54, 12, 77, -3, -20 };
- //int[] a = new int[n];
- for (int j = 0; j <= a.Length - 1; j++)
- a[j] = r.Next(1,100);
- //for (int j = 0; j <= a.Length - 1; j++)
- //{
- // Console.Write("input value :");
- // a[j] = int.Parse(Console.ReadLine());
- //}
- int i;
- int left = 0;
- int right = a.Length - 1;
- Console.WriteLine("Data before sort ");
- for (i = 0; i < a.Length; i++)
- Console.Write(" {0} ", a[i]);
- Console.WriteLine();
- qsort(a, left, right);
- Console.WriteLine("Data after sort");
- for (i = 0; i < a.Length; i++)
- Console.Write(" {0} ", a[i]);
- Console.WriteLine();
- Console.ReadLine();
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement