Ātrā atlase ir atlases algoritms, lai atrastu k-to mazāko elementu nesakārtotā sarakstā. Tas ir saistīts ar ātra šķirošana šķirošanas algoritms.
Piemēri:
Input: arr[] = {7, 10, 4, 3, 20, 15} k = 3 Output: 7 Input: arr[] = {7, 10, 4, 3, 20, 15} k = 4 Output: 10> Algoritms ir līdzīgs QuickSort. Atšķirība ir tāda, ka tā atkārtojas nevis abām pusēm (pēc pivota atrašanas), bet tikai tai daļai, kurā ir k-tais mazākais elements. Loģika ir vienkārša, ja sadalītā elementa indekss ir lielāks par k, tad mēs atkārtojam kreiso daļu. Ja indekss ir tāds pats kā k, mēs esam atraduši k-to mazāko elementu un atgriezīsimies. Ja indekss ir mazāks par k, tad mēs atkārtojam labo daļu. Tas samazina paredzamo sarežģītību no O(n log n) līdz O(n), sliktākajā gadījumā O(n^2).
function quickSelect(list, left, right, k) if left = right return list[left] Select a pivotIndex between left and right pivotIndex := partition(list, left, right, pivotIndex) if k = pivotIndex return list[k] else if k C++14 // CPP program for implementation of QuickSelect #include using namespace std; // Standard partition process of QuickSort(). // It considers the last element as pivot // and moves all smaller element to left of // it and greater elements to right int partition(int arr[], int l, int r) { int x = arr[r], i = l; for (int j = l; j <= r - 1; j++) { if (arr[j] <= x) { swap(arr[i], arr[j]); i++; } } swap(arr[i], arr[r]); return i; } // This function returns k'th smallest // element in arr[l..r] using QuickSort // based method. ASSUMPTION: ALL ELEMENTS // IN ARR[] ARE DISTINCT int kthSmallest(int arr[], int l, int r, int k) { // If k is smaller than number of // elements in array if (k>0 && k<= r - l + 1) { // Partition the array around last // element and get position of pivot // element in sorted array int index = partition(arr, l, r); // If position is same as k if (index - l == k - 1) return arr[index]; // If position is more, recur // for left subarray if (index - l>k - 1) return kthMallest(arr, l, index - 1, k); // Citādi atkārtojas labā apakšslāņa atgriešanai kthMallest(arr, index + 1, r, k - index + l - 1); } // Ja k ir vairāk nekā // elementu skaits masīvā, atgriež INT_MAX; } // Draivera programma, lai pārbaudītu iepriekš minētās metodes int main() { int arr[] = { 10, 4, 5, 8, 6, 11, 26 }; int n = izmērs(arr) / izmērs(arr[0]); int k = 3; cout<< 'K-th smallest element is ' << kthSmallest(arr, 0, n - 1, k); return 0; } Java // Java program of Quick Select import java.util.Arrays; class GFG { // partition function similar to quick sort // Considers last element as pivot and adds // elements with less value to the left and // high value to the right and also changes // the pivot position to its respective position // in the final array. public static int partition(int[] arr, int low, int high) { int pivot = arr[high], pivotloc = low; for (int i = low; i <= high; i++) { // inserting elements of less value // to the left of the pivot location if (arr[i] int temp = arr[i]; arr[i] = arr[pivotloc]; arr[pivotloc] = temp; pivotloc++; } } // swapping pivot to the final pivot location int temp = arr[high]; arr[high] = arr[pivotloc]; arr[pivotloc] = temp; return pivotloc; } // finds the kth position (of the sorted array) // in a given unsorted array i.e this function // can be used to find both kth largest and // kth smallest element in the array. // ASSUMPTION: all elements in arr[] are distinct public static int kthSmallest(int[] arr, int low, int high, int k) { // find the partition int partition = partition(arr, low, high); // if partition value is equal to the kth position, // return value at k. if (partition == k - 1) return arr[partition]; // if partition value is less than kth position, // search right side of the array. else if (partition 1) return kthSmallest(arr, partition + 1, high, k); // if partition value is more than kth position, // search left side of the array. else return kthSmallest(arr, low, partition - 1, k); } // Driver Code public static void main(String[] args) { int[] array = new int[] { 10, 4, 5, 8, 6, 11, 26 }; int[] arraycopy = new int[] { 10, 4, 5, 8, 6, 11, 26 }; int kPosition = 3; int length = array.length; if (kPosition>garums) { System.out.println('Index out of bound'); } else { // atrast k-to mazāko vērtību System.out.println( 'K-tais mazākais elements masīvā : ' + kthMallest(masīva kopija, 0, garums - 1, kPosition)); } } } // Šo kodu nodrošina Saiteja Pamulapati Python3 # Quick Select (QuickSort() standarta nodalījuma process Python3 programma. # Tas uzskata pēdējo elementu par pivot # un pārvieto visus mazākos elementus pa kreisi no tā un lielākos elementus uz labo def partition (arr, l, r): x = arr[r] i = l j diapazonā (l, r): ja arr[j]<= x: arr[i], arr[j] = arr[j], arr[i] i += 1 arr[i], arr[r] = arr[r], arr[i] return i # finds the kth position (of the sorted array) # in a given unsorted array i.e this function # can be used to find both kth largest and # kth smallest element in the array. # ASSUMPTION: all elements in arr[] are distinct def kthSmallest(arr, l, r, k): # if k is smaller than number of # elements in array if (k>0 un k<= r - l + 1): # Partition the array around last # element and get position of pivot # element in sorted array index = partition(arr, l, r) # if position is same as k if (index - l == k - 1): return arr[index] # If position is more, recur # for left subarray if (index - l>no bound') # Draivera kods arr = [ 10, 4, 5, 8, 6, 11, 26 ] n = len(arr) k = 3 print('K-tais mazākais elements ir ', end = ' ') print(kthMallest(arr, 0, n - 1, k)) # Šo kodu ir sagatavojis Muskans Kalra. C# // Ātrās atlases C# programma, izmantojot sistēmu; class GFG { // nodalījuma funkcija, kas līdzīga ātrajai kārtošanai // Uzskata pēdējo elementu par pivot un pievieno // elementus ar mazāku vērtību pa kreisi un // augstu vērtību pa labi, kā arī maina // pagrieziena pozīciju attiecīgajā pozīcijā / / tikai lasāmajā masīvā. statiskās int starpsienas(int []arr,int zems, int augsts) { int pivot = arr[high], pivotloc = zems, temp; for (int i = zems; i<= high; i++) { // inserting elements of less value // to the left of the pivot location if(arr[i] { temp = arr[i]; arr[i] = arr[pivotloc]; arr[pivotloc] = temp; pivotloc++; } } // swapping pivot to the readonly pivot location temp = arr[high]; arr[high] = arr[pivotloc]; arr[pivotloc] = temp; return pivotloc; } // finds the kth position (of the sorted array) // in a given unsorted array i.e this function // can be used to find both kth largest and // kth smallest element in the array. // ASSUMPTION: all elements in []arr are distinct static int kthSmallest(int[] arr, int low, int high, int k) { // find the partition int partition = partitions(arr,low,high); // if partition value is equal to the kth position, // return value at k. if(partition == k) return arr[partition]; // if partition value is less than kth position, // search right side of the array. else if(partition return kthSmallest(arr, partition + 1, high, k ); // if partition value is more than kth position, // search left side of the array. else return kthSmallest(arr, low, partition - 1, k ); } // Driver Code public static void Main(String[] args) { int[] array = {10, 4, 5, 8, 6, 11, 26}; int[] arraycopy = {10, 4, 5, 8, 6, 11, 26}; int kPosition = 3; int length = array.Length; if(kPosition>garums) { Console.WriteLine('Index out of bound'); } else { // atrast k-to mazāko vērtību Console.WriteLine('K-tais mazākais elements masīvā : ' + kthMallest(masīva kopija, 0, garums - 1, kPosition - 1)); } } } // Šo kodu nodrošina 29AjayKumar Javascript // Ātrās atlases Javascript programma // nodalījuma funkcija ir līdzīga ātrai kārtošanai // Uzskata pēdējo elementu par pivot un pievieno // elementus ar mazāku vērtību pa kreisi un // lielu vērtību pa labi un arī maina // pagrieziena pozīciju uz attiecīgo pozīciju // galīgajā masīvā. function _partition(arr, low, high) { let pivot = arr[high], pivotloc = zems; for (lai i = zems; i<= high; i++) { // inserting elements of less value // to the left of the pivot location if (arr[i] { let temp = arr[i]; arr[i] = arr[pivotloc]; arr[pivotloc] = temp; pivotloc++; } } // swapping pivot to the final pivot location let temp = arr[high]; arr[high] = arr[pivotloc]; arr[pivotloc] = temp; return pivotloc; } // finds the kth position (of the sorted array) // in a given unsorted array i.e this function // can be used to find both kth largest and // kth smallest element in the array. // ASSUMPTION: all elements in arr[] are distinct function kthSmallest(arr, low, high, k) { // find the partition let partition = _partition(arr, low, high); // if partition value is equal to the kth position, // return value at k. if (partition == k - 1) return arr[partition]; // if partition value is less than kth position, // search right side of the array. else if (partition return kthSmallest(arr, partition + 1, high, k); // if partition value is more than kth position, // search left side of the array. else return kthSmallest(arr, low, partition - 1, k); } // Driver Code let array = [ 10, 4, 5, 8, 6, 11, 26]; let arraycopy = [10, 4, 5, 8, 6, 11, 26 ]; let kPosition = 3; let length = array.length; if (kPosition>garums) { document.write('Index out of bound '); } else { // atrast k-to mazāko vērtību document.write( 'K-tais mazākais elements masīvā : ' + kthMallest(masīva kopija, 0, garums - 1, kPosition)+' '); } // Šo kodu nodrošina rag2127 Izvade: K-tais mazākais elements ir 6 Svarīgi punkti: Tāpat kā ātrā šķirošana, praksē tas ir ātrs, taču tam ir slikta sliktākā gadījuma veiktspēja. To izmanto sadaļā Sadalīšanas process ir tāds pats kā QuickSort, atšķiras tikai rekursīvais kods. Pastāv algoritms, kas atrod k-to mazāko elementu O(n) sliktākajā gadījumā, bet QuickSelect darbojas vidēji labāk. Saistītā C++ funkcija : std::nth_element programmā C++>