Sorting And Binary Search With ArrayList


Random class is used to generate random number. This program generate random number and we convert that number to char type and after that find that particular character that is generated randomly in the collection created by the ArrayList.
------------------------------------------------------------------------------------------------------------
using System;  
using System.Collections;

public class SortAndBinarySearchWithArrayList
{
static void Main()
{

ArrayList alist = new ArrayList();
Random rand = new Random();
char k = (char)rand.Next(65,90);
Console.WriteLine("Adding 6 Elements");

for(int i=0;i<6;i++)  
{
alist.Add((char)(rand.Next(65,90)));
}

for(int i=0;i<alist.Count;i++)  
{
Console.Write(alist[i] + " ");
}

Console.WriteLine();

alist.Sort();

Console.WriteLine("Elements After Sorting");

foreach(char c in alist)
{
Console.Write(c + " ");  
}

Console.WriteLine();

Console.WriteLine("The index of {0} is {1}", k,alist.BinarySearch(k));
}
}
------------------------------------------------------------------------------------------------------------



Sorting And Binary Search With ArrayList Sorting And Binary Search With ArrayList Reviewed by Baljeet Singh on 22:50 Rating: 5

No comments:

Powered by Blogger.