Here is a sample program that I wrote using C++ to demonstrate the quick sort algorithm. It will ask the user to give how many items to be processed and then the program will sort and display the unsorted items and sorted items in the screen.
My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.
My mobile number here in the Philippines is 09173084360.
My telephone number at home here in Bacolod City, Negros Occidental Philippines is +63 (034) 4335675.
Here in Bacolod I also accepting computer repair, networking and Arduino Project development at a very affordable price.
My personal website is http://www.jakerpomperada.com
Sample Program Output
Program Listing
#include<iostream>
#include<cstdlib>
using namespace std;
void swap_numbers(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int Partition(int a[], int low, int high)
{
int pivot, index, i;
index = low;
pivot = high;
for(i=low; i < high; i++)
{
if(a[i] < a[pivot])
{
swap_numbers(&a[i], &a[index]);
index++;
}
}
swap_numbers(&a[pivot], &a[index]);
return index;
}
int RandomPivotPartition(int a[], int low, int high)
{
int pvt, n, temp;
n = rand();
pvt = low + n%(high-low+1);
swap_numbers(&a[high], &a[pvt]);
return Partition(a, low, high);
}
int QuickSort(int a[], int low, int high)
{
int pindex;
if(low < high)
{
pindex = RandomPivotPartition(a, low, high);
QuickSort(a, low, pindex-1);
QuickSort(a, pindex+1, high);
}
return 0;
}
int main()
{
int n=0, i=0;
cout <<"\n";
cout <<"Quick Sort in C++";
cout <<"\n";
cout<<"\nHow many items to be sorted? ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter item no. "<<i+1<<": ";
cin>>arr[i];
}
cout <<"\n";
cout<<"\nUnSorted Data ";
for (i = 0; i < n; i++)
cout<<" ,"<<arr[i];
cout <<"\n";
QuickSort(arr, 0, n-1);
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<" ,"<<arr[i];
cout <<"\n\n";
cout <<"\tEnd of Program";
cout <<"\n";
return 0;
}