GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Sep-2007, 23:00
Peter_APIIT Peter_APIIT is offline
Account Disabled
 
Join Date: May 2007
Location: Malaysia
Posts: 520
Peter_APIIT can only hope to improve
Thumbs up

Quick Sort Algorithm


Hello all experience C++ programmer,

i doing some simple quick sort program but i read a ton of articles but still cannot solved it.

Example of articles:
goanna.cs.rmit.edu.au

I don't know how to divide an array into separate group. I even not understand how it works.

This is what i have done so far.

CPP / C++ / C Code:
/*
   The quick sort is an in-place, 
   divide-and-conquer, 
   massively recursive sort.

   1. Choose a pivot elemnt based 
      on middle of three(L, R, Middle)
   2. Swap the pivot with last element(R)
   3. Start searching from left, 
      if (number[left]>=pivot 
	   || left_index > right_index) 
	  {
	    stop();
	  }
   4. Start seaching from right, 
      if (number[right]<=pivot
	  || right_index < left_index)
	  { 
	    if (left_index!=right_index)
		{
		  swap the two element;
		}
		else
		{
		  swap the last element(pivot) 
		  most left element;
		}
	5. If both group more than 1 element
	   {
	      perform quick sort
	   }
	   else
	   {
	      done;
	   }
*/

#include<iostream>

using std::cout;
using std::cin;

void quickSort(int [], int);

// -----------------------------------------

int main(int argc, char *argv[])
{
	int number[] = {5, 4, 2, 3, 1};
	quickSort(number, 5);
	return 0;
}
// -----------------------------------------
void quickSort(int number[], int size)
{
	int leftptr = number[0];
	int rightptr = number[size - 1];
	int middle = size / 2;
	int tempPivot, pivot;
	int temp;

	/* Select a pivot based on middle of
       three
    */
	if (number[leftptr] > number[rightptr])
	{
		tempPivot = number[rightptr];
		if (tempPivot > number[middle])
		{
			pivot = tempPivot;
		}
		else
		{
			pivot = number[middle];
		}
	}
	else
	{
		tempPivot = number[leftptr];
		if (tempPivot > number[middle])
		{
			pivot = tempPivot;
		}
		else
		{
			pivot = number[middle];
		}
	}
	temp = pivot;
	pivot = number[rightptr];
	number[rightptr] = pivot;

}


I not asking you all to write for me but at least give some hint, explanation how it work and algorithms.

Thanks for your help.
  #2  
Old 28-Sep-2007, 16:28
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 115
ahbi82 will become famous soon enough

Re: Quick Sort Algorithm


There are many versions of quicksort. The quicksort that u require is a more complicated one.

Here's a link that you can understand better for "In place" algorithm

http://en.wikipedia.org/wiki/Quicksort

The persuode is quite understandable. A picture speaks a million words. Maybe you can try finding java applets demostrating sort. It gives you a visual feel of how the algorithm works. That's one method how i learn for sorting and algorithms during my Uni days.

Hope this helps!!!

  #3  
Old 03-Oct-2007, 23:43
Peter_APIIT Peter_APIIT is offline
Account Disabled
 
Join Date: May 2007
Location: Malaysia
Posts: 520
Peter_APIIT can only hope to improve
Thumbs up

Re: Quick Sort Algorithm


Please help me.
  #4  
Old 03-Oct-2007, 23:45
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 115
ahbi82 will become famous soon enough

Re: Quick Sort Algorithm


Quote:
Originally Posted by Peter_APIIT
Please help me.

What do you mean by help??

I did provide you with the resources u required.
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help quick sort template shinx C++ Forum 1 11-Dec-2006 15:00
Algorithm Help Please! daking_09 C++ Forum 0 24-May-2006 20:12
Quick, Insertion, and Partition silicon C++ Forum 0 18-May-2005 21:49
Merge and Heap...which is really faster silicon C++ Forum 0 10-May-2005 14:46
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 09:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:47.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.