Skip to content

Instantly share code, notes, and snippets.

@ouwenshi
Created March 1, 2018 22:01
Show Gist options
  • Select an option

  • Save ouwenshi/6171c3b58ad2f8036512a6a3629f7199 to your computer and use it in GitHub Desktop.

Select an option

Save ouwenshi/6171c3b58ad2f8036512a6a3629f7199 to your computer and use it in GitHub Desktop.
An implementation of quick sort.
#include <iostream>
#include <vector>
using namespace std;
template <class T>
int partition_array(int low, int high, vector<T> &input_array) {
int i = low;
for (int j = low; j < high; j++)
if (input_array[j] <= input_array[high])
swap(input_array[i++], input_array[j]);
swap(input_array[i], input_array[high]);
return i;
}
template <class T>
void quick_sort(int low, int high, vector<T> &input_array) {
if (low < high) {
int mid = partition_array(low, high, input_array);
quick_sort(low, mid - 1, input_array);
quick_sort(mid + 1, high, input_array);
}
}
void main() {
vector<int> my_array;
int number;
do {
cin >> number;
my_array.push_back(number);
} while (cin.get() != '\n');
quick_sort(0, my_array.size() - 1, my_array);
for (auto i : my_array)
cout << i << ' ';
cin.get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment