diff --git a/C++/heap sort.cpp b/C++/heap sort.cpp new file mode 100644 index 0000000..2772af3 --- /dev/null +++ b/C++/heap sort.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +void heapify(int arr[], int n, int i) { + int largest = i; // Initialize largest as root + int left = 2*i + 1; // Left child + int right = 2*i + 2; // Right child + + // If left child is larger than root + if (left < n && arr[left] > arr[largest]) + largest = left; + + // If right child is larger than largest so far + if (right < n && arr[right] > arr[largest]) + largest = right; + + // If largest is not root + if (largest != i) { + swap(arr[i], arr[largest]); // Swap the elements + heapify(arr, n, largest); // Recursively heapify the affected subtree + } +} + +void buildHeap(int arr[], int n) { + // Build max heap (rearrange array) + for (int i = n / 2 - 1; i >= 0; i--) + heapify(arr, n, i); +} + +void heapSort(int arr[], int n) { + buildHeap(arr, n); // Build a max heap + + // One by one extract an element from heap + for (int i = n - 1; i > 0; i--) { + swap(arr[0], arr[i]); // Move current root to end + heapify(arr, i, 0); // call max heapify on the reduced heap + } +} + +int main() { + int arr1[] = {4, 1, 3, 9, 7}; + int n1 = sizeof(arr1)/sizeof(arr1[0]); + + heapSort(arr1, n1); + + cout << "Sorted array: "; + for (int i = 0; i < n1; i++) + cout << arr1[i] << " "; + cout << endl; + + int arr2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; + int n2 = sizeof(arr2)/sizeof(arr2[0]); + + heapSort(arr2, n2); + + cout << "Sorted array: "; + for (int i = 0; i < n2; i++) + cout << arr2[i] << " "; + cout << endl; + + return 0; +} \ No newline at end of file diff --git a/C++/quick sort.cpp b/C++/quick sort.cpp new file mode 100644 index 0000000..e6aebf0 --- /dev/null +++ b/C++/quick sort.cpp @@ -0,0 +1,114 @@ +//C++ Implementation of the Quick Sort Algorithm. +#include + +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[] = { 9, 3, 4, 2, 1, 8 }; + + int n = 6; + + + quickSort(arr, 0, n - 1); + + + for (int i = 0; i < n; i++) { + + cout << arr[i] << " "; + + } + + + return 0; +} \ No newline at end of file