Dastur natijasi:
talabalar sonini kiriting=5
5 ta talabalar FIO sini kiriting
Farhod
Asror
Sobir
Bobur
Vali
| 2 | Asror |
| 4 | Bobur |
| 1 | Farhod |
| 3 | Sobir |
| 5 | Vali |
Bu algoritm jadvalni 3 ta o‘rinlashtirishda saraladi
Masalani yechish (algoritm, dastur kodi, natija).
// Online C++ compiler to run C++ program online
/* C++ implementation of QuickSort */
#include
using namespace std;
// A utility function to swap two elements
void swap(double* a, double* b)
{
double t = *a;
*a = *b;
*b = t;
}
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
double partition (double arr[], int low, int high)
{
double pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element and indicates the right position of pivot found so far
for (int j = low; j <= high - 1; j++)
{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(double arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
/* Function to print an array */
void printArray(double arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout <cout << endl;
}
;
// Driver Code
int main()
{
double arr[] = {4.200, 39.400, 6.800, 20.500, 6.300, 7.900,
110.800, 28.400, 160.000, 16.400, 5.100, 20.800, 15.300 };
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n - 1);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}
Xulosa.
Quick sort algoritmi boshqa algoritmlarga nisbatan tezligi bilan ajralib turadi.
Ushbu algoritm yuqoridagi kabi masalalarni juda soddalik va osonlik bilan xal etadi.
Foydalanilgan adabiyotlar.
Qudratxo’ja Musayev (“sralash algoritmlari”)
Jahongir Soataliyev (sralash algoritmlarining 5 xil usuli)
M.M.Aripov, N.A.Otaxanov (DASTURLASH ASOSLARI)Introduction to
Programming with C++ (Myprogramminglab) 3rd Edition by Y. Daniel Liang (Author), Y. Liang (Author)
C++ maruza Qudrat Abdurahimov
https://www.geeksforgeeks.org/selection-sort/
Do'stlaringiz bilan baham: |