C# 0 The Complete Reference


Sorting and Searching Arrays



Download 4,07 Mb.
Pdf ko'rish
bet804/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   800   801   802   803   804   805   806   807   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Sorting and Searching Arrays

Often you will want to sort the contents of an array. To handle this, 



Array

 supports a rich 

complement of sorting methods. Using 

Sort( )

, you can sort an entire array, a range within 

an array or a pair of arrays that contain corresponding key/value pairs. Once an array has 

been sorted, you can efficiently search it using 



BinarySearch( )

. Here is a program that 

demonstrates the 

Sort( )

 and 


BinarySearch( )

 methods by sorting an array of 



int

s:

// Sort an array and search for a value.



using System;

class SortDemo {

  static void Main() {

    int[] nums = { 5, 4, 6, 3, 14, 9, 8, 17, 1, 24, -1, 0 };

    // Display original order.

    Console.Write("Original order: ");

    foreach(int i in nums)

      Console.Write(i + " ");

    Console.WriteLine();

    // Sort the array.

    Array.Sort(nums);

    // Display sorted order.

    Console.Write("Sorted order:   ");

    foreach(int i in nums)

      Console.Write(i + " ");

    Console.WriteLine();

    // Search for 14.

    int idx = Array.BinarySearch(nums, 14);

    Console.WriteLine("Index of 14 is " + idx);

  }


}

The output is shown here:

Original order: 5 4 6 3 14 9 8 17 1 24 -1 0

Sorted order:   -1 0 1 3 4 5 6 8 9 14 17 24

Index of 14 is 9

In the preceding example, the array has an element type of 



int

, which is a value type. 

All methods defined by 

Array

 are automatically available to all of the built-in value types. 

However, this may not be the case for arrays of object references. To sort or search an array 

of object references, the class type of those objects must implement either the 



IComparable

or

IComparable

 interface. If the class does not implement one of these interfaces, a 

runtime exception will occur when attempting to sort or search the array. Fortunately, both 



IComparable

 and 


IComparable

 are easy to implement.



IComparable

 defines just one method:

int CompareTo(object 

obj

)

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   800   801   802   803   804   805   806   807   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish