# Amaliy mashg‘ulot 6 Mavzu

 Download 336.37 Kb. bet 6/6 Sana 15.05.2023 Hajmi 336.37 Kb. #59738
Bog'liq
Amaliy mashg
O‘RQ-518 06.02.2019, N. ismatova, O. Karimova davlat va huquq asoslari, Ikkinchi va uchinchi tartibli determinantlar. Determinantlarni h, Ismatullayev Samandar MB-2 (1), Nazorat ishi kitob 7-11(1)(1), madaminovamubina, 1 labaratoriya zamonaviy, 1678966370, Statistika (1), 3af246d441c8e9fcca40daa88bc92858, 001 1mavzu 2 kurs adabiyot, gggggg, axborot, озода, М А Ъ Л У М О Т Н О М А
 public class MergeSort { public static void main(String[] args) { int[] A = {6,5,12,10,9,1}; sort(A); //saralashni boshlash System.out.println(Arrays.toString(A)); //natijani chop qilish } private static void sort(int[] array) { if(array.length<=1){ //massiv qismlarini elementlar sonin aniqlash return; //1 dan kichik yoki teng bo`lsa ajratishni to`xtatish } int leftSize = array.length/2; //chap tomon bo`lak uzunligini aniqlash int rightSize = array.length-leftSize; //o`ng tomon bo`lak uzunligini aniqlash int[] left = Arrays.copyOfRange(array, 0, leftSize); //mos ravishda o`ng va chap int[] right = Arrays.copyOfRange(array, leftSize, leftSize+rightSize); //submassivlarga ajratish sort(left); //rekursiya orqali sort(right); //bo`laklarga ajratish merge(array, left, right); //to`g`ri tartibda birlashtirish } private static void merge(int[] array, int[] left, int[] right) { int leftIndex = 0; //left va right submassiv elementlarini int rightIndex = 0; //to`g`ri saralangan tartibda int targetIndex = 0; //array massiviga joylashtirsh int remaining = left.length+right.length; while(remaining>0){ if (leftIndex >= left.length){ array[targetIndex] = right[rightIndex++]; }else if (rightIndex >= right.length){ array[targetIndex] = left[leftIndex++]; }else if (left[leftIndex]-right[rightIndex] < 0){ array[targetIndex] = left[leftIndex++]; }else{ array[targetIndex] = right[rightIndex++]; } targetIndex++; remaining--; } } } Aytib o`tish joizki, chiziqli saralash algoritmlaridan farqli o`laroq, birlashmali saralash massiv avvaldan saralangan yoki saralanmaganligiga qaramay ajratib birlashtiradi. Shuning uchun ham eng yomon holatda, u chiziqli saralash algoritmlariga qaraganda tez ishlashiga qaramay, eng yaxshi holatda uning samarasi chiziqli algoritmlardan past bo`ladi. Demak, birlashmali saralashni qisman saralangan massivlarni saralash uchun qo`llamagan ma'qul. Topshiriq Download 336.37 Kb.