Friday 24 April 2015

Quick Short Algorithm example in c# asp.net



How it Works:-

The algorithm starts by choosing a pivot value. It proceeds by partitioning the elements. Elements larger than the pivot are partitioned on the right side of the pivot and element smaller than the pivot are partitioned on the left side of the pivot. It then recursively applies the algorithm on the partitions.

Below I have written a quick short program in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleDemoApp
{
    class test
    {
        static void Main(string[] args)
        {
            Console.Write("\nQUICK SORT Program in using Recursive Method c#");
            Console.Write("\n\nEnter the total number of elements: ");
            int max = Convert.ToInt32(Console.ReadLine());

            int[] NumArray = new int[max];

            for (int i = 0; i < max; i++)
            {
                Console.Write("\nEnter [" + (i + 1).ToString() + "] element: ");
                NumArray[i] = Convert.ToInt32(Console.ReadLine());
            }

            Console.Write("\nBefore Sorting   : ");
            for (int k = 0; k < max; k++)
                Console.Write(NumArray[k] + " ");
            Console.Write("\n");

            QuickSort_Recursive(NumArray, 0, max - 1);
            Console.Write("\nShorted Array   : ");
            for (int k = 0; k < max; k++)
                Console.Write(NumArray[k] + " ");

            Console.WriteLine("\nPress any key to continue");
            Console.ReadLine();

        }
        static public int Partition(int[] numbers, int left, int right)
        {
            int pivot = numbers[left];
            while (true)
            {
                while (numbers[left] < pivot)
                    left++;

                while (numbers[right] > pivot)
                    right--;

                if (left < right)
                {
                    int temp = numbers[right];
                    numbers[right] = numbers[left];
                    numbers[left] = temp;
                }
                else
                {
                    return right;
                }
            }
        }

        static public void QuickSort_Recursive(int[] arr, int left, int right)
        {
            // For Recusrion
            if (left < right)
            {
                int pivot = Partition(arr, left, right);

                if (pivot > 1)
                    QuickSort_Recursive(arr, left, pivot - 1);

                if (pivot + 1 < right)
                    QuickSort_Recursive(arr, pivot + 1, right);
            }
        }
       
    }

}

Output:



Thursday 23 April 2015

Bubble short example in c# asp.net

Example of bubble short in c# asp.net

In Bubble short we start comparing from the first two adjacent element of the Array a for given order like Ascending  or Descending  for each pass.

Syntex
int i=0, temp;
pas 1 for i=0
     Array[i]>Array[i+1];  
if condition is true then swap
temp=Array[i];
Array[i]=Array[i+1]; 
Array[i]=temp; 

then next pass...

Here I wrting a program in which I have taken defined Array with elements.
Simply it will short by Bubble short and print the resulted Array in Ascending order.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleDemoApp
{
    class test
    {
        static void Main()
        {
            int[] NumArray = { 4, 69, 9, 83, 34, 45,11,17,55};
            int temp;
            Console.Write("Before Sorting   : ");
            Console.Write("\n");
            for (int k = 0; k < NumArray.Length; k++)
                Console.Write(NumArray[k] + " ");
            Console.Write("\n");

            for (int OuterPass = 1; OuterPass <= NumArray.Length - 2; OuterPass++)
            {
                // Inner  loop 
                for (int i = 0; i <= NumArray.Length - 2; i++)
                {
                    // condition check
                    if (NumArray[i] > NumArray[i + 1])
                    {
                        // Swap the values
                        temp = NumArray[i + 1];
                        NumArray[i + 1] = NumArray[i];
                        NumArray[i] = temp;
                    }

                }

            }

            Console.WriteLine("The Sorted array");
            foreach (int aa in NumArray)
                Console.Write(aa + " ");
            Console.ReadKey();
        }
    }
}

Output










And Here I am writing a program that will ask user to enter size of Array thel will to enter all elemets one by one. it will print the result after each pass and then final shorted Array.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleDemoApp
{
    class test
    {
        static void Main()
        {
            Console.Write("\nBUBBLE SORT Program in c#");
            Console.Write("\n\nEnter the total number of elements: ");
            int max = Convert.ToInt32(Console.ReadLine());

            int[] NumArray = new int[max];

            for (int i = 0; i < max; i++)
            {
                Console.Write("\nEnter [" + (i + 1).ToString() + "] element: ");
                NumArray[i] = Convert.ToInt32(Console.ReadLine());
            }

            Console.Write("Before Sorting   : ");
            for (int k = 0; k < max; k++)
                Console.Write(NumArray[k] + " ");
            Console.Write("\n");

            for (int i = 1; i < max; i++)
            {
                for (int j = 0; j < max - i; j++)
                {
                    if (NumArray[j] > NumArray[j + 1])
                    {
                        int temp = NumArray[j];
                        NumArray[j] = NumArray[j + 1];
                        NumArray[j + 1] = temp;
                    }
                }
                Console.Write("After iteration " + i.ToString() + ": ");
                for (int k = 0; k < max; k++)
                    Console.Write(NumArray[k] + " ");
                Console.Write(" \n");  
            }

            Console.Write("\n\nThe numbers in ascending orders are given below:\n\n");
            for (int i = 0; i < max; i++)
            {
                Console.Write("Sorted [" + (i + 1).ToString() + "] element: ");
                Console.Write(NumArray[i]);
                Console.Write("\n");
            }
            Console.ReadKey();
        }
    }
}

Output