Tuesday, March 19, 2013

Array Sort, Binarysearch, and Reverse

using System;

class
linSearch{
    public static void Main()
    {
        int[] a = new int[3];
        Console.WriteLine("Enter number of elements you want to hold in the array (max3)?");
        string s = Console.ReadLine();
        int x = Int32.Parse(s);
        Console.WriteLine("--------------------------------------------------");
        Console.WriteLine("\n Enter array elements \n");
        Console.WriteLine("--------------------------------------------------");

        for (int i = 0; i < x; i++)
        {
            string s1 = Console.ReadLine();
            a[i] = Int32.Parse(s1);
        }

        Console.WriteLine("Enter Search element\n");
        Console.WriteLine("--------------------------------------------------");
        string s3 = Console.ReadLine();
        int x2 = Int32.Parse(s3);

        // Sort the values of the Array.        Array.Sort(a);

        for (int i = 0; i < x; i++)
        {
            Console.WriteLine("--------------Sorted-------------------------");
            Console.WriteLine("Element{0} is {1}", i + 1, a[i]);
        }

        // BinarySearch the values of the Array.        int x3 = Array.BinarySearch(a, (Object)x2);
        Console.WriteLine("--------------------------------------------------");
        Console.WriteLine("BinarySearch: " + x3);
        Console.WriteLine("Element{0} is {1}", x3, a[x3]);
        Console.WriteLine("--------------------------------------------------");

        // Reverse the values of the Array.        Array.Reverse(a);
        Console.WriteLine("-----------Reversed-------------------------------");

        for (int i = 0; i < x; i++)
        {
            Console.WriteLine("----------------------------------------------");
            Console.WriteLine("Element{0} is {1}", i + 1, a[i]);
        }
    }
}


Listing 20.15 is a more sophisticated example of using the IComparer interface and Sort function together. The IComparer interface allows you to define a Compare method in order to do a comparison between two elements of your array. This Compare method is called repeatedly by the Sort function in order to sort the array. Listing 20.15 defines a Compare method that does a comparison between two strings.
Listing 20.15: Array Sorting

// sort an array according to the Nth element

using
System;
using
System.Collections;

public
class CompareX : IComparer{
    int compareFrom = 0;
    public CompareX(int i)
    {
        compareFrom = i;
    }

    public int Compare(object a, object b)
    {
        return String.Compare(a.ToString().Substring(compareFrom),
        b.ToString().Substring(compareFrom));
    }
}


public
class ArrListEx{
    ArrayList arr = new ArrayList();
    public ArrListEx()
    {
        arr.Add("aaaa9999");
        arr.Add("bbbb8888");
        arr.Add("cccc7777");
        arr.Add("dddd6666");
        arr.Sort(new CompareX(4));
        IEnumerator arrList = arr.GetEnumerator();

        while (arrList.MoveNext())
        {
            Console.WriteLine("Item: {0}", arrList.Current);
        }
    }

    public static void Main(string[] args)
    {
        new ArrListEx();
    }
}

No comments:

Post a Comment