Advertisement
jhei13

QUICKSORT _ALGORI_FINAL

Sep 8th, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace KWEKSHORT002
  7. {
  8.     class Program
  9.     {
  10.        
  11.            //Where swapping happens
  12.             static void Swap<T>(ref T lhs, ref T rhs)
  13.         {
  14.            
  15.             T temp;
  16.             temp = lhs;
  17.             lhs = rhs;
  18.             rhs = temp;
  19.  
  20.         }
  21.  
  22.         static void qsort(int[] tab, int left, int right)
  23.         {
  24.             //foreach (int number in tab)
  25.             //{
  26.             //    Console.Write(number + " ");
  27.             //}
  28.             //Console.WriteLine();
  29.  
  30.  
  31.  
  32.             if (left < right)
  33.             {
  34.                 int m = left;
  35.  
  36.                 for (int i = left + 1; i <= right; i++)
  37.                     if (tab[i] < tab[left])
  38.                         Swap(ref tab[++m], ref tab[i]);
  39.  
  40.                 Swap(ref tab[left], ref tab[m]);
  41.  
  42.                 qsort(tab, left, m - 1);
  43.                 qsort(tab, m + 1, right);
  44.             }
  45.         }
  46.  
  47.         //Starts here
  48.         static void Main(string[] args)
  49.         {
  50.             //int n = 0;
  51.  
  52.             //Console.WriteLine("input range :");
  53.             //n = int.Parse(Console.ReadLine());
  54.             Console.Title = "Quick Sort";
  55.             const int nItems = 0;
  56.             Random r = new Random(nItems);
  57.  
  58.                 int[] a = { 0, 12, 34, 9, 54, 12, 77, -3, -20 };
  59.             //int[] a = new int[n];
  60.             for (int j = 0; j <= a.Length - 1; j++)
  61.            
  62.  
  63.               a[j] = r.Next(1,100);
  64.             //for (int j = 0; j <= a.Length - 1; j++)
  65.             //{
  66.             //    Console.Write("input value :");
  67.             //    a[j] = int.Parse(Console.ReadLine());
  68.             //}
  69.             int i;
  70.             int left  = 0;
  71.             int right = a.Length - 1;
  72.  
  73.             Console.WriteLine("Data before sort ");
  74.             for (i = 0; i < a.Length; i++)
  75.                 Console.Write(" {0} ", a[i]);
  76.             Console.WriteLine();
  77.  
  78.             qsort(a, left, right);
  79.  
  80.             Console.WriteLine("Data after sort");
  81.             for (i = 0; i < a.Length; i++)
  82.                 Console.Write(" {0} ", a[i]);
  83.             Console.WriteLine();
  84.             Console.ReadLine();
  85.  
  86.             Console.ReadLine();
  87.  
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement