-
Notifications
You must be signed in to change notification settings - Fork 369
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 #1754 from tarunvyshnav777/quickselect
added quick select algorithm.
- Loading branch information
Showing
4 changed files
with
211 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,56 @@ | ||
// Path: C++\Searching\quick_select.cpp | ||
// C++ program to kth smallest element using quickSelect Algorithm. | ||
// Time-Complexity: O(N^2), where N is the size of array. | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <cstdlib> | ||
|
||
// Function to swap two elements in the vector | ||
void swap(int &a, int &b) { | ||
int temp = a; | ||
a = b; | ||
b = temp; | ||
} | ||
|
||
// Partition function to rearrange elements around the pivot | ||
int partition(std::vector<int> &vec, int left, int right, int pivotIndex) { | ||
int pivotValue = vec[pivotIndex]; | ||
swap(vec[pivotIndex], vec[right]); // Move pivot to the end | ||
int storeIndex = left; | ||
|
||
for (int i = left; i < right; i++) { | ||
if (vec[i] < pivotValue) { | ||
swap(vec[i], vec[storeIndex]); | ||
storeIndex++; | ||
} | ||
} | ||
swap(vec[storeIndex], vec[right]); // Move pivot to its final place | ||
return storeIndex; | ||
} | ||
|
||
// Quickselect function | ||
int quickSelect(std::vector<int> &vec, int left, int right, int k) { | ||
if (left == right) | ||
return vec[left]; | ||
|
||
int pivotIndex = left + (rand() % (right - left + 1)); | ||
pivotIndex = partition(vec, left, right, pivotIndex); | ||
|
||
if (k == pivotIndex) | ||
return vec[k]; | ||
else if (k < pivotIndex) | ||
return quickSelect(vec, left, pivotIndex - 1, k); | ||
else | ||
return quickSelect(vec, pivotIndex + 1, right, k); | ||
} | ||
|
||
int main() { | ||
std::vector<int> vec = {3, 8, 2, 5, 1, 4, 7, 6}; | ||
int k = 4; // Find the 4th smallest element | ||
|
||
int result = quickSelect(vec, 0, vec.size() - 1, k - 1); | ||
std::cout << "The " << k << "-th smallest element is: " << result << std::endl; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Path: C\Searching\quick_select.c | ||
// C program to kth smallest element using quickSelect Algorithm. | ||
// Time-Complexity: O(N^2), where N is the size of array. | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
// Function to swap two elements in the array | ||
void swap(int *a, int *b) { | ||
int temp = *a; | ||
*a = *b; | ||
*b = temp; | ||
} | ||
|
||
// Partition function to rearrange elements around the pivot | ||
int partition(int arr[], int left, int right, int pivotIndex) { | ||
int pivotValue = arr[pivotIndex]; | ||
swap(&arr[pivotIndex], &arr[right]); // Move pivot to the end | ||
int storeIndex = left; | ||
|
||
for (int i = left; i < right; i++) { | ||
if (arr[i] < pivotValue) { | ||
swap(&arr[i], &arr[storeIndex]); | ||
storeIndex++; | ||
} | ||
} | ||
swap(&arr[storeIndex], &arr[right]); // Move pivot to its final place | ||
return storeIndex; | ||
} | ||
|
||
// Quickselect function | ||
int quickSelect(int arr[], int left, int right, int k) { | ||
if (left == right) | ||
return arr[left]; | ||
|
||
int pivotIndex = left + (rand() % (right - left + 1)); | ||
pivotIndex = partition(arr, left, right, pivotIndex); | ||
|
||
if (k == pivotIndex) | ||
return arr[k]; | ||
else if (k < pivotIndex) | ||
return quickSelect(arr, left, pivotIndex - 1, k); | ||
else | ||
return quickSelect(arr, pivotIndex + 1, right, k); | ||
} | ||
|
||
int main() { | ||
int arr[] = {3, 8, 2, 5, 1, 4, 7, 6}; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
int k = 4; // Find the 4th smallest element | ||
|
||
int result = quickSelect(arr, 0, n - 1, k - 1); | ||
printf("The %d-th smallest element is: %d\n", k, result); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Path: Java\Searching\QuickSelect.java | ||
// Java program to kth smallest element using quickSelect Algorithm. | ||
// Time-Complexity: O(N^2), where N is the size of array. | ||
|
||
import java.util.Random; | ||
|
||
public class QuickSelect { | ||
|
||
// Function to swap two elements in the array | ||
private static void swap(int[] arr, int a, int b) { | ||
int temp = arr[a]; | ||
arr[a] = arr[b]; | ||
arr[b] = temp; | ||
} | ||
|
||
// Partition function to rearrange elements around the pivot | ||
private static int partition(int[] arr, int left, int right, int pivotIndex) { | ||
int pivotValue = arr[pivotIndex]; | ||
swap(arr, pivotIndex, right); // Move pivot to the end | ||
int storeIndex = left; | ||
|
||
for (int i = left; i < right; i++) { | ||
if (arr[i] < pivotValue) { | ||
swap(arr, i, storeIndex); | ||
storeIndex++; | ||
} | ||
} | ||
swap(arr, storeIndex, right); // Move pivot to its final place | ||
return storeIndex; | ||
} | ||
|
||
// Quickselect function | ||
private static int quickSelect(int[] arr, int left, int right, int k) { | ||
if (left == right) | ||
return arr[left]; | ||
|
||
int pivotIndex = left + new Random().nextInt(right - left + 1); | ||
pivotIndex = partition(arr, left, right, pivotIndex); | ||
|
||
if (k == pivotIndex) | ||
return arr[k]; | ||
else if (k < pivotIndex) | ||
return quickSelect(arr, left, pivotIndex - 1, k); | ||
else | ||
return quickSelect(arr, pivotIndex + 1, right, k); | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] arr = {3, 8, 2, 5, 1, 4, 7, 6}; | ||
int k = 4; // Find the 4th smallest element | ||
|
||
int result = quickSelect(arr, 0, arr.length - 1, k - 1); | ||
System.out.println("The " + k + "-th smallest element is: " + result); | ||
} | ||
} |
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,44 @@ | ||
# Path: Python\Searching\quick_select.py | ||
# Python program to kth smallest element using quickSelect Algorithm. | ||
# Time-Complexity: O(N^2), where N is the size of array. | ||
|
||
import random | ||
|
||
# Function to swap two elements in the list | ||
def swap(arr, a, b): | ||
arr[a], arr[b] = arr[b], arr[a] | ||
|
||
# Partition function to rearrange elements around the pivot | ||
def partition(arr, left, right, pivotIndex): | ||
pivotValue = arr[pivotIndex] | ||
swap(arr, pivotIndex, right) # Move pivot to the end | ||
storeIndex = left | ||
|
||
for i in range(left, right): | ||
if arr[i] < pivotValue: | ||
swap(arr, i, storeIndex) | ||
storeIndex += 1 | ||
swap(arr, storeIndex, right) # Move pivot to its final place | ||
return storeIndex | ||
|
||
# Quickselect function | ||
def quickSelect(arr, left, right, k): | ||
if left == right: | ||
return arr[left] | ||
|
||
pivotIndex = left + random.randint(0, right - left) | ||
pivotIndex = partition(arr, left, right, pivotIndex) | ||
|
||
if k == pivotIndex: | ||
return arr[k] | ||
elif k < pivotIndex: | ||
return quickSelect(arr, left, pivotIndex - 1, k) | ||
else: | ||
return quickSelect(arr, pivotIndex + 1, right, k) | ||
|
||
if __name__ == "__main__": | ||
arr = [3, 8, 2, 5, 1, 4, 7, 6] | ||
k = 4 # Find the 4th smallest element | ||
|
||
result = quickSelect(arr, 0, len(arr) - 1, k - 1) | ||
print(f"The {k}-th smallest element is: {result}") |