| a | b | |
|---|
| 0 | + | #include <iostream> |
|---|
| 0 | + | #include <vector> |
|---|
| 0 | + | |
|---|
| 0 | + | using namespace std; |
|---|
| 0 | + | |
|---|
| 0 | + | void swap(int &a, int &b) |
|---|
| 0 | + | { |
|---|
| 0 | + | int temp; |
|---|
| 0 | + | a = b; |
|---|
| 0 | + | b = temp; |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | int split_array(vector<int>& array, int pivot, int start_index, int end_index) |
|---|
| 0 | + | { |
|---|
| 0 | + | int left_boundary = start_index; |
|---|
| 0 | + | int right_boundary = end_index; |
|---|
| 0 | + | |
|---|
| 0 | + | while (left_boundary < right_boundary) { |
|---|
| 0 | + | while (pivot < array[right_boundary] && right_boundary > left_boundary) { |
|---|
| 0 | + | right_boundary--; |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | swap(array[left_boundary], array[right_boundary]); |
|---|
| 0 | + | |
|---|
| 0 | + | while (pivot >= array[left_boundary] && left_boundary < right_boundary) { |
|---|
| 0 | + | left_boundary++; |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | swap(array[right_boundary], array[left_boundary]); |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | return left_boundary; |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | void quicksort(vector<int>& array, int start_index, int end_index) |
|---|
| 0 | + | { |
|---|
| 0 | + | int pivot = array[start_index]; |
|---|
| 0 | + | int split_point; |
|---|
| 0 | + | |
|---|
| 0 | + | if (end_index > start_index) { |
|---|
| 0 | + | split_point = split_array(array, pivot, start_index, end_index); |
|---|
| 0 | + | array[split_point] = pivot; |
|---|
| 0 | + | quicksort(array, start_index, split_point-1); |
|---|
| 0 | + | quicksort(array, split_point+1, end_index); |
|---|
| 0 | + | } |
|---|
| 0 | + | } |
|---|
| ... | |
|---|