-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2521 from CoderGhost37/kushagra
Update contributorsList.js & added Quick_Sort.js
- Loading branch information
Showing
2 changed files
with
63 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 @@ | ||
// C++ program for implementation of Quick sort | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// Partition function | ||
int partition(int arr[], int low, int high) | ||
{ | ||
int pivot = arr[high]; // pivot | ||
int i = (low - 1); // Index of smaller element | ||
|
||
for (int j = low; j <= high - 1; j++) { | ||
// If current element is smaller than or | ||
// equal to pivot | ||
if (arr[j] <= pivot) { | ||
i++; // increment index of smaller element | ||
swap(arr[i], arr[j]); | ||
} | ||
} | ||
swap(arr[i + 1], arr[high]); | ||
return (i + 1); | ||
} | ||
|
||
// A function to implement quick sort | ||
// Time Complexity: O(nlogn) | ||
void quickSort(int arr[], int low, int high) | ||
{ | ||
if (low < high) | ||
{ | ||
// pi is partitioning index, arr[p] is now | ||
// at right place | ||
int pi = partition(arr, low, high); | ||
|
||
// Separately sort elements before | ||
// partition and after partition | ||
quickSort(arr, low, pi - 1); | ||
quickSort(arr, pi + 1, high); | ||
} | ||
} | ||
|
||
// Function to print an array | ||
void printArray(int arr[], int size) | ||
{ | ||
int i; | ||
for (i = 0; i < size; i++) | ||
cout << arr[i] << " "; | ||
cout << endl; | ||
} | ||
|
||
// Driver code | ||
int main() | ||
{ | ||
int arr[] = { 5, 1, 4, 2, 8}; | ||
int N = sizeof(arr) / sizeof(arr[0]); | ||
quickSort(arr, N); | ||
cout << "Sorted array: \n"; | ||
printArray(arr, N); | ||
return 0; | ||
} |
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