C++ Programming Code Examples C++ > Sorting Searching Code Examples Simple Quick Sort Program in C++ Simple Quick Sort Program in C++ Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. Quicksort is a comparison sort, meaning that it can sort items of any type for which a "less-than" relation (formally, a total order) is defined. In efficient implementations it is not a stable sort, meaning that the relative order of equal sort items is not preserved. Quicksort can operate in-place on an array, requiring small additional amounts of memory to perform the sorting. #include <iostream> #include<conio.h> #include<stdlib.h> #define maxsize 5 using namespace std; void quick_sort(int, int); int arr_sort[maxsize]; int main() { int i; cout << "Simple C++ Quick Sort Example - Functions and Array\n"; cout << "\nEnter " << maxsize << " Elements for Sorting : " << endl; for (i = 0; i < maxsize; i++) cin >> arr_sort[i]; cout << "\nYour Data :"; for (i = 0; i < maxsize; i++) { cout << "\t" << arr_sort[i]; } quick_sort(0, maxsize - 1); cout << "\n\nSorted Data :"; for (i = 0; i < maxsize; i++) { cout << "\t" << arr_sort[i]; } getch(); } void quick_sort(int f, int l) { int i, j, t, p = 0; if (f < l) { p = f; i = f; j = l; while (i < j) { while (arr_sort[i] <= arr_sort[p] && i < l) i++; while (arr_sort[j] > arr_sort[p]) j--; if (i < j) { t = arr_sort[i]; arr_sort[i] = arr_sort[j]; arr_sort[j] = t; } } t = arr_sort[p]; arr_sort[p] = arr_sort[j]; arr_sort[j] = t; quick_sort(f, j - 1); quick_sort(j + 1, l); } }