Reja. Bubble sort (Pufakchali saralash) Selection sort (Tanlab saralash) Insertion sort (Qo’shib saralash Quick sort (Tez saralash) Merge sort (Birlashtirib saralash) Shell sort (Qobiqsimon saralash) Heap sort(To’plab saralash) Large number sort


Keling, Merge Sort texnikasining rasmini ko'raylik


Download 348.6 Kb.
bet7/11
Sana08.06.2023
Hajmi348.6 Kb.
#1465411
1   2   3   4   5   6   7   8   9   10   11
Bog'liq
Murakkab saralash algortmlari juda ham katta sonlar bilan ishlas

Keling, Merge Sort texnikasining rasmini ko'raylik.

Yuqoridagi rasmda, birlashtirish tartiblash texnikasi har bir pastki chiziqda bitta element mavjud bo'lmaguncha, dastlabki massivni subarrayslarga qayta-qayta ajratib qo'yganini ko'ramiz. Bu amalga oshirilgandan so'ng, subarrayslar mustaqil ravishda saralanadi va to'liq tartiblangan qatorni yaratish uchun birlashtiriladi.
Keyingi, C ++ tilidan foydalanib, Birlashtirish tartibini amalga oshiramiz.

#include
using namespace std;
void merge(int *,int, int , int );
void merge_sort(int *arr, int low, int high)
{
int mid;
if (low < high){
//divide the array at mid and sort independently using merge sort
mid=(low+high)/2;
merge_sort(arr,low,mid);
merge_sort(arr,mid+1,high);
//merge or conquer sorted arrays
merge(arr,low,high,mid);
}
}
// Merge sort
void merge(int *arr, int low, int high, int mid)
{
int i, j, k, c[50];
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high) {
if (arr[i] < arr[j]) {
c[k] = arr[i];
k++;
i++;
}
else {
c[k] = arr[j];
k++;
j++;
}
}
while (i <= mid) {
c[k] = arr[i];
k++;
i++;
}
while (j <= high) {
c[k] = arr[j];
k++;
j++;
}
for (i = low; i < k; i++) {
arr[i] = c[i];
}
}
// read input array and call mergesort
int main()
{
int myarray[30], num;
cout<<"Enter number of elements to be sorted:";
cin>>num;
cout<<"Enter "<for (int i = 0; i < num; i++) { cin>>myarray[i];
}
merge_sort(myarray, 0, num-1);
cout<<"Sorted array\n";
for (int i = 0; i < num; i++)
{
cout<}
}


Download 348.6 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   10   11




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