Simple Quick Sort Program in C++
Definition
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.
Simple Quick Sort Program Example
/* Simple Quick Sort Program Using Functions and Array in C++*/
/* Data Structure C++ Programs,C++ Functions and Array Examples */
#include <iostream>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 5
using namespace std;
void quick_sort(int, int);
int arr_sort[MAX_SIZE];
int main() {
int i;
cout << "Simple C++ Quick Sort Example - Functions and Array\n";
cout << "\nEnter " << MAX_SIZE << " Elements for Sorting : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_sort[i];
cout << "\nYour Data :";
for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_sort[i];
}
quick_sort(0, MAX_SIZE - 1);
cout << "\n\nSorted Data :";
for (i = 0; i < MAX_SIZE; 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);
}
}
Sample Output
Simple Quick Sort Example - Functions and Array
Enter 5 Elements for Sorting
56
24
20
17
2
Your Data : 56 24 20 17 2
Sorted Data : 2 17 20 24 56
------------------
(program exited with code: 0)
Sorting Programs
- Simple Bubble Sort Program in C++
- Simple Bubble Sort Program using functions in C++
- Simple Insertion Sort Program in C++
- Simple Insertion Sort Program using functions in C++
- Simple Selection Sort Program in C++
- Simple Selection Sort Program using functions in C++
- Simple Quick Sort Program in C++
- Simple Merge Sort Program in C++
- Simple Shell Sort Program in C++
- Simple Shell Sort Program using functions in C++
- Simple Heap Sort Program in C++
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Example Program For Copy Constructor In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program For Constructor In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program for Inline Function Using C++ Programming
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Do While Loop Example Program In C++
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Function Template Using C++ Programming