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





Wednesday 22 April 2015

How to find the Greatest and the smallest number in a given Array in c# asp.net ?

Here I am writing a program in c# to find out the Greatest and the samllest number in gievn array.This program will ask to enter the size of Array then will all numbers one by finally it will print the results.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp
{
    class Program
    {
        public static void Main()
        {
            int n;
            float largest, smallest;
            int[] a = new int[50];
            Console.WriteLine("Please enter the size of Array");
            string s = Console.ReadLine();
            n = Int32.Parse(s);
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("Please enter the {0} elements of the Array", + i+1);
                string s1 = Console.ReadLine();
                a[i] = Int32.Parse(s1);
            }
            Console.Write("");
            largest = a[0];
            smallest = a[0];
            for (int i = 1; i < n; i++)
            {
                if (a[i] > largest)
                    largest = a[i];
                else if (a[i] < smallest)
                    smallest = a[i];
            }
            Console.WriteLine("The Largest element in the array is {0}", largest);
            Console.WriteLine("The Smallest element in the array is {0}", smallest);
            Console.ReadKey();
        }
    }

}


Output