Nov
28
2008
28
2008
C++ Program for QUICK SORT
C++ Program for QUICK SORT.
/************************************************************** Author: Arun Vishnu M V Web: www.arunmvishnu.com Description: C++ Program for QUICK SORT. ***************************************************************/ #include<conio.h> #include<iostream.h> #include<process.h> void quickSort(int numbers[], int array_size); void q_sort(int numbers[], int left, int right); int numbers[150]; int main() { clrscr(); int i,n; cout<<"How many numbers you want to sort: "; cin>>n; cout<<"Enter "<<n<<" numbers.\n"; for (i = 0; i<n; i++) cin>>numbers[i]; //perform quick sort on array q_sort(numbers,0,n-1); cout<<"Numbers are sorted\n"; for (i = 0; i<n; i++) cout<<numbers[i]<<" "; getch(); return(0); } // Function to sort void q_sort(int numbers[], int left, int right) { int pivot, l_hold, r_hold; l_hold = left; r_hold = right; pivot = numbers[left]; while (left < right) { while ((numbers[right] >= pivot) && (left < right)) right--; if (left != right) { numbers[left] = numbers[right]; left++; } while ((numbers[left] <= pivot) && (left < right)) left++; if (left != right) { numbers[right] = numbers[left]; right--; } } numbers[left] = pivot; pivot = left; left = l_hold; right = r_hold; if (left < pivot) q_sort(numbers, left, pivot-1); if (right > pivot) q_sort(numbers, pivot+1, right); } //---------------------- END--------------------
Related Posts
Latest Posts
- Google Account Activity gives you a monthly summary of your account activity across many Google products
- Apple released iOS 5.1
- You won’t be actually able to take 41MP photos using Nokia 808 PureView
- Apple Releases Mac OS X Mountain Lion 10.8 Developer Preview
- A walk down memory lane
- What is the real meaning of Facebook “Like”?
- Wish you and your family a very Happy and Prosperous New Year 2012!
- Import Orkut’s albums to Google+
- Download 100 Paid Android Apps(Like Fruit Ninja HD, BW, Smart Office etc. )For Free
- List Of Samsung Devices Getting Official Android 4.x Ice Cream Sandwich Update
Tags
apple
c and cpp
Cheats
download
downloads
Entertainment
Film
friendship
Fun
gadgets
Games
gmail
google
greetings
india
Internet
ipad
iphone
java script
linux
love
mac
microsoft
mobile
News
office
Orkut
Personal
photos
Programming
review
Security
software
source code
technology
Tips & Tricks
video
videos
virus
web
web development
windows
windows 7
worm
yahoo
Recent Comments
- Vikesh Kumar on C++ Program for QUICK SORT
- pratikchavan on C++ Program for QUICK SORT
- suraj on C++ Program Program for Binary Search.
- saravanan on Enabling REGEDIT and Task Manager
- VP on I Got My GirlFriend

An article by
Hi.
I need to find out a bug in the partition function of the quick sort in following program but i am not able to find it out.Can you help me.
void partition (int a[ ], int n) {
int pivot = a[0];
left = 0;
right = n-1;
while (left = pivot) right–;
if (left != right)
{
a[left] = a[right];
left++;
}
while (a[left] < pivot) left++;
if (left != right)
{
a[right] = a[left];
right–;
}
}
a[left] = pivot;
}
Hii Shilpi,
I am sorry, I didt gt time to go through the code.I am @ office and bsy now..
one error is: while (left = pivot) .. condition inside the while lop is
while (left == pivot)
i just wanna ask u that y u ve given the function prototype void quickSort(int numbers[], int array_size);
since it is not being used in prog.
great code.so helpful.
great code it is
I support you Anshul. There is no use of void quickSort(…); at all.