BinarySearch, Sort And Reverse Method of ArrayList in C#

Introduction

The ArrayList class provides a number of properties and methods that are used to work with an ArrayList. In this article, I'm going to describe three important methods of the ArrayList class, which are:

  • BinarySearch(): Performs a binary search on the ArrayList.
  • Sort(): Sorts the items in the ArrayList.
  • Reverse(): Reverses items in the ArrayList.

Namespace: System.Collections.Generic

Assembly: mscorlib (in mscorlib.dll)

BinarySearch()

The BinarySearch method of the ArrayList class implements the binary searching algorithm. This uses a "divide and conquer" approach to finding the correct element, and only works on a pre-sorted array. For this reason, never use BinarySearch if your ArrayList might not be sorted.

The BinarySearch method returns the 0-based index of the object if it's found. If the object is not found in the list, BinarySearch returns –1.

  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace BinarySearchmethod  
  5. {  
  6.     class AKSHAY  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             List<string> list = new List<string>() { "A""B""C""D""E""F""G" };  
  11.             int index1 = list.BinarySearch("C");  
  12.             Console.WriteLine($"Index 1: {index1}");  
  13.             int index2 = list.BinarySearch("F");  
  14.             Console.WriteLine($"Index 2 : {index2}");  
  15.             int index3 = list.BinarySearch("H");  
  16.             Console.WriteLine($"Index 3 : {index3}");  
  17.             // wait for input before exiting   
  18.             Console.WriteLine("Press enter to finish");  
  19.             Console.ReadLine();  
  20.         }  
  21.     }  

Binary Search in ArrayList

Sort()

Sorting an ArrayList is done in many programs that use ArrayList, as sorting provides a view of the data that is helpful for both users and computers. The Sort method in the base class libraries is a parameterless instance method on ArrayList.

The ArrayList.Sort() used Quick algorithm to sort the element of Arraylist class. The QuickStort algorithm is a comparison sort (also called an unstable sort), which means that a "less than or equal to" comparison operation determines which of two elements should occur first in the final sorted list. However, if two elements are equal, their original order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal. To perform a stable sort, you must implement a custom IComparer interface to use with the other overloads of this method.

When we call Sort() on the ArrayList, the default implementation of IComparer is called which uses QuickSort.  QuickSort calls the IComparable implementation of CompareTo() on each of your objects in the ArrayList.

  1. using System;  
  2. using System.Collections;  
  3. class akshay  
  4. {  
  5.     static void Main()  
  6.      {  
  7.         // Create an ArrayList with 4 strings.  
  8.         ArrayList list = new ArrayList();  
  9.         list.Add("Manesh");  
  10.         list.Add("Akshay");  
  11.         list.Add("Vikash");  
  12.         list.Add("Anuj");  
  13.         list.Add("Dharmesh");  
  14.         list.Add("Raman"):         
  15.         // Get all elements in ArrayList.  
  16.         Console.WriteLine("The ArrayList initially contains the following values...");  
  17.         foreach (string value in list)  
  18.         {  
  19.             // Display the elements.  
  20.             Console.WriteLine(value);  
  21.         }  
  22.         // sort the elements.  
  23.         list.Sort();  
  24.         Console.WriteLine("The sorted array list is ...");  
  25.         foreach (string value in list)  
  26.         {  
  27.             // Display Sorted elements.  
  28.             Console.WriteLine(value);   
  29.         }  
  30.         Console.Read();  
  31.     }  
  32. }

Sort ArrayList

Reverse()

The ArrayList.Reverse() method are used to reverse the order of element of the ArrayList class such that the element at ArrayList [i], where i is any index within the range, moves to ArrayList [j], where j equals index + index + count - i - 1. The Reverse method is Overloded. The simplest form of this method uses the following declaration:

  ArrayList.Reverse()

  1. using System;  
  2. using System.Collections;  
  3. class akshay  
  4. {  
  5.     static void Main()  
  6.      {  
  7.         // Create an ArrayList with 4 strings.  
  8.         ArrayList list = new ArrayList();  
  9.         list.Add("Manesh");  
  10.         list.Add("Akshay");  
  11.         list.Add("Vikash");  
  12.         list.Add("Anuj");  
  13.         list.Add("Dharmesh");  
  14.         list.Add("Raman"):         
  15.         // Get all elements in ArrayList.  
  16.         Console.WriteLine("The ArrayList initially contains the following values...");  
  17.         foreach (string value in list)  
  18.         {  
  19.             // Display the elements.  
  20.             Console.WriteLine(value);  
  21.         }  
  22.         // Reverse the elements.  
  23.         list.Reverse();  
  24.         Console.WriteLine("The sorted array list is ...");  
  25.         foreach (string value in list)  
  26.         {  
  27.             // Display Reverse elements.  
  28.             Console.WriteLine(value);   
  29.         }  
  30.         Console.Read();  
  31.     }  
  32. }

reverse arraylist

Resources


Similar Articles