-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2e344c
commit 8ae7847
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <iostream> | ||
#include <algorithm> | ||
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<k;i++) | ||
cin >> arr[i]; | ||
|
||
quick_sort(arr,0, k-1); //sorting | ||
|
||
for(int i=0;i<k;i++) //printing the array | ||
cout << arr[i] << " "; | ||
} |