GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / 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 31-Aug-2007, 23:17
eidalina20 eidalina20 is offline
New Member
 
Join Date: Aug 2007
Posts: 2
eidalina20 is on a distinguished road
Unhappy

please help with reverse


Hello everyone... i just wrote a simple quicksort program using C++ ..
it works fine sorting it and all..
but what i am trying to do now is to reverse the sort results...
so for instance if you type in: 5 7 3 9 6
it should give: 9 7 6 5 3 instead of 3 5 6 7 9

my code is:

CPP / C++ / C Code:
/* 
Assignment #1 - Quicksort
Alina Eideman
CS 575
*/

#include <iostream>
using namespace std;

#define ARRAY_SIZE 5                                  

void QuickSort(int* array, int left, int right);
int Partition(int* array, int pivotValue, int left, int right);
void swap(int &a, int &b);
void PrintArray(int* array, int n);


// This function does the quicksort

void QuickSort(int* array, int left, int right)
{
    int pivot = array[left];                    
    int sl; // split point
    
    if(left < right)                         
    {
        sl = Partition(array, pivot, left, right);
                                                     
        array[sl] = pivot;
        QuickSort(array, left, sl-1);   
        QuickSort(array, sl+1, right);     
    }
}


// This function splits the array around the pivot

int Partition(int* array, int pivot, int left, int right)
{
    int lb = left; //left boundary
    int rb = right; //right boundary
    
    while(lb < rb)               
    {
         while( pivot < array[rb]&& rb > lb)      
         {
              rb--;                        
         }
         swap(array[lb], array[rb]);
         
         while( pivot >= array[lb]&& lb < rb)      
         {
              lb++;                         
         }
         swap(array[rb], array[lb]);
    }
	return lb; 
}


// This function swaps two numbers

void swap(int &a, int &b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
}


// This function prints an array.

void PrintArray(int* array, int n)
{
    int i;
     
    for( i = 0; i < n; i++) cout<<array[i]<<'\t';
}


int main(void)
{
    int array[ARRAY_SIZE];
    int i;
    
    for( i = 0; i < ARRAY_SIZE; i++)                
    {
         cout<<"Enter an integer : ";
         cin>>array[i];
    }
    
    cout<<endl<<"The list you input is : "<<endl;
    PrintArray(array, ARRAY_SIZE);
    QuickSort(array,0,ARRAY_SIZE - 1);                
    cout<<endl<<"The list has been sorted, now it is : "<<endl;
    PrintArray(array, ARRAY_SIZE);
    
    cin.get();
    cin.get();
    return 0;
}


i am pretty sure the changes are minor... but not sure what needs to be changed... please anyone help me..

thanks!!!!
Last edited by LuciWiz : 31-Aug-2007 at 23:47. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 01-Sep-2007, 09:54
davis
 
Posts: n/a

Re: please help with reverse


Since it appears that you don't want to use the std::algorithm reverse operation, maybe this will work for you?

CPP / C++ / C Code:
#include <iostream>

using namespace std;

void swap( int* p1, int* p2 )
{
    int tmp = *p1;
    *p1 = *p2;
    *p2 = tmp;
}

void reverse( int* sorted, const int len )
{
    int* p_front = &sorted[0];
    int* p_back  = &sorted[len -1];

    while( p_front < p_back )
    {
        swap( p_front++, p_back-- );
    }
}

int main()
{
    int sorted[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
    reverse( sorted, sizeof( sorted ) / sizeof( int ) );
    for( int i = 0; i < (int)(sizeof( sorted ) / sizeof( int )); i++ )
    {
        cout << sorted[i] << endl;
    }

    return 0;
}

Output:

Code:
18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Also, why do:

CPP / C++ / C Code:
// you use
int* name

// but use
int &name

// and not
int& name


:davis:
  #3  
Old 01-Sep-2007, 10:32
eidalina20 eidalina20 is offline
New Member
 
Join Date: Aug 2007
Posts: 2
eidalina20 is on a distinguished road

Re: please help with reverse


hey,... ummmmm is there a way not to use reverse function.... maybe just change the "<" to">" somewhere.. ?

i mean i understand the use of reverse function but not sure how to apply to the code that i provided...

can anyone modify what i have?
  #4  
Old 01-Sep-2007, 22:27
sunboy sunboy is offline
New Member
 
Join Date: Aug 2006
Posts: 7
sunboy is on a distinguished road

Re: please help with reverse


i don't know whether i understand you or not.
just change two places;

CPP / C++ / C Code:
/* 
Assignment #1 - Quicksort
Alina Eideman
CS 575
*/

#include <iostream>
using namespace std;

#define ARRAY_SIZE 5                                  

void QuickSort(int* array, int left, int right);
int Partition(int* array, int pivotValue, int left, int right);
void swap(int &a, int &b);
void PrintArray(int* array, int n);


// This function does the quicksort

void QuickSort(int* array, int left, int right)
{
    int pivot = array[left];                    
    int sl; // split point
    
    if(left < right)                         
    {
        sl = Partition(array, pivot, left, right);
                                                     
        array[sl] = pivot;
        QuickSort(array, left, sl-1);   
        QuickSort(array, sl+1, right);     
    }
}


// This function splits the array around the pivot

int Partition(int* array, int pivot, int left, int right)
{
    int lb = left; //left boundary
    int rb = right; //right boundary
    
    while(lb < rb)               
    {
     while( pivot > array[rb]&& rb > lb)   //change from   pivot < array[rb]
         {
              rb--;                        
         }
         swap(array[lb], array[rb]);
         
         while( pivot <= array[lb]&& lb < rb)  //change from   pivot >= array[lb]
  
         {
              lb++;                         
         }
         swap(array[rb], array[lb]);
    }
	return lb; 
}


// This function swaps two numbers

void swap(int &a, int &b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
}


// This function prints an array.

void PrintArray(int* array, int n)
{
    int i;
     
    for( i = 0; i < n; i++) cout<<array[i]<<'\t';
}


int main(void)
{
    int array[ARRAY_SIZE];
    int i;
    
    for( i = 0; i < ARRAY_SIZE; i++)                
    {
         cout<<"Enter an integer : ";
         cin>>array[i];
    }
    
    cout<<endl<<"The list you input is : "<<endl;
    PrintArray(array, ARRAY_SIZE);
    QuickSort(array,0,ARRAY_SIZE - 1);                
    cout<<endl<<"The list has been sorted, now it is : "<<endl;
    PrintArray(array, ARRAY_SIZE);
    
    cin.get();
    cin.get();
    return 0;
}

Last edited by LuciWiz : 02-Sep-2007 at 05:58. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
 
 

Recent GIDBlog2nd Week of IA Training 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
Convert English phrases to morse code and it‘s reverse process pigfromsahara C Programming Language 25 13-Jun-2008 20:42
Reverse a number or digits tootoo CPP / C++ Forum 6 29-Jul-2007 20:34
reverse a string using recursion varun51 C Programming Language 1 13-Oct-2006 18:31
Reverse DNS entries admin Web Hosting Forum 1 21-Apr-2006 10:39
DNS report for my site... russ Web Hosting Forum 4 15-Oct-2004 23:13

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

All times are GMT -6. The time now is 03:52.


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