La clasificación es una de las funciones más básicas aplicadas a los datos. Significa organizar los datos de una manera particular, que puede ser creciente o decreciente. Hay una función incorporada en C++ STL con el nombre de sort().
Esta función utiliza internamente IntroSort. En más detalles, se implementa mediante un híbrido de QuickSort, HeapSort e InsertionSort. De forma predeterminada, utiliza QuickSort, pero si QuickSort realiza una partición injusta y tarda más de N*logN, cambia a HeapSort y cuando el tamaño de la array se vuelve realmente pequeño, cambia a Ordenar por inserción.
El prototipo de sort es:
C++
// C++ program to sort an array #include <algorithm> #include <iostream> using namespace std; void show(int a[], int array_size) { for (int i = 0; i < array_size; ++i) cout << a[i] << " "; } // Driver code int main() { int a[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 }; // size of the array int asize = sizeof(a) / sizeof(a[0]); cout << "The array before sorting is : \n"; // print the array show(a, asize); // sort the array sort(a, a + asize); cout << "\n\nThe array after sorting is :\n"; // print the array after sorting show(a, asize); return 0; }
C++
//quick sort code in C++ #include <iostream> using namespace std; int partition(int arr[], int start, int end) { int pivot = arr[start]; int count = 0; for (int i = start + 1; i <= end; i++) { if (arr[i] <= pivot) count++; } // Giving pivot element its correct position int pivotIndex = start + count; swap(arr[pivotIndex], arr[start]); // Sorting left and right parts of the pivot element int i = start, j = end; while (i < pivotIndex && j > pivotIndex) { while (arr[i] <= pivot) { i++; } while (arr[j] > pivot) { j--; } if (i < pivotIndex && j > pivotIndex) { swap(arr[i++], arr[j--]); } } return pivotIndex; } void quickSort(int arr[], int start, int end) { // base case if (start >= end) return; // partitioning the array int p = partition(arr, start, end); // Sorting the left part quickSort(arr, start, p - 1); // Sorting the right part quickSort(arr, p + 1, end); } int main() { int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0}; int n = 10; quickSort(arr, 0, n - 1); cout<<"array after using quick sort: "<<endl; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } return 0; } //this code is contributed by Machhaliya muhammad
C++
//C++ program of heap sort #include <iostream> using namespace std; // To heapify a subtree rooted with node i which is // an index in arr[]. n is size of heap void heapify(int arr[], int n, int i) { int largest = i; // Initialize largest as root int l = 2 * i + 1; // left = 2*i + 1 int r = 2 * i + 2; // right = 2*i + 2 // If left child is larger than root if (l < n && arr[l] > arr[largest]) largest = l; // If right child is larger than largest so far if (r < n && arr[r] > arr[largest]) largest = r; // If largest is not root if (largest != i) { swap(arr[i], arr[largest]); // Recursively heapify the affected sub-tree heapify(arr, n, largest); } } // main function to do heap sort void heapSort(int arr[], int n) { // Build heap (rearrange array) for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); // One by one extract an element from heap for (int i = n - 1; i >= 0; i--) { // Move current root to end swap(arr[0], arr[i]); // call max heapify on the reduced heap heapify(arr, i, 0); } } /* A utility function to print array of size n */ void printArray(int arr[], int n) { for (int i = 0; i < n; ++i) cout << arr[i] << " "; cout <<endl; } // Driver program int main() { int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 }; int n = 10; heapSort(arr, n); cout << "array after using heap sort:"<<endl; printArray(arr, n); } //this code is contributed by Machhaliya Muhammad
C++
//C++ program of insertion sort #include <bits/stdc++.h> using namespace std; // Function to sort an array using // insertion sort void insertionSort(int arr[], int n) { int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; // Move elements of arr[0..i-1], // that are greater than key, to one // position ahead of their // current position while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } } // A utility function to print an array // of size n void printArray(int arr[], int n) { int i; for (i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } // Driver code int main() { int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 }; int N =10; insertionSort(arr, N); cout<<"array after using insertion sort:"<<endl; printArray(arr, N); return 0; } //This code is contributed by Machhaliya Muhammad
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA