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


C ++ yordamida tezkor tartiblash texnikasini amalga oshiramiz


Download 348.6 Kb.
bet6/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

C ++ yordamida tezkor tartiblash texnikasini amalga oshiramiz.

#include
using namespace std;
// Swap two elements - Utility function
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
// partition the array using last element as pivot
int partition (int arr[], int low, int high)
{
int i = (low - 1);
for (int j = low; j <= high- 1; j++)
{
//if current element is smaller than pivot, increment the low element
//swap elements at i and j
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
//quicksort algorithm
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
//partition the array
int pivot = partition(arr, low, high);
//sort the sub arrays independently
quickSort(arr, low, pivot - 1);
quickSort(arr, pivot + 1, high);
}
}
void displayArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
cout<}
int main()
{
int arr[] = {12,23,3,43,51};
int n = sizeof(arr)/sizeof(arr[0]);
cout<<"Input array"<displayArray(arr,n);
cout<quickSort(arr, 0, n-1);
cout<<"Array sorted with quick sort"<displayArray(arr,n);
return 0;
}

Chiqish natijasi:
Kirish qatori
12 23 3 43 51
Massiv Quicksort bilan tartiblangan
3 12 23 43 51
Yuqoridagi yorliqni bajarishda, bizda serialning oxirgi elementi bo'lgan pivot elementi atrofida kirish massivini ajratish uchun foydalaniladigan qism tartibi mavjud. Keyin biz rasmda ko'rsatilgandek sub-massivlarni alohida tartiblash uchun rekordni rekursiv ravishda chaqiramiz.


7.Merge Sort
Bu "bo'linish va zabt etish" strategiyasidan foydalanadigan yana bir usul. Ushbu texnikada biz birinchi navbatda ro'yxatni teng yarmga ajratamiz. Keyin ikkala ro'yxat saralanishi uchun biz ushbu ro'yxatlar bo'yicha tartiblash texnikasini mustaqil ravishda amalga oshiramiz. Va nihoyat, biz to'liq tartiblangan ro'yxatni olish uchun ikkala ro'yxatni ham birlashtiramiz.
Birlashtirish va tez tartiblash boshqa tartiblash texnikalariga qaraganda tezroq. Ro'yxat kattalashgan taqdirda ham ularning ishlashi saqlanib qoladi.

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