クイックソート
クイックソート時間の複雑さ: O ( nlogn ) O(nlogn)O ( nログn ) _ _
コード上で直接
ジャバ:
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Quicksort {
public static void main(String[] args) {
System.out.println(quicksort(Arrays.asList(10, 5, 2, 3))); // [2, 3, 5, 10]
}
private static List<Integer> quicksort(List<Integer> list) {
if (list.size() < 2) {
// base case, arrays with 0 or 1 element are already "sorted"
return list;
} else {
// recursive case
Integer pivot = list.get(0);
// sub-array of all the elements less than the pivot
List<Integer> less = list.stream().skip(1).filter(el -> el <= pivot)
.collect(Collectors.toList());
// sub-array of all the elements greater than the pivot
List<Integer> greater = list.stream().skip(1).filter(el -> el > pivot)
.collect(Collectors.toList());
return Stream.of(
quicksort(less).stream(),
Stream.of(pivot),
quicksort(greater).stream())
.flatMap(Function.identity()).collect(Collectors.toList());
}
}
}
子:
#include <stdio.h>
// Quick Sort
void quick_sort(int *array, int start, int end) {
if (start < end) {
int q = partition(array, start, end);
quick_sort(array, start, q - 1);
quick_sort(array, q + 1, end);
}
}
// Partition by pivot
int partition(int *array, int start, int end) {
int pivot = array[end];
int i = start - 1;
int temp = 0;
for (int j = start; j < end; j++) {
if (array[j] <= pivot) {
i++;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
temp = array[i + 1];
array[i + 1] = array[end];
array[end] = temp;
return i + 1;
}
int main(void) {
int arr[4] = {
10, 5, 2, 3};
quick_sort(arr, 0, 3);
// Print result
for (int i = 0; i < 4; i++) {
printf("%d ", arr[i]);
}
return 0;
}
C++:
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
template <typename T>
std::vector<T> quicksort(const std::vector<T>& arr) {
// base case, arrays with 0 or 1 element are already "sorted"
if (arr.size() < 2)
return arr;
// recursive case
const T* pivot = &arr.front() + arr.size() / 2 - 1; // set the pivot somewhere in the middle
std::vector<T> less; // vector to store all the elements less than the pivot
std::vector<T> greater; // vector to store all the elements greater than the pivot
for (const T* item = &arr.front(); item <= &arr.back(); item++) {
if (item == pivot) continue; // skip pivot element
if (*item <= *pivot) less.push_back(*item);
else greater.push_back(*item);
}
std::vector<T> sorted_less = quicksort(less);
std::vector<T> sorted_greater = quicksort(greater);
// concatenate less part, pivot and greater part
sorted_less.push_back(*pivot);
sorted_less.insert(sorted_less.end(), sorted_greater.begin(), sorted_greater.end());
return sorted_less;
}
int main() {
std::vector<int> arr = {
69, 60, 38, 82, 99, 15, 8, 94, 30, 42, 35, 40, 63, 1, 49, 66, 93, 83, 20, 32, 87, 6, 78, 17, 2, 61, 91, 25, 7, 4, 97, 31, 23, 67, 95, 47, 55, 92, 37, 59, 73, 81, 74, 41, 39};
std::vector<int> sorted = quicksort(arr);
for (int num : sorted) {
cout << num << " ";
}
cout << endl;
}
パイソン:
def quicksort(array):
if len(array) < 2:
# base case, arrays with 0 or 1 element are already "sorted"
return array
else:
# recursive case
pivot = array[0]
# sub-array of all the elements less than the pivot
less = [i for i in array[1:] if i <= pivot]
# sub-array of all the elements greater than the pivot
greater = [i for i in array[1:] if i > pivot]
return quicksort(less) + [pivot] + quicksort(greater)
print(quicksort([10, 5, 2, 3]))