From 8ae7847ee4f1fbc97ae518b99407523a00cbad6c Mon Sep 17 00:00:00 2001 From: abdurrezzak Date: Wed, 25 Oct 2017 15:39:09 +0300 Subject: [PATCH] Add files via upload --- 7.QuickSort.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 7.QuickSort.cpp diff --git a/7.QuickSort.cpp b/7.QuickSort.cpp new file mode 100644 index 0000000..520dec5 --- /dev/null +++ b/7.QuickSort.cpp @@ -0,0 +1,58 @@ +/* + * This program takes an array from the user + * sorts it using quicksort + * Prints the sorted array + * For more information about Quicksort Algorithm: https://en.wikipedia.org/wiki/Quicksort + * + * Coded by: Abdurrezak EFE + * + * */ +#include +#include +using namespace std; + +int partition(int arr[], int l, int r) // The used partition method is Lomuto's Partition +{ + int pivot = arr[r]; //choosing the last element as the pivot + + int i = (l - 1); //starting from the left + int temp; + + for (int j = l; j <= r- 1; j++) + { + if (arr[j] <= pivot) + { + i++; + temp = arr[i]; + arr[i] = arr[j],arr[j]=temp; //swapping elements + } + } + temp = arr[i+1], arr[i+1] = arr[r], arr[r] = temp; //swapping elements again + return (i + 1); +} + +void quick_sort(int arr[], int l, int r) +{ + if (l < r) + { + int p = partition(arr, l, r); + quick_sort(arr, l, p - 1); + quick_sort(arr, p + 1, r); + } +} + +int main() +{ + int k; + cout << "Enter the number of the integers you want to construct the array from: "; + cin >> k; + + int arr[k]; + for(int i=0;i> arr[i]; + + quick_sort(arr,0, k-1); //sorting + + for(int i=0;i