Get current working directory


Using Arrays.binarySearch()



Download 0,84 Mb.
bet10/21
Sana21.04.2022
Hajmi0,84 Mb.
#569000
1   ...   6   7   8   9   10   11   12   13   ...   21
Bog'liq
notes2

Using Arrays.binarySearch()


public class BinarySearch {

public int runBinarySearchUsingJavaArrays(int[] sortedArray, Integer key) {


int index = Arrays.binarySearch(sortedArray, key);
return index;
}

public static void main(String[] args) {


int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
int key = 6;
BinarySearch binSearch = new BinarySearch();
int index1 = binSearch.runBinarySearchUsingJavaArrays(sortedArray, key);
System.out.println("Search element found in location index : " + index1);
}
}
Output:
Search element found in location index : 7

Using Collections.binarySearch()


public class BinarySearch {

public int runBinarySearchUsingJavaCollections(List sortedList, Integer key) {


int index = Collections.binarySearch(sortedList, key);
return index;
}
public static void main(String[] args) {
Integer[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
int key = 6;
BinarySearch binarySearch = new BinarySearch();
int index1 = binarySearch.runBinarySearchUsingJavaCollections(Arrays.asList(sortedArray), key);
System.out.println("Search element found in location index : " + index1);
}
}
Output:
Search element found in location index : 7
Time Complexity: The time complexity of Binary Search can be written as
T(n) = T(n/2) + c
Auxiliary Space: O(1) in the case of iterative implementation. In case of recursive implementation, O(Logn) recursion call stack space.
Algorithmic Paradigm: Decrease and Conquer.

DS Sorting Algorithms



Sorting is an algorithm that arranges the elements of a list in a certain order [either ascending or descending].

Bubble Sort Overview


Bubble sort is the simplest sorting algorithm. It works by iterating the input array from the first element to the last, comparing each pair of elements and swapping them if needed. Bubble sort continues its iterations until no more swaps are needed.
This algorithm is not suitable for large datasets as its average and worst case complexity is of Ο(n2) where n is the number of items.
The only significant advantage that bubble sort has over other implementations is that it can detect whether the input list is already sorted or not.

Download 0,84 Mb.

Do'stlaringiz bilan baham:
1   ...   6   7   8   9   10   11   12   13   ...   21




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